Displaying Bitmaps Efficiently(2)-Processing Bitmaps Off the UI Thread
来源:互联网 发布:单片机编程用什么语言 编辑:程序博客网 时间:2024/06/05 04:57
The BitmapFactory.decode*
methods, discussed in theLoad Large Bitmaps Efficiently lesson, should not be executed on the main UI thread if the source data is read from disk or a network location (or really any source other than memory). The time this data takes to load is unpredictable and depends on a variety of factors (speed of reading from disk or network, size of image, power of CPU, etc.). If one of these tasks blocks the UI thread, the system flags your application as non-responsive and the user has the option of closing it (seeDesigning for Responsiveness for more information).
This lesson walks you through processing bitmaps in a background thread usingAsyncTask
and shows you how to handle concurrency issues.
为防止从disk或network获取数据时延迟导致UI线程的阻塞,我们使用AsyncTask来处理图片展示。
Use an AsyncTask
The AsyncTask
class provides an easy way to execute some work in a background thread and publish the results back on the UI thread. To use it, create a subclass and override the provided methods. Here’s an example of loading a large image into anImageView
usingAsyncTask
anddecodeSampledBitmapFromResource()
:
class BitmapWorkerTask extends AsyncTask { private final WeakReference imageViewReference; private int data = 0; public BitmapWorkerTask(ImageView imageView) { // Use a WeakReference to ensure the ImageView can be garbage collected imageViewReference = new WeakReference(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); } } }}
The WeakReference
to theImageView
ensures that theAsyncTask
does not prevent theImageView
and anything it references from being garbage collected. There’s no guarantee theImageView
is still around when the task finishes, so you must also check the reference inonPostExecute()
. TheImageView
may no longer exist, if for example, the user navigates away from the activity or if a configuration change happens before the task finishes.
这里使用WeakReference来持有ImageView,这样就不会阻止别的地方对ImageView对象的释放;所以当在AsyncTask中使用ImageView时我们要先判断ImageView是否为空。
To start loading the bitmap asynchronously, simply create a new task and execute it:
public void loadBitmap(int resId, ImageView imageView) { BitmapWorkerTask task = new BitmapWorkerTask(imageView); task.execute(resId);}
- Displaying Bitmaps Efficiently(2)-Processing Bitmaps Off the UI Thread
- Displaying Bitmaps Efficiently - Processing Bitmaps Off the UI Thread
- Displaying Bitmaps Efficiently (二)-----Processing Bitmaps Off the UI Thread
- Displaying Bitmaps Efficiently之Processing Bitmaps Off the UI Thread
- Processing Bitmaps Off the UI Thread
- Processing Bitmaps Off the UI Thread
- Processing Bitmaps Off the UI Thread
- Processing Bitmaps Off the UI Thread
- Processing Bitmaps Off the UI Thread
- [Developer Android] Processing Bitmaps Off the UI Thread
- Processing Bitmaps Off the UI Thread不在UI线程中处理Bitmaps(Android官方翻译文档2)
- Displaying Bitmaps Efficiently之Displaying Bitmaps in Your UI
- Displaying Bitmaps Efficiently
- Displaying Bitmaps Efficiently
- Displaying Bitmaps Efficiently
- Displaying Bitmaps Efficiently
- Displaying Bitmaps Efficiently
- Displaying Bitmaps Efficiently
- jdk中解析BMP图片的过程
- .sscanf真的很好用
- 【项目经验】上传excel 之拼SQL语句
- winform 中 给DataGridView的表头添加CheckBox
- 计算星期几
- Displaying Bitmaps Efficiently(2)-Processing Bitmaps Off the UI Thread
- 问题 H: 数据结构(C语言版)算法6.1至算法6.4__二叉树遍历
- 【项目经验】Map类型得new实例化再用 不能作为全局变量后就不new 不然报空指针
- UE使用技巧
- 数据库变化监听方法
- 如何选择VC界面库产品?(二)– DSkinLite界面库介绍
- vs2008配置boost库
- .sprintf 用法
- bat批处理文件中常用命令释疑