如何通过代码将xml图层列表转换为layerdrawable
我正在尝试通过代码编写以下 xml 代码。
----drawable\my_layerdrawable.xml
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<bitmap android:src="@drawable/my_image"
android:gravity="left"/>
</item>
<item android:left="10dp">
<bitmap android:src="@drawable/my_image"
android:gravity="left" />
</item>
<item android:top="10dp">
<bitmap android:src="@drawable/my_image"
android:gravity="left"/>
</item>
<item android:top="10dp" android:left="10dp">
<bitmap android:src="@drawable/my_image"
android:gravity="left" />
</item>
<item android:top="20dp">
<bitmap android:src="@drawable/my_image"
android:gravity="left"/>
</item>
<item android:top="20dp" android:left="10dp">
<bitmap android:src="@drawable/my_image"
android:gravity="left" />
</item>
</layer-list>
我编写了以下块,但它正在拉伸图像。
InsetDrawable[] layers = new InsetDrawable[this.itemCount];
Resources resources = getResources();
ImageButton imgButton = (ImageButton) findViewById(R.id.btnItems);
int layerTop = 0;
for (int i = 0; i < this.itemCount; i++)
{
int layerLeft = i % 2 == 1 ? 5 : 0;
Drawable dr = resources.getDrawable(R.drawable.my_image);
layers[i] = new InsetDrawable(dr, layerLeft, layerTop, -layerLeft, -layerTop);
layerTop += i % 2 == 1 ? 10 : 0;
}
LayerDrawable layerDrawable = new LayerDrawable(layers);
imgButton.setImageDrawable(layerDrawable);
当我将可绘制 xml 分配给 imgButton
时,它工作正常,没有拉伸或任何更改。
ImageButton imgButton = (ImageButton) findViewById(R.id.btnItems);
imgButton.setImageResource(R.drawable.my_layerdrawable);
您有什么想法可以通过代码使图层可绘制吗?
谢谢。
I am trying to code the following xml by code.
----drawable\my_layerdrawable.xml
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<bitmap android:src="@drawable/my_image"
android:gravity="left"/>
</item>
<item android:left="10dp">
<bitmap android:src="@drawable/my_image"
android:gravity="left" />
</item>
<item android:top="10dp">
<bitmap android:src="@drawable/my_image"
android:gravity="left"/>
</item>
<item android:top="10dp" android:left="10dp">
<bitmap android:src="@drawable/my_image"
android:gravity="left" />
</item>
<item android:top="20dp">
<bitmap android:src="@drawable/my_image"
android:gravity="left"/>
</item>
<item android:top="20dp" android:left="10dp">
<bitmap android:src="@drawable/my_image"
android:gravity="left" />
</item>
</layer-list>
I coded the following block but it is stretching the images.
InsetDrawable[] layers = new InsetDrawable[this.itemCount];
Resources resources = getResources();
ImageButton imgButton = (ImageButton) findViewById(R.id.btnItems);
int layerTop = 0;
for (int i = 0; i < this.itemCount; i++)
{
int layerLeft = i % 2 == 1 ? 5 : 0;
Drawable dr = resources.getDrawable(R.drawable.my_image);
layers[i] = new InsetDrawable(dr, layerLeft, layerTop, -layerLeft, -layerTop);
layerTop += i % 2 == 1 ? 10 : 0;
}
LayerDrawable layerDrawable = new LayerDrawable(layers);
imgButton.setImageDrawable(layerDrawable);
When I assign the drawable xml to the imgButton
It is working correctly no stretch or any change.
ImageButton imgButton = (ImageButton) findViewById(R.id.btnItems);
imgButton.setImageResource(R.drawable.my_layerdrawable);
Do you have any idea to make the layer drawable by code?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用 BitmapDrawable。
您的代码将如下所示:
You could use BitmapDrawable.
Your code would then look like this: