setCloseButtonIcon(位图可绘制)不适用于ChromeCustomTab中的SVG [英] setCloseButtonIcon(Bitmap drawable) is not working with SVGs in ChromeCustomTab
问题描述
我需要在ChromeCustomTab Android中更改默认的跨图标,我正在使用以下代码使用后图标进行更改:
I need to change the default cross-icon in ChromeCustomTab Android, I am changing it with back-icon using the code below:
Bitmap icon = BitmapFactory.decodeResource(context.getResources(),R.drawable.ic_arrow_back_white_24dp);
它与PNG兼容,但与SVG兼容.
It is working fine with PNGs but not with SVGs.
根据本文档,我们必须根据本文档维护图像的大小.
As per this documentation, we have to maintain size of image according to this documentation.
https://developer.android.com/reference/android/support/customtabs/CustomTabsIntent.html#KEY_ICON
我认为它不起作用,因为它们没有遵循文档中给出的尺寸.
I think it is not working because they are not following the dimensions given in Documentation.
推荐答案
您需要返回有效的Bitmap
.对于VectorDrawable
,有必要做更多的事情.您可以使用以下方法:
You need to return a valid Bitmap
. For a VectorDrawable
it is necessary to do something more. You can use these methods:
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;
}
然后您可以像使用它一样
Then you can use it like:
builder.setCloseButtonIcon(bitmapFromDrawable(this, R.drawable. ic_arrow_back_white_24dp));
这篇关于setCloseButtonIcon(位图可绘制)不适用于ChromeCustomTab中的SVG的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!