在UI线程之外,多线程处理Bitmaps
多线程处理Bitmaps
图片载入的时间和很多因素有关(比方从网络或本地读取速度,图片的大小。CPU的能力),假设这些任务堵塞了UI线程,系统有可能会回收并关闭它(see Designing
for Responsiveness for more information).
使用异步任务
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);
}
}
}
}
使用 WeakReference来保存ImageView为了内存可以非常好的回收他们,可是不能保证异步任务结束的时候。该ImageView还存在,所以必须在
public void loadBitmap(int resId, ImageView imageView) {
BitmapWorkerTask task = new BitmapWorkerTask(imageView);
task.execute(resId);
}
并行处理
为了更好的使用内存,当用户滚动的时候就開始回收其它子View。假设每一个子View都使用一个异步任务。我们不能保证当异步任务结束的时候。相关联的view没有被回收掉。我们也不能保证异步任务依照我们的顺序运行。
static class AsyncDrawable extends BitmapDrawable {
private final WeakReference<BitmapWorkerTask> bitmapWorkerTaskReference; public AsyncDrawable(Resources res, Bitmap bitmap,
BitmapWorkerTask bitmapWorkerTask) {
super(res, bitmap);
bitmapWorkerTaskReference =
new WeakReference<BitmapWorkerTask>(bitmapWorkerTask);
} public BitmapWorkerTask getBitmapWorkerTask() {
return bitmapWorkerTaskReference.get();
}
}
在运行 BitmapWorkerTask之前。我们能够给ImageView绑定一个AsyncDrawable。
public void loadBitmap(int resId, ImageView imageView) {
if (cancelPotentialWork(resId, imageView)) {
final BitmapWorkerTask task = new BitmapWorkerTask(imageView);
final AsyncDrawable asyncDrawable =
new AsyncDrawable(getResources(), mPlaceHolderBitmap, task);
imageView.setImageDrawable(asyncDrawable);
task.execute(resId);
}
}
cancelPotentialWork这种方法检查是否有其它任务关联到该ImageView。
假设是的话,我们使用函数cancel()取消掉关联的任务。
非常少情况下,新任务的数据与之前的匹配而且交互。以下是cancelPotentialWork的实现:
public static boolean cancelPotentialWork(int data, ImageView imageView) {
final BitmapWorkerTask bitmapWorkerTask = getBitmapWorkerTask(imageView); if (bitmapWorkerTask != null) {
final int bitmapData = bitmapWorkerTask.data;
// If bitmapData is not yet set or it differs from the new data
if (bitmapData == 0 || bitmapData != data) {
// Cancel previous task
bitmapWorkerTask.cancel(true);
} else {
// The same work is already in progress
return false;
}
}
// No task associated with the ImageView, or an existing task was cancelled
return true;
}
一个辅助方法getBitmapWorkerTask(),用于检索与ImageView相关的任务。
private static BitmapWorkerTask getBitmapWorkerTask(ImageView imageView) {
if (imageView != null) {
final Drawable drawable = imageView.getDrawable();
if (drawable instanceof AsyncDrawable) {
final AsyncDrawable asyncDrawable = (AsyncDrawable) drawable;
return asyncDrawable.getBitmapWorkerTask();
}
}
return null;
}
最后一步onPostExecute()在BitmapWorkerTask更新为了检验任务是否取消而且检验当前任务是否匹配该ImageView:
class BitmapWorkerTask extends AsyncTask<Integer, Void, Bitmap> {
... @Override
protected void onPostExecute(Bitmap bitmap) {
if (isCancelled()) {
bitmap = null;
} if (imageViewReference != null && bitmap != null) {
final ImageView imageView = imageViewReference.get();
final BitmapWorkerTask bitmapWorkerTask =
getBitmapWorkerTask(imageView);
if (this == bitmapWorkerTask && imageView != null) {
imageView.setImageBitmap(bitmap);
}
}
}
}
如今。我们就能使用异步任务非常好的在ListView和GridView里面或者其它组件里面更有效的回收资源。
我们仅仅须要运行loadBitmap
的方法就能放一张图片到ImageVIew里面。
比方,在GridVIew里面,我们在Adapter的getView方法里面使用这样的方法。
关于Bitmap的优化。请看上篇:Android有效的处理Bitmap,降低内存
最新文章
- Java程序员的日常—— Arrays工具类的使用
- Linux如何查看JDK的安装路径
- href的那些事
- ES5 vs ES6
- http://www.cnblogs.com/yjmyzz/p/dubbox-demo.html
- Java:多线程
- css 雪碧图的制作
- Java泛型:类型擦除
- input文字方框中,字体颜色的变化 要求默认的字体颜色是灰色,当要输入字时,字体是正常的黑色
- oracle配置odbc数据源
- Selenium_WebDriver_定位元素
- /usr,/usr/local/ 还是 /opt ?
- java:编程比赛中有用的方法整理(一)数组
- python之单例模式、栈、队列和有序字典
- python学习笔记1-python相关应用套件
- Flask 验证码 点击验证码刷新
- ThinkPHP页面跳转success与error方法
- 【读书笔记】iOS-处理内存警告
- NATS—发布/订阅机制
- 3.1 vue组件的使用