简体   繁体   中英

How can i convert a statelistDrawable to bitmapdrawable in android

How can i convert a state list Drawable to bitmap drawable in android. I want to convert a statelist drawable into bitmap drawable.

I found a work around if i find any icon whose type is other then bitmap drawable then use this code here searchResult.getIcon() is my icon value

  Bitmap bitmap = Bitmap.createBitmap(searchResult.getIcon().getIntrinsicWidth(),searchResult.getIcon().getIntrinsicHeight(), Config.ARGB_8888);
            Canvas canvas = new Canvas(bitmap); 
            searchResult.getIcon().setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
            searchResult.getIcon().draw(canvas);
            itemIconBitmap = bitmap;
            e.printStackTrace();

You can use toBitmap or casting the current drawable

(stateListDrawable as? StateListDrawable)?.let { drawable ->
                            (drawable.current as BitmapDrawable).bitmap
                        }

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