在我的应用程序中,我使用LayerDrawable来显示覆盖图像.我这样做是成功使用layerDrawable.
之后我将layerDrawable设置为imageView.setImageDrawable(layerDrawable);
现在我想使用这个图像drawable take作为Bitmap并在下一个图像处理中使用.但是当我试图让Bitmap使用它时
((BitmapDrawable)imageLayout.getDrawable()).getBitmap();
Run Code Online (Sandbox Code Playgroud)
我得到以下错误.
04-04 12:56:02.102: E/AndroidRuntime(15127): java.lang.ClassCastException: android.graphics.drawable.LayerDrawable cannot be cast to android.graphics.drawable.BitmapDrawable
Run Code Online (Sandbox Code Playgroud)
所以我改变了我的图像处理的跟随并尝试将LayerDrawable转换为Drawable并将此drawable设置为imageLayout backGround.那就完美了. 我的问题是如何将LayerDrawable转换为drwable?
请任何一个帮助.给我一些想法.谢谢.
只是一个测试,我没试过这个
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;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5198 次 |
| 最近记录: |