How to convert the LayerDrawable to Drawable in Android?

雨燕双飞 提交于 2019-12-19 03:18:48

问题


In my app i use the LayerDrawable to display overlayer image.and i do this is successfully use of layerDrawable.

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

Now i want to use this image drawable take as Bitmap and use in next imageprocessing. but when i try to get the Bitmap use of 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 change the follow of my image processing and try to convert the LayerDrawable to Drawable and set this drawable as imageLayout backGround. then it's work perfectly. My problem is how to convert the LayerDrawable to drwable?

please any one help. give me some idea. Thanks.


回答1:


Just a test, I haven't 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;
}


来源:https://stackoverflow.com/questions/15805060/how-to-convert-the-layerdrawable-to-drawable-in-android

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!