如何通过代码将xml图层列表转换为layerdrawable

发布于 2024-11-28 03:58:49 字数 2027 浏览 0 评论 0原文

我正在尝试通过代码编写以下 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

泪冰清 2024-12-05 03:58:49

您可以使用 BitmapDrawable。

包装位图的可绘制对象,可以平铺、拉伸或对齐。您可以从文件路径、输入流、通过 XML 膨胀或从 Bitmap 对象创建 BitmapDrawable。

您的代码将如下所示:

Drawable[] layers = new Drawable[this.itemCount];
Resources resources = getResources();
ImageButton imgButton = (ImageButton) findViewById(R.id.btnItems);

DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);

for (int i = 0; i < this.itemCount; i++)
{
    Bitmap bm = BitmapFactory.decodeResource(resources, R.drawable.icon);
    BitmapDrawable bmd = new BitmapDrawable(bm);
    bmd.setGravity(Gravity.TOP);        
    bmd.setTargetDensity(metrics);
    layers[i] = bmd;   
}
LayerDrawable layerDrawable = new LayerDrawable(layers);

int layerTop = 0;
for (int i = 0; i < this.itemCount; i++)
{

    int layerLeft = i % 2 == 1 ? 5 : 0;
    layerDrawable.setLayerInset(i, layerLeft, layerTop, -layerLeft, -layerTop);
    layerTop += i % 2 == 1 ? 10 : 0;
}

imgButton.setImageDrawable(layerDrawable);

You could use BitmapDrawable.

A Drawable that wraps a bitmap and can be tiled, stretched, or aligned. You can create a BitmapDrawable from a file path, an input stream, through XML inflation, or from a Bitmap object.

Your code would then look like this:

Drawable[] layers = new Drawable[this.itemCount];
Resources resources = getResources();
ImageButton imgButton = (ImageButton) findViewById(R.id.btnItems);

DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);

for (int i = 0; i < this.itemCount; i++)
{
    Bitmap bm = BitmapFactory.decodeResource(resources, R.drawable.icon);
    BitmapDrawable bmd = new BitmapDrawable(bm);
    bmd.setGravity(Gravity.TOP);        
    bmd.setTargetDensity(metrics);
    layers[i] = bmd;   
}
LayerDrawable layerDrawable = new LayerDrawable(layers);

int layerTop = 0;
for (int i = 0; i < this.itemCount; i++)
{

    int layerLeft = i % 2 == 1 ? 5 : 0;
    layerDrawable.setLayerInset(i, layerLeft, layerTop, -layerLeft, -layerTop);
    layerTop += i % 2 == 1 ? 10 : 0;
}

imgButton.setImageDrawable(layerDrawable);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文