- 浏览: 2144947 次
- 性别:
- 来自: 北京
文章分类
- 全部博客 (1240)
- mac/IOS (287)
- flutter (1)
- J2EE (115)
- android基础知识 (582)
- android中级知识 (55)
- android组件(Widget)开发 (18)
- android 错误 (21)
- javascript (18)
- linux (70)
- 树莓派 (18)
- gwt/gxt (1)
- 工具(IDE)/包(jar) (18)
- web前端 (17)
- java 算法 (8)
- 其它 (5)
- chrome (7)
- 数据库 (8)
- 经济/金融 (0)
- english (2)
- HTML5 (7)
- 网络安全 (14)
- 设计欣赏/设计窗 (8)
- 汇编/C (8)
- 工具类 (4)
- 游戏 (5)
- 开发频道 (5)
- Android OpenGL (1)
- 科学 (4)
- 运维 (0)
- 好东西 (6)
- 美食 (1)
最新评论
-
liangzai_cool:
请教一下,文中,shell、C、Python三种方式控制led ...
树莓派 - MAX7219 -
jiazimo:
...
Kafka源码分析-序列5 -Producer -RecordAccumulator队列分析 -
hp321:
Windows该命令是不是需要安装什么软件才可以?我试过不行( ...
ImageIO读jpg的时候出现javax.imageio.IIOException: Unsupported Image Type -
hp321:
Chenzh_758 写道其实直接用一下代码就可以解决了:JP ...
ImageIO读jpg的时候出现javax.imageio.IIOException: Unsupported Image Type -
huanghonhpeng:
大哥你真强什么都会,研究研究。。。。小弟在这里学到了很多知识。 ...
android 浏览器
Loading Large Bitmaps Efficiently
有效加载大型Bitmap
Images come in all shapes and sizes.
图片的形状和尺寸是各种各样的
In many cases they are larger than required for a typical application user interface (UI).
For example, the system Gallery application displays photos taken using your Android devices's camera which are typically much higher resolution than the screen density of your device.
很多情况中,他们都大于典型应用UI所需要的
例如,系统Gallery应用显示使用你的Android设备的camera拍出的照片,它们分辨率都远大于你的设备的屏幕密度
Given that you are working with limited memory, ideally you only want to load a lower resolution version in memory.
The lower resolution version should match the size of the UI component that displays it.
An image with a higher resolution does not provide any visible benefit, but still takes up precious memory and incurs additional performance overhead due to additional on the fly scaling.
由于你在一个有限内存的设备上工作,理想情况下,你只是想在内存中加载一个低分辨率的版本
这个低分辨率的版本应该正好与显示它的UI组件大小匹配
一个高分辨率的图像不会提供任何可见的好处,但是仍然会占据非常大的内存并且会引起额外的性能开销
This lesson walks you through decoding large bitmaps without exceeding the per application memory limit by loading a smaller subsampled version in memory.
这一课将给你展示通过在内存中加载一个小一些的子样版本来解码大bitmap而不会超过每一个应用内存限制
Read Bitmap Dimensions and Type
读取bitmap尺寸与类型
The BitmapFactory class provides several decoding methods (decodeByteArray(), decodeFile(), decodeResource(), etc.) for creating a Bitmap from various sources.
Choose the most appropriate decode method based on your image data source.
These methods attempt to allocate memory for the constructed bitmap and therefore can easily result in an OutOfMemory exception.
Each type of decode method has additional signatures that let you specify decoding options via the BitmapFactory.Options class.
Setting the inJustDecodeBounds property to true while decoding avoids memory allocation, returning null for the bitmap object but setting outWidth, outHeight and outMimeType. This technique allows you to read the dimensions and type of the image data prior to construction (and memory allocation) of the bitmap.
BitmapFactory类提供几种解码方法(decodeByteArray(), decodeFile(), decodeResource()等等)从资源中来建立一个bitmap
基于你的图片数据源选字最恰当的解码方式
这些方法试图为构建bitmap分配内存,因而可能很容易的导致一个OutOfMemory异常
每种解码方法都有额外的签名好让你通过BitmapFactory.Options指定解码选项
当解码的时候,设置属性inJustDecodeBounds=true来避免内存分配,返回值为null,但是设置了BitmapFactory.Options的outWidth, outHeight 和 outMimeType属性。
这种技术允许你在建立bitmap之前,读取图片数据的尺寸和类型
To avoid java.lang.OutOfMemory exceptions, check the dimensions of a bitmap before decoding it, unless you absolutely trust the source to provide you with predictably sized image data that comfortably fits within the available memory.
为了避免java.lang.OutOfMemory异常,在解码bitmap之前检查他的尺寸,除非你绝对信任提供给你的图片资源的大小是可知的,正好在可用内存范围内的
Load a Scaled Down Version into Memory
加载一个按比例比缩小版本的到内存中
Now that the image dimensions are known, they can be used to decide if the full image should be loaded into memory or if a subsampled version should be loaded instead.
Here are some factors to consider:
现在图片尺寸知道了,它们可以用于判断全图是否应该被加载到内存,或者应该加载子样版本
下面一些因素需要考虑:
Estimated memory usage of loading the full image in memory.
Amount of memory you are willing to commit to loading this image given any other memory requirements of your application.
Dimensions of the target ImageView or UI component that the image is to be loaded into.
Screen size and density of the current device.
For example, it’s not worth loading a 1024x768 pixel image into memory if it will eventually be displayed in a 128x96 pixel thumbnail in an ImageView.
评估加载全图所使用的内存
基于你的应用中任何其他内存需求,加载这个图片你愿意提供多少内存
图片要加载到的目标ImageView或者其他UI组件的尺寸
当前设备的屏幕尺寸和密度
例如,如果图片最终是要在一个ImageView中显示为一个128x96像素的缩略图,那么加载一张1024x768像素图片到内存中是不值得的
To tell the decoder to subsample the image, loading a smaller version into memory, set inSampleSize to true in your BitmapFactory.Options object.
For example, an image with resolution 2048x1536 that is decoded with an inSampleSize of 4 produces a bitmap of approximately 512x384.
Loading this into memory uses 0.75MB rather than 12MB for the full image (assuming a bitmap configuration of ARGB_8888).
Here’s a method to calculate a the sample size value based on a target width and height:
在你的BitmapFactory.Options对象中设置inSampleSize=true,可以告诉解码器把图片子样话,加载一个小版本到内存中
例如,一个分辨率为2048x1536的图片,使用inSampleSize=4进行编码,产出的bitmap大致为512x384.
一个基于目标宽高的方法来计算样本大小的值
Note: Using powers of 2 for inSampleSize values is faster and more efficient for the decoder.
However, if you plan to cache the resized versions in memory or on disk, it’s usually still worth decoding to the most appropriate image dimensions to save space.
注意:解码时使用2的方幂设置inSampleSize,速度和效率都更好一些
然而,如果你打算缓存调整过的图片版本到内存或者到磁盘中,通常还是需要解码到最合适的图片大小来节省空间
To use this method, first decode with inJustDecodeBounds set to true, pass the options through and then decode again using the new inSampleSize value and inJustDecodeBounds set to false:
使用这个方法,首先设置inJustDecodeBounds=true来解码,传递这个选项,然后使用新的inSampleSize值并且设置inJustDecodeBounds=false再解码一次
This method makes it easy to load a bitmap of arbitrarily large size into an ImageView that displays a 100x100 pixel thumbnail, as shown in the following example code:
这个方法使得加载一个任意大小的图片到一个ImageView显示一个100x100像素的缩略图变得容易,就像下面示例代码展示的那样
You can follow a similar process to decode bitmaps from other sources, by substituting the appropriate BitmapFactory.decode* method as needed.
根据需要通过替换恰当的BitmapFactory.decode*方法,你可以效仿出一个简单的过程来从其他资源中解码bitmap
原文地址如下,英文水平实在有限,希望拍砖同时能给予指正。
http://developer.android.com/training/displaying-bitmaps/load-bitmap.html
有效加载大型Bitmap
Images come in all shapes and sizes.
图片的形状和尺寸是各种各样的
In many cases they are larger than required for a typical application user interface (UI).
For example, the system Gallery application displays photos taken using your Android devices's camera which are typically much higher resolution than the screen density of your device.
很多情况中,他们都大于典型应用UI所需要的
例如,系统Gallery应用显示使用你的Android设备的camera拍出的照片,它们分辨率都远大于你的设备的屏幕密度
Given that you are working with limited memory, ideally you only want to load a lower resolution version in memory.
The lower resolution version should match the size of the UI component that displays it.
An image with a higher resolution does not provide any visible benefit, but still takes up precious memory and incurs additional performance overhead due to additional on the fly scaling.
由于你在一个有限内存的设备上工作,理想情况下,你只是想在内存中加载一个低分辨率的版本
这个低分辨率的版本应该正好与显示它的UI组件大小匹配
一个高分辨率的图像不会提供任何可见的好处,但是仍然会占据非常大的内存并且会引起额外的性能开销
This lesson walks you through decoding large bitmaps without exceeding the per application memory limit by loading a smaller subsampled version in memory.
这一课将给你展示通过在内存中加载一个小一些的子样版本来解码大bitmap而不会超过每一个应用内存限制
Read Bitmap Dimensions and Type
读取bitmap尺寸与类型
The BitmapFactory class provides several decoding methods (decodeByteArray(), decodeFile(), decodeResource(), etc.) for creating a Bitmap from various sources.
Choose the most appropriate decode method based on your image data source.
These methods attempt to allocate memory for the constructed bitmap and therefore can easily result in an OutOfMemory exception.
Each type of decode method has additional signatures that let you specify decoding options via the BitmapFactory.Options class.
Setting the inJustDecodeBounds property to true while decoding avoids memory allocation, returning null for the bitmap object but setting outWidth, outHeight and outMimeType. This technique allows you to read the dimensions and type of the image data prior to construction (and memory allocation) of the bitmap.
BitmapFactory类提供几种解码方法(decodeByteArray(), decodeFile(), decodeResource()等等)从资源中来建立一个bitmap
基于你的图片数据源选字最恰当的解码方式
这些方法试图为构建bitmap分配内存,因而可能很容易的导致一个OutOfMemory异常
每种解码方法都有额外的签名好让你通过BitmapFactory.Options指定解码选项
当解码的时候,设置属性inJustDecodeBounds=true来避免内存分配,返回值为null,但是设置了BitmapFactory.Options的outWidth, outHeight 和 outMimeType属性。
这种技术允许你在建立bitmap之前,读取图片数据的尺寸和类型
BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true; BitmapFactory.decodeResource(getResources(), R.id.myimage, options); int imageHeight = options.outHeight; int imageWidth = options.outWidth; String imageType = options.outMimeType;
To avoid java.lang.OutOfMemory exceptions, check the dimensions of a bitmap before decoding it, unless you absolutely trust the source to provide you with predictably sized image data that comfortably fits within the available memory.
为了避免java.lang.OutOfMemory异常,在解码bitmap之前检查他的尺寸,除非你绝对信任提供给你的图片资源的大小是可知的,正好在可用内存范围内的
Load a Scaled Down Version into Memory
加载一个按比例比缩小版本的到内存中
Now that the image dimensions are known, they can be used to decide if the full image should be loaded into memory or if a subsampled version should be loaded instead.
Here are some factors to consider:
现在图片尺寸知道了,它们可以用于判断全图是否应该被加载到内存,或者应该加载子样版本
下面一些因素需要考虑:
Estimated memory usage of loading the full image in memory.
Amount of memory you are willing to commit to loading this image given any other memory requirements of your application.
Dimensions of the target ImageView or UI component that the image is to be loaded into.
Screen size and density of the current device.
For example, it’s not worth loading a 1024x768 pixel image into memory if it will eventually be displayed in a 128x96 pixel thumbnail in an ImageView.
评估加载全图所使用的内存
基于你的应用中任何其他内存需求,加载这个图片你愿意提供多少内存
图片要加载到的目标ImageView或者其他UI组件的尺寸
当前设备的屏幕尺寸和密度
例如,如果图片最终是要在一个ImageView中显示为一个128x96像素的缩略图,那么加载一张1024x768像素图片到内存中是不值得的
To tell the decoder to subsample the image, loading a smaller version into memory, set inSampleSize to true in your BitmapFactory.Options object.
For example, an image with resolution 2048x1536 that is decoded with an inSampleSize of 4 produces a bitmap of approximately 512x384.
Loading this into memory uses 0.75MB rather than 12MB for the full image (assuming a bitmap configuration of ARGB_8888).
Here’s a method to calculate a the sample size value based on a target width and height:
在你的BitmapFactory.Options对象中设置inSampleSize=true,可以告诉解码器把图片子样话,加载一个小版本到内存中
例如,一个分辨率为2048x1536的图片,使用inSampleSize=4进行编码,产出的bitmap大致为512x384.
一个基于目标宽高的方法来计算样本大小的值
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) { // Calculate ratios of height and width to requested height and width final int heightRatio = Math.round((float) height / (float) reqHeight); final int widthRatio = Math.round((float) width / (float) reqWidth); // Choose the smallest ratio as inSampleSize value, this will guarantee // a final image with both dimensions larger than or equal to the // requested height and width. inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio; } return inSampleSize; }
Note: Using powers of 2 for inSampleSize values is faster and more efficient for the decoder.
However, if you plan to cache the resized versions in memory or on disk, it’s usually still worth decoding to the most appropriate image dimensions to save space.
注意:解码时使用2的方幂设置inSampleSize,速度和效率都更好一些
然而,如果你打算缓存调整过的图片版本到内存或者到磁盘中,通常还是需要解码到最合适的图片大小来节省空间
To use this method, first decode with inJustDecodeBounds set to true, pass the options through and then decode again using the new inSampleSize value and inJustDecodeBounds set to false:
使用这个方法,首先设置inJustDecodeBounds=true来解码,传递这个选项,然后使用新的inSampleSize值并且设置inJustDecodeBounds=false再解码一次
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); }
This method makes it easy to load a bitmap of arbitrarily large size into an ImageView that displays a 100x100 pixel thumbnail, as shown in the following example code:
这个方法使得加载一个任意大小的图片到一个ImageView显示一个100x100像素的缩略图变得容易,就像下面示例代码展示的那样
mImageView.setImageBitmap( decodeSampledBitmapFromResource(getResources(), R.id.myimage, 100, 100));
You can follow a similar process to decode bitmaps from other sources, by substituting the appropriate BitmapFactory.decode* method as needed.
根据需要通过替换恰当的BitmapFactory.decode*方法,你可以效仿出一个简单的过程来从其他资源中解码bitmap
原文地址如下,英文水平实在有限,希望拍砖同时能给予指正。
http://developer.android.com/training/displaying-bitmaps/load-bitmap.html
发表评论
-
带你深入理解 FLUTTER 中的字体“冷”知识
2020-08-10 23:40 567本篇将带你深入理解 Flutter 开发过程中关于字体和文 ... -
Flutter -自定义日历组件
2020-03-01 17:56 1025颜色文件和屏幕适配的文件 可以自己给定 import ... -
Dart高级(一)——泛型与Json To Bean
2020-02-23 19:13 925从 Flutter 发布到现在, 越来越多人开始尝试使用 Da ... -
flutter loading、Progress进度条
2020-02-21 17:03 1032Flutter Progress 1 条形无固定值进度条 ... -
Flutter使用Https加载图片
2020-02-21 01:39 901Flutter使用Https加载图片 使用http加载图片出 ... -
flutter shared_preferences 异步变同步
2020-02-21 00:55 765前言 引用 在开发原生iOS或Native应用时,一般有判断上 ... -
Flutter TextField边框颜色
2020-02-19 21:31 853监听要销毁 myController.dispose(); T ... -
flutter Future的正确用法
2020-02-18 21:55 728在flutter中经常会用到异步任务,dart中异步任务异步处 ... -
记一次Flutter简单粗暴处理HTTPS证书检验方法
2020-02-18 14:13 826最近在做Flutter项目到了遇到一个无解的事情,当使用Ima ... -
flutter 获取屏幕宽度高度 通知栏高度等屏幕信息
2019-07-27 08:39 1225##MediaQuery MediaQuery.of(con ... -
关于flutter RefreshIndicator扩展listview下拉刷新的问题
2019-07-10 19:40 998当条目过少时listview某些嵌套情况下可能不会滚动(条目 ... -
flutter listview 改变状态的时候一直无限添加
2019-07-10 16:01 676setstate的时候会一直无限的调用listview.bui ... -
Flutter Android端启动白屏问题的解决
2019-07-09 00:51 1402问题描述 Flutter 应用在 Android 端上启动时 ... -
Flutter中SnackBar使用
2019-07-08 23:43 679底部弹出,然后在指定时间后消失。 注意: build(Bui ... -
Flutter 之点击空白区域收起键盘
2019-07-08 18:43 1661点击空白处取消TextField焦点这个需求是非常简单的,在学 ... -
Flutter 弹窗 Dialog ,AlertDialog,IOS风格
2019-07-08 18:04 1289import 'package:flutter/mate ... -
flutter ---TextField 之 输入类型、长度限制
2019-07-08 14:30 2199TextField想要实现输入类型、长度限制需要先引入impo ... -
【flutter 溢出BUG】键盘上显示bottom overflowed by 104 PIXELS
2019-07-08 11:13 1447一开始直接使用Scaffold布局,body:new Colu ... -
解决Flutter项目卡在Initializing gradle...界面的问题
2019-07-07 12:53 790Flutter最近很火,我抽出了一点时间对Flutter进行了 ... -
关于android O 上 NotificationChannel 的一些注意事项
2019-07-04 11:47 887最近在适配android O,遇到个问题,应用中原本有设置界面 ...
相关推荐
包含翻译后的API文档:RoaringBitmap-0.5.11-javadoc-API文档-中文(简体)版.zip; Maven坐标:org.roaringbitmap:RoaringBitmap:0.5.11; 标签:roaringbitmap、RoaringBitmap、jar包、java、中文文档; 使用方法:...
包含翻译后的API文档:RoaringBitmap-0.7.45-javadoc-API文档-中文(简体)-英语-对照版.zip; Maven坐标:org.roaringbitmap:RoaringBitmap:0.7.45; 标签:roaringbitmap、RoaringBitmap、中英对照文档、jar包、java...
包含翻译后的API文档:RoaringBitmap-0.5.11-javadoc-API文档-中文(简体)-英语-对照版.zip; Maven坐标:org.roaringbitmap:RoaringBitmap:0.5.11; 标签:roaringbitmap、RoaringBitmap、jar包、java、API文档、...
包含翻译后的API文档:RoaringBitmap-0.7.45-javadoc-API文档-中文(简体)版.zip; Maven坐标:org.roaringbitmap:RoaringBitmap:0.7.45; 标签:roaringbitmap、RoaringBitmap、中文文档、jar包、java; 使用方法:...
bitmap-fangsongti-fonts-0.3-15.el6.noarch.rpm是centos工具包。
bitmap-console-fonts-0.3-15.el6.noarch.rpm是centos工具包。
bitmap-fonts-0.3-6.fc10.noarch.rpm
bitmap-lucida-typewriter-fonts-0.3-15.el6.noarch.rpm是centos工具包。
离线安装包,测试可用
离线安装包,亲测可用
bitmap-fixed-fonts-0.3-15.el6.noarch.rpm是centos工具包。
This project came about as part of my blog post: http://www.senab.co.uk/2012/07/01/android-bitmap-caching-revisited/ Android-BitmapCache is a specialised cache, for use with Android Bitmap objects. I...
若是从内存中读取到的Bitmap为空,则在磁盘缓存中读取。 若是从磁盘中读取到的Bitmap为空,则从起始资源中读取 2.开启工作线程,执行从起始资源中读取bitmap的任务 3.计算合适比例,读取适屏的bitmap 4.检查...
离线安装包,亲测可用
离线安装包,亲测可用
离线安装包,测试可用
官方离线安装包,亲测可用。使用rpm -ivh [rpm完整包名] 进行安装
离线安装包,亲测可用
离线安装包,测试可用
软件开发网在此之前给大家介绍过图片加载框架Glide的基本用法介绍,大家可以先参考一下,本篇内容更加深入的分析了Glide获取图片Path、Bitmap用法,以及实现的代码分析。 1. 获取Bitmap: 1)在图片下载缓存好之后...