Resize bitmap inside a drawable layer-list

99封情书 提交于 2020-01-11 05:30:35

问题


Currently I'm developing a Android application. I want to make a text (a header) with a background that looks like this:

 _______
/______/

I have the following code:

<?xml version="1.0" encoding="UTF-8"?> 
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape>    
            <gradient
              android:startColor="@color/jaarkleur"
              android:centerColor="@color/jaarkleur"
              android:endColor="#E66C2C00"
              android:angle="0" />
            <stroke android:width="1dp"
            android:color="#ff000000"
            />       
        </shape>
    </item>
    <item>
        <bitmap android:src="@drawable/overlay_left" android:gravity="left"  />
    </item>
    <item>
        <bitmap android:src="@drawable/overlay_right" android:gravity="right"  />
    </item>

</layer-list>

So I made to images so with a transparant triangle which are placed at both ends.

This works fine for headers with only one line, but when a header takes up two lines it looks like this:

So my question is: Is it possible to resize both images so looks correctly? Thank you!


回答1:


I solved it using two (half-transparant) images which represent the corners.

The XML looks like this:

<?xml version="1.0" encoding="UTF-8"?> 
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
        <item>
                <shape>    
                    <gradient
                      android:startColor="@color/jaarkleur"
                      android:centerColor="@color/jaarkleur"
                      android:endColor="#E66C2C00"
                      android:angle="0" />
                    <stroke android:width="1dp"
            android:color="#ff000000"
            />       
                </shape>
        </item>
        <item>
            <bitmap android:src="@drawable/overlay_left" android:gravity="left|top"  />
        </item>
        <item>
            <bitmap android:src="@drawable/overlay_right" android:gravity="right|bottom" />
        </item>        
</layer-list>



回答2:


Instead of using a layer-list with gradient / border stroke and a custom image, consider using a NinePatchDrawable.

There is also a handy 9-patch creater that comes with the android SDK.



来源:https://stackoverflow.com/questions/9608019/resize-bitmap-inside-a-drawable-layer-list

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!