在非UI线程中处理Bitmap
2016-06-24 14:53
369 查看
译文出自谷歌安卓官网
BitmapFactory.decode*方法不能在UI线程中执行。因为加载这些图片所花费的时间是不可预知的,取决于各种各样的因素(从磁盘或网络上的加载速度,图片的大小,CPU的性能等等)。如果图片加载任务阻塞了UI线程,系统会为你的应用程序标记一个ANR,并且你可以选择关闭你的应用程序。
本课程引导你进入使用AsyncTask作为背景线程来处理图片加载任务,并且向你展示如何处理并发问题
使用AsyncTask
这个AsyncTask类提供了一个背景执行工作和将执行结果发布到UI线程中。要想使用AsyncTask,你得创建一个基类并重写其方法。
如下使用AsyncTask将图片加载到ImageView中,并且使用了decodeSampledBitmapFromResource()的例子
这个对ImageView的WeakReference确保AsyncTask不会阻碍ImageView被GC回收。当任务结束的时候,无法保证ImageView仍然在当前任务中,因此在onPostExecute( )中你必须检查ImageView的索引。例如,当用户从这个界面离开或者任务结束前配置发生了改变,这些会导致ImageView不存在,
开始异步加载Bitmap,并执行它
BitmapFactory.decode*方法不能在UI线程中执行。因为加载这些图片所花费的时间是不可预知的,取决于各种各样的因素(从磁盘或网络上的加载速度,图片的大小,CPU的性能等等)。如果图片加载任务阻塞了UI线程,系统会为你的应用程序标记一个ANR,并且你可以选择关闭你的应用程序。
本课程引导你进入使用AsyncTask作为背景线程来处理图片加载任务,并且向你展示如何处理并发问题
使用AsyncTask
这个AsyncTask类提供了一个背景执行工作和将执行结果发布到UI线程中。要想使用AsyncTask,你得创建一个基类并重写其方法。
如下使用AsyncTask将图片加载到ImageView中,并且使用了decodeSampledBitmapFromResource()的例子
class BitmapWorkerTask extends AsyncTask<Integer, Void, Bitmap> { private final WeakReference<ImageView> imageViewReference; private int data = 0; public BitmapWorkerTask(ImageView imageView) { // Use a WeakReference to ensure the ImageView can be garbage collected imageViewReference = new WeakReference<ImageView>(imageView); } // Decode image in background. @Override protected Bitmap doInBackground(Integer... params) { data = params[0]; return decodeSampledBitmapFromResource(getResources(), data, 100, 100)); } // Once complete, see if ImageView is still around and set bitmap. @Override protected void onPostExecute(Bitmap bitmap) { if (imageViewReference != null && bitmap != null) { final ImageView imageView = imageViewReference.get(); if (imageView != null) { imageView.setImageBitmap(bitmap); } } } }
这个对ImageView的WeakReference确保AsyncTask不会阻碍ImageView被GC回收。当任务结束的时候,无法保证ImageView仍然在当前任务中,因此在onPostExecute( )中你必须检查ImageView的索引。例如,当用户从这个界面离开或者任务结束前配置发生了改变,这些会导致ImageView不存在,
开始异步加载Bitmap,并执行它
public void loadBitmap(int resId, ImageView imageView) { BitmapWorkerTask task = new BitmapWorkerTask(imageView); task.execute(resId); }
相关文章推荐
- require.js的用法
- UIAlertController代替UIAlertView
- UEftp配置
- iOS UI篇10- Storyboard(多个Storyboard的连接)
- mongoVue此实现不是 Windows 平台 FIPS 验证的加密算法的一部分。的解决办法
- Mosquitto on OpenWrt
- 虚拟现实-VR-UE4-LEAP-Motion手势识别
- 【POJ1699】-Best Sequence 搜索剪枝
- php中生成标准uuid(guid)的方法
- 传入颜色返回图片
- JS获取URL中参数值(QueryString)的4种方法分享<转>
- Distinct Subsequences
- [LeetCode] Longest Consecutive Sequence
- ssm框架搭建+easyui增删改查实现
- 357. Count Numbers with Unique Digits
- Vue学习3--数据绑定语法
- iOS通知 you still need to add "remote-notification" to the list of your supported UIBackgroundModes
- 框架: require.js
- easyui dialog 关闭按钮怎么去掉
- vuejs和webpack项目(VueComponent)初尝试——瀑布流组件