开发者

Android ImageButton with LayerDrawable Image

开发者 https://www.devze.com 2023-02-08 14:41 出处:网络
I\'m trying to display a LayerDrawable as an ImageButton image. All the layers must 开发者_如何学编程have different top values. such as top += 10;. For this I used setLayerInset with Drawable layer it

I'm trying to display a LayerDrawable as an ImageButton image. All the layers must 开发者_如何学编程have different top values. such as top += 10;. For this I used setLayerInset with Drawable layer items but it stretched the images. I also tried with different parameters and it just displayed other stupid layouts.

After the setLayerInset attempt I used InsetDrawable, giving the top parameter its own constructor:

new InsetDrawable(resources.getDrawable(R.drawable.soldier), 0, layerTop, 0, 0);

this time, the tops look OK, but ImageButton displays a very small area of it.

Basic layout, let say this is my image,

*---*
|   |
|   |
 ---

the layout should be,

*---*
|   |
*---*
|   |
*---*
|   |
*---*
|   |
|   |
 ---


I set the android:scaleType="fitStart" and changed the line as below and it is working like a charm.

new InsetDrawable(resources.getDrawable(R.drawable.soldier), 0, layerTop,0,-layerTop);

Now I have more questions,

First, as you can see I am creating a new instance for every layers. If I create only one Drawable object and set it to the every items of the array that will be used for creating the LayerDrawable and use layerDrawable.setLayerInset function to set the top and bottom properties, would it be more efficient.

And the other one if the LayerDrawable object has more items bigger than the imageButton it still displays the images. Is there a setting such as CSS overflow:hidden or something?

Thanks.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号