简体   繁体   English

NinePatchDrawable无法转换为android.graphics.drawable.BitmapDrawable

[英]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直接NinePatchDrawableBitmapDrawable ,因为它们是两种不同的不兼容的可绘制类型。

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.

相关问题 IonDrawable无法转换为android.graphics.drawable.BitmapDrawable - IonDrawable cannot be cast to android.graphics.drawable.BitmapDrawable android.graphics.drawable.AdaptiveIconDrawable 无法转换为 android.graphics.drawable.BitmapDrawable 错误 - android.graphics.drawable.AdaptiveIconDrawable cannot be cast to android.graphics.drawable.BitmapDrawable error java.lang.ClassCastException:android.graphics.drawable.ColorDrawable无法转换为android.graphics.drawable.BitmapDrawable - java.lang.ClassCastException: android.graphics.drawable.ColorDrawable cannot be cast to android.graphics.drawable.BitmapDrawable java.lang.ClassCastException:android.graphics.drawable.BitmapDrawable无法转换为android.graphics.drawable.LayerDrawable - java.lang.ClassCastException: android.graphics.drawable.BitmapDrawable cannot be cast to android.graphics.drawable.LayerDrawable android.graphics.drawable.BitmapDrawable 不能转换为 java.lang.CharSequence - android.graphics.drawable.BitmapDrawable cannot be cast to java.lang.CharSequence NotSerializableException:android.graphics.drawable.BitmapDrawable - NotSerializableException: android.graphics.drawable.BitmapDrawable 如何在Android中使用android.graphics.drawable.bitmapdrawable? - How to use android.graphics.drawable.bitmapdrawable in android? android.graphics.drawable.NinePatchDrawable无法转换为android.graphics.drawable.TransitionDrawable” - android.graphics.drawable.NinePatchDrawable cannot be cast to android.graphics.drawable.TransitionDrawable" 在目标类android.graphics.drawable.BitmapDrawable上找不到类型为int的getAlpha()方法 - Method getAlpha() with type int not found on target class class android.graphics.drawable.BitmapDrawable 无法转换为android.graphics.drawable.ColorDrawable - Cannot be cast to android.graphics.drawable.ColorDrawable
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM