开发者

How to exchange an image in a layer-list by a bitmap?

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|le开发者_StackOverflow社区ft" />
      </item>
    </layer-list>


The method setDrawableByLayerId in LayerDrawable takes (int, Drawable) not (int, Bitmap), so first create a drawable from your bitmap

Drawable drawable = new BitmapDrawable(bitmap);
layerDrawable.setDrawableByLayerId(R.id.dynamicItem, drawable);


To avoid stretching while shifting the image You must specify gravity. In my case it helps with mentioned solution:

android:gravity="top|left" 

or programmatically:

 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);
    }

On the screen You may see that standalone image is stretched and blur at the left side

How to exchange an image in a layer-list by a bitmap?

How to exchange an image in a layer-list by a bitmap?

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜