1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > android退出图标按钮 android-setCloseButtonIcon(位图可绘制)不适用于...

android退出图标按钮 android-setCloseButtonIcon(位图可绘制)不适用于...

时间:2023-10-18 09:36:47

相关推荐

android退出图标按钮 android-setCloseButtonIcon(位图可绘制)不适用于...

您需要返回一个有效的位图.对于VectorDrawable,有必要做更多的事情.您可以使用以下方法:

private static Bitmap bitmapFromDrawable(Context context, int drawableId) {

Drawable drawable = ContextCompat.getDrawable(context, drawableId);

if (drawable instanceof VectorDrawable) {

return bitmapFromVectorDrawable((VectorDrawable) drawable);

}

return ((BitmapDrawable) drawable).getBitmap();

}

@TargetApi(Build.VERSION_CODES.LOLLIPOP)

private static Bitmap bitmapFromVectorDrawable(VectorDrawable vectorDrawable) {

Bitmap bitmap = Bitmap.createBitmap(vectorDrawable.getIntrinsicWidth(), vectorDrawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);

Canvas canvas = new Canvas(bitmap);

vectorDrawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());

vectorDrawable.draw(canvas);

return bitmap;

}

然后您可以像这样使用它:

builder.setCloseButtonIcon(bitmapFromDrawable(this, R.drawable. ic_arrow_back_white_24dp));

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。