android--BitMap 你应该记住的一些事-1
2013-11-19 11:26
369 查看
[1].Mobile devices typically have constrained system resources. Android devices can have as little as 16MB of memory available
to a single application.
[2].Each
type of decode method has additional signatures that let you specify decoding options via the
Setting the
to
decoding avoids memory allocation, returning
the bitmap object but setting
This technique allows you to read the dimensions and type of the image data prior to construction (and memory allocation) of the bitmap.
[3].
[4].Note: In
the past, a popular memory cache implementation was a
cache, however this is not recommended. Starting from Android 2.3 (API Level 9) the garbage collector is more aggressive with collecting soft/weak references which makes them fairly ineffective. In addition, prior to Android 3.0 (API Level 11), the backing
data of a bitmap was stored in native memory which is not released in a predictable manner, potentially causing an application to briefly exceed its memory limits and crash.
[5].Note: In
this example, one eighth of the application memory is allocated for our cache. On a normal/hdpi device this is a minimum of around 4MB (32/8). A full screen
with images on a device with 800x480 resolution would use around 1.5MB (800*480*4 bytes), so this would cache a minimum of around 2.5 pages of images in memory.
[6].A
memory cache is useful in speeding up access to recently viewed bitmaps, however you cannot rely on images being available in this cache. Components like
larger datasets can easily fill up a memory cache. Your application could be interrupted by another task like a phone call, and while in the background it might be killed and the memory cache destroyed. Once the user resumes, your application has to process
each image again.
[7].Note: A
be a more appropriate place to store cached images if they are accessed more frequently, for example in an image gallery application.
to a single application.
[2].Each
type of decode method has additional signatures that let you specify decoding options via the
BitmapFactory.Optionsclass.
Setting the
inJustDecodeBoundsproperty
to
truewhile
decoding avoids memory allocation, returning
nullfor
the bitmap object but setting
outWidth,
outHeightand
outMimeType.
This technique allows you to read the dimensions and type of the image data prior to construction (and memory allocation) of the bitmap.
[3].
public static int calculateInSampleSize( BitmapFactory.Options options, int reqWidth, int reqHeight) { // Raw height and width of image final int height = options.outHeight; final int width = options.outWidth; int inSampleSize = 1; if (height > reqHeight || width > reqWidth) { final int halfHeight = height / 2; final int halfWidth = width / 2; // Calculate the largest inSampleSize value that is a power of 2 and keeps both // height and width larger than the requested height and width. while ((halfHeight / inSampleSize) > reqHeight && (halfWidth / inSampleSize) > reqWidth) { inSampleSize *= 2; } } return inSampleSize; }
public static Bitmap decodeSampledBitmapFromResource(Resources res, int resId, int reqWidth, int reqHeight) { // First decode with inJustDecodeBounds=true to check dimensions final BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true; BitmapFactory.decodeResource(res, resId, options); // Calculate inSampleSize options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight); // Decode bitmap with inSampleSize set options.inJustDecodeBounds = false; return BitmapFactory.decodeResource(res, resId, options); }
[4].Note: In
the past, a popular memory cache implementation was a
SoftReferenceor
WeakReferencebitmap
cache, however this is not recommended. Starting from Android 2.3 (API Level 9) the garbage collector is more aggressive with collecting soft/weak references which makes them fairly ineffective. In addition, prior to Android 3.0 (API Level 11), the backing
data of a bitmap was stored in native memory which is not released in a predictable manner, potentially causing an application to briefly exceed its memory limits and crash.
[5].Note: In
this example, one eighth of the application memory is allocated for our cache. On a normal/hdpi device this is a minimum of around 4MB (32/8). A full screen
GridViewfilled
with images on a device with 800x480 resolution would use around 1.5MB (800*480*4 bytes), so this would cache a minimum of around 2.5 pages of images in memory.
[6].A
memory cache is useful in speeding up access to recently viewed bitmaps, however you cannot rely on images being available in this cache. Components like
GridViewwith
larger datasets can easily fill up a memory cache. Your application could be interrupted by another task like a phone call, and while in the background it might be killed and the memory cache destroyed. Once the user resumes, your application has to process
each image again.
[7].Note: A
ContentProvidermight
be a more appropriate place to store cached images if they are accessed more frequently, for example in an image gallery application.
相关文章推荐
- android--service 你应该记住的一些事-1
- android--Process and Thread 你应该记住的一些事
- android--View 你应该记住的一些事
- android--Touch Events 你应该记住的一些事
- android--service 你应该记住的一些事-2
- android--Content Provider 你应该记住的一些事-1
- Android处理Bitmap的一些方法
- Android ORM——初识greenDAO 3及使用greenDAO 3前应该掌握的一些知识点(一)
- android BitmapFactory.Options(总结网络中出现的一些)
- android系列:Bitmap的一些操作
- 你应该知道的一些Android ADB 命令
- Android开发中解析、创建Bitmap对象时OOM的有效解决方法并附上一些干货
- Android bitmap 一些常用用法
- 关于Android屏幕适配应该知道的一些知识
- Android处理Bitmap的一些方法
- android如果给imageview做圆角,如果在原有的bitmap上加上一些修饰的drawable
- 最近项目中应该记住的一些经验
- Android Bitmap一些知识
- Android底层启动过程(应该说是应用进程init启动后的一些步骤)
- Android组建5:android中一些常见的类型转换ID/Drawable/Byte/Bitmap..