How to convert LayerDrawable to Drawable in Android?

In my application, I use LayerDrawable to display an overlay image. And I do this, it successfully uses layerDrawable.

after i get layerDrawable set as imageView.setImageDrawable (layerDrawable);

Now I want to use this image for drawing as a bitmap and use it in the next image processing. but when i try to use bitmap for this

((BitmapDrawable)imageLayout.getDrawable()).getBitmap();

I got the following error.

04-04 12:56:02.102: E/AndroidRuntime(15127): java.lang.ClassCastException: android.graphics.drawable.LayerDrawable cannot be cast to android.graphics.drawable.BitmapDrawable

so I modify the following image processing steps and try to convert LayerDrawable to Drawable and set this drawing as imageLayout backGround. then it works fine. My problem is how to convert LayerDrawable to drwable?

any help please. give me some idea. Thank.

+5
source share
1 answer

Just a test, I have not tried this

public Drawable geSingleDrawable(LayerDrawable layerDrawable){

          int resourceBitmapHeight = 136, resourceBitmapWidth = 153;

          float widthInInches = 0.9f;

          int widthInPixels = (int)(widthInInches * getResources().getDisplayMetrics().densityDpi);
          int heightInPixels = (int)(widthInPixels * resourceBitmapHeight / resourceBitmapWidth);

          int insetLeft = 10, insetTop = 10, insetRight = 10, insetBottom = 10;

          layerDrawable.setLayerInset(1, insetLeft, insetTop, insetRight, insetBottom);     

          Bitmap bitmap = Bitmap.createBitmap(widthInPixels, heightInPixels, Bitmap.Config.ARGB_8888);

          Canvas canvas = new Canvas(bitmap);
          layerDrawable.setBounds(0, 0, widthInPixels, heightInPixels);
          layerDrawable.draw(canvas);

          BitmapDrawable bitmapDrawable = new BitmapDrawable(getResources(), bitmap);
          bitmapDrawable.setBounds(0, 0, widthInPixels, heightInPixels);

          return bitmapDrawable;
}
+5
source

All Articles