Posts

Showing posts with the label Bitmap

Android: Resizing Bitmaps Without Losing Quality

Answer : Good downscaling algorithm (not nearest neighbor like) consists of just 2 steps (plus calculation of the exact Rect for input/output images crop): downscale using BitmapFactory.Options::inSampleSize->BitmapFactory.decodeResource() as close as possible to the resolution that you need but not less than it get to the exact resolution by downscaling a little bit using Canvas::drawBitmap() Here is detailed explanation how SonyMobile resolved this task: http://developer.sonymobile.com/2011/06/27/how-to-scale-images-for-your-android-application/ Here is the source code of SonyMobile scale utils: http://developer.sonymobile.com/downloads/code-example-module/image-scaling-code-example-for-android/ Try below mentioned code for resizing bitmap. public Bitmap get_Resized_Bitmap(Bitmap bmp, int newHeight, int newWidth) { int width = bmp.getWidth(); int height = bmp.getHeight(); float scaleWidth = ((float) newWidth) / width; float scaleH...

Android Bitmap Image Size In XML

Answer : Only API 23 or later Just use <item android:width="200dp" android:height="200dp" android:drawable="@drawable/splash_background" android:gravity="center" /> instead <item android:left="200dp" android:right="200dp"> <bitmap android:src="@drawable/splash_background" android:gravity="center" /> </item> Although there is no width and height parameters for bitmap, you can set bitmap size using gravity and item width/height. <item android:width="230dp" android:height="70dp"> <bitmap android:gravity="fill_horizontal|fill_vertical" android:src="@drawable/screen" /> </item> You can set item width and height and scale bitmap inside of it using gravity. I know this is old question but maybe someone will find it useful. WARNING: As already ...

Android: How Does Bitmap Recycle() Work?

Answer : The first bitmap is not garbage collected when you decode the second one. Garbage Collector will do it later whenever it decides. If you want to free memory ASAP you should call recycle() just before decoding the second bitmap. If you want to load really big image you should resample it. Here's an example: Strange out of memory issue while loading an image to a Bitmap object. I think the problem is this: On pre-Honeycomb versions of Android, the actual raw bitmap data is not stored in VM memory but in native memory instead. This native memory is freed when the corresponding java Bitmap object is GC'd. However , when you run out of native memory, the dalvik GC isn't triggered, so it is possible that your app uses very little of the java memory, so the dalvik GC is never invoked, yet it uses tons of native memory for bitmaps which eventually causes an OOM error. At least that's my guess. Thankfully in Honeycomb and later, all bitmap data is stored in...