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

做~自己de王妃 提交于 2019-12-02 08:03:46

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));
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!