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

后端 未结 1 1779
轻奢々
轻奢々 2021-01-23 23:31

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.decodeResou         


        
1条回答
  •  说谎
    说谎 (楼主)
    2021-01-24 00:17

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

    0 讨论(0)
提交回复
热议问题