问题
In my activity I am creating a Bitmap of the width and height same as the device resolution (width and height )
what I am doing
Bitmap mBitmap = Bitmap.createBitmap(screenWidth, screenHeight, Bitmap.Config.ARGB_8888);
and screenWidth and screenHeight is
screenHeight = displaymetrics.heightPixels;
screenWidth = displaymetrics.widthPixels;
Now if I make this bitmap I get my heap goes up to 19mb , which is not so good.
So tell me 2 things
1. What is a good way of creating the bitmap with respect to screen with and height with minimum memory consumed
2. How can I destroy the bitmap after using it ?
Please provide me a little source code or link of source code.
回答1:
What is a good way of creating the bitmap with respect to screen with and height with minimum memory consumed ?
- Never ever create a
Bitmapwith thescreen height or width, because that will be very huge based on thedensityof the device.Instead use theheightandwidthof the screen and calculate theaspect ratio. Now fix theheightto a constant value (like 1200px) and then calculate thewidthbased on theaspect ratio. TheImageViewor any other view will scale this properly. - If you really want transparency then use
Bitmap.Config.ARGB_8888, if not could useRGB_565or something else from the list here: http://developer.android.com/reference/android/graphics/Bitmap.Config.html - Follow below points if you are not creating a fresh
bitmapbut decoding thebitmapfrom some resource:- You need to use
BitmapFactory.Options.inSampleSizeto reduce thesampling. So how to calculate theinSampleSizeif you already know the height and width? Refer this here: http://developer.android.com/training/displaying-bitmaps/load-bitmap.html - So you do all this, and still the
inSampleSizeis not high enough and you get theOutOfMemoryError? Don't worry, if this is the case, you need tocatchthisOutOfMemoryErrorand in that increase yourinSampleSizeand do the bitmap decode again. Put this logic in a loop, so the crash never happens.
- You need to use
- Never ever create a
How can I destroy the bitmap after using it ?
- This is really simple. Make sure to call the
Bitmap.recycle()method. And remove all reference to the bitmap. Once you do this, the resource will be released and GC will do the cleaning up.
- This is really simple. Make sure to call the
UPDATE: When you create the a bitmap with screen_width and screen_height you would end up with a huge bitmap. Instead create a less resolution bitmap that fits the whole screen. You can see the following code on how this is done.
float screenHeight = displaymetrics.heightPixels;
float screenWidth = displaymetrics.widthPixels;
float aspectRatio = screenWidth/screenHeight;
int modifiedScreenHeight = 1000;
int modifiedScreenWidth = (int) (modifiedScreenHeight * aspectRatio);
Bitmap mBitmap = Bitmap.createBitmap(modifiedScreenWidth, modifiedScreenHeight, Bitmap.Config.ARGB_8888);
Now you got the bitmap with the right aspect ratio. You could use this bitmap as it is in the ImageView to fill the whole screen. Make sure you put android:scaleType="fitXY" for the ImageView.
来源:https://stackoverflow.com/questions/33392964/how-to-get-scaled-bitmap-with-respect-to-screen-size-with-out-getting-out-of-mem