Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Efficient way of creating Bitmap out of Drawable from res (BitmapFactory vs Type Casting)

Which method is more efficient for creating Bitmap out of Drawable from resources?

Bitmap myBitmap = BitmapFactory.decodeResource(context.getResources(),
                                       R.drawable.icon_resource);

Vs

Drawable myDrawable = getResources().getDrawable(R.drawable.icon_resource);
Bitmap myBitmap = ((BitmapDrawable) myDrawable).getBitmap();

since API 22 above method is deprecated so use following

Drawable myDrawable = ContextCompat.getDrawable(context, R.drawable.icon_resource)
like image 752
AndyW Avatar asked Nov 13 '22 11:11

AndyW


1 Answers

You can take a look at the source code for Bitmap factory at http://source.android.com specifically the code for decodeResource.

I would reason that using BitmapFactory is preferred but in either case if you are decoding multiple bitmaps then you should call getResources() once and store the result for use as the resources argument for the functions.

like image 120
Moog Avatar answered Nov 15 '22 06:11

Moog