简体   繁体   中英

setCloseButtonIcon(Bitmap drawable) is not working with SVGs in ChromeCustomTab

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);

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.

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));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM