[英]NinePatchDrawable cannot be cast to android.graphics.drawable.BitmapDrawable
我在imageview中使用了可绘制的九个补丁,并使用下面的代码来获取位图,但是会发生此错误。
Bitmap stickerBitmap = ((BitmapDrawable) Front.getDrawable()).getBitmap();
You cannot directly cast a NinePatchDrawable
to a BitmapDrawable
, since those are two different incompatible drawable types. 您不能将
NinePatchDrawable
直接NinePatchDrawable
为BitmapDrawable
,因为它们是两种不同的不兼容的可绘制类型。
If you want to get a Bitmap from a non-BitmapDrawable you have to draw it with a Canvas onto a Bitmap: 如果要从非BitmapDrawable获取位图,则必须使用画布将其绘制到位图上:
Drawable drawable = Front.getDrawable();
Bitmap stickerBitmap = Bitmap.createBitmap(Front.getWidth(),
Front.getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(stickerBitmap);
drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
drawable.draw(canvas);
Note that your ImageView has to be layouted so that it has a non-zero size. 请注意,必须对ImageView进行布局,使其大小不为零。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.