How to share an image in a layer list using 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 list of layers (nothing impressive). I want to exchange 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> 
+3
android android-layout android-widget layer
source share
2 answers

The setDrawableByLayerId method in LayerDrawable accepts (int, Drawable) not (int, Bitmap), so first create an extract from your bitmap

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

To avoid stretching when moving the image, you must define gravity. In my case, this helps with the solution mentioned:

 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 can see that a single image is stretched and blurred on the left side

example

stretch zoom

+1
source share

All Articles