3

Here is my Bitmap

Bitmap bitmap = BitmapFactory.decodeStream((InputStream) new URL(url).getContent());
//Need some code to access "dynamicItem" and exchange it with my Bitmap

And here is my layer list (nothing spectacular). I want to exchange the dynamicItem with my Bitmap.

<?xml version="1.0" encoding="utf-8"?>
    <layer-list
      xmlns:android="http://schemas.android.com/apk/res/android">
      <item
        android:id="@+id/dynamicItem"
        android:drawable="@drawable/defaultItem" />
      <item>
        <bitmap

          android:src="@drawable/some_stuff_on_top"
          android:gravity="top|left" />
      </item>
    </layer-list>
4

2 回答 2

4

LayerDrawable 中的 setDrawableByLayerId 方法采用 (int, Drawable) 而不是 (int, Bitmap),因此首先从您的位图中创建一个可绘制对象

Drawable drawable = new BitmapDrawable(bitmap);
layerDrawable.setDrawableByLayerId(R.id.dynamicItem, drawable);
于 2011-02-23T23:04:23.370 回答
1

为避免在移动图像时拉伸您必须指定重力。就我而言,它有助于上述解决方案:

android:gravity="top|left" 

或以编程方式:

 private void shiftLayer(LayerDrawable pieceDrawable,int level){

        int l=level * shiftSize;
        int r=0;
        int t=0;
        int b=0;
        pieceDrawable.setLayerInset(level, l, t, r,  b);
        ((BitmapDrawable)pieceDrawable.getDrawable(level)).setGravity(Gravity.LEFT|Gravity.TOP);
    }

在屏幕上您可能会看到独立图像在左侧被拉伸和模糊

例子

拉伸缩放

于 2012-03-06T23:44:26.393 回答