Android 大位图加载
说明:没对图片进行缓存处理,只是使用软引用进行位图资源的释放,从而避免内存泄漏。
对位图进行解码显示:
public Bitmap decodeBitmap(Resources resources, int resId, int reqWith, reqHeight ) {
//对位图进行解码的参数设置
BitmapFactory.Options options = new BitmapFactory.Options();
//在对位图进行解码的过程中,避免申请内存空间
options.inJustDecodeBounds = true;
BimapFactory.decodeResource(resources, resId, options);
//对图片进行一定比例的压缩处理
options.inSampleSize = caculateInSimpleSize(options, reqWith, reqHeight);
//真正输出位图
options.inJustDecodeBounds = false;
return BimapFactory.decodeResource(resources, resId, options);
}
计算图片压缩比例:
public int caculateInSimpleSize(BitmapFactory.Options options, int reqWidth, int reqHeight){
//
int imageHeight = options.outHeight;
int imageWidth = options.outWidth;
int inSimpeSize = 1; // 压缩比例
if (imageHeight > reqHeight || imageWidth > reqWidth) {
final int heightRatio = Math.round((float) imageHeight / (float) reqHeight );
final int widthRatio = Math.round((float) imageWidth / (float) reqWidth);
inSimpleSize = heightRatio < widthRatio ? heightRatio : widthRatio ;
}
return inSimpleSize;
}
网络图片请求:
public static byte[] sendPost (String path){
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttPost (path);
HttpResponse response = null;
try {
response = httpClient.execute(httpPost);
if (response.getStatusLine().getStatusCode() == 200) {
return EntityUtils.toByteArray(response.getEntity());
}
} catch (Exception e) {
e.printStackTrace();
} finally {
httpClient.getConnectionManager().shutdown();
}
return null;
}
批量加载大位图:
//在adpter中的getView中处理显示
public View getView(int position, View converView, ViewGroup parent) {
View view = null;
if (converView == null ){
view = LayoutInflater.from(MainActivity.this,).inflate(R.layout.item, null);
} else {
view = converView;
}
ImageView imageView = (ImageView) view.findViewById(R.id.item_image);
//获取图片
loadBitmap(path[position], imageView);
return view;
}
//在滑动ListView时,会对旧的布局进行资源回收,如果ListView结合异步任务操作时,不能确保重用的布局被及时回收。
static class AsyncDrawable extends BitmapDrawable{
private final SoftReference<BitmapWorkerTask> softReference;
public AsyncDrawable (Resources resources, Bitmap bitmap, BitmapWorkerTask bitmapWorkerTask) {
super(resources, bitmap);
softReference = SoftReference<MainActivity.BitmapWorkerTask>(bitmapWorkerTask);
} public BitmapWorkerTask getBitmapWorkerTask() {
return softReference.get();
}
} /**异步任务**/
class BitmapWorkerTask extends AsyncTask<String, void, Bitmap> {
private SoftReference<ImageView> imageSoftReference;
private String data = ""; public BitmapWorkerTask (ImageView imageView) {
imageSoftReference = new SoftReference<ImageView>(imageView);
} @Override
protected Bitmap doInBackground (String... params) {
data = params[0];
byte[] result = sendPost(data);
// 对位图解码, 返回100*100
return decodeBitmap(result, 100, 100);
} @Override
potected void onPostExecute(Bitmap bitmap) {
super.onPostExecute(bitmap);
if (isCancelled()) {
bitmap = null;
}
if (imageSoftReference != null && bitmap != null) {
final ImageView imageView = imageSoftReference.get();
final BitmapWorkerTask bitmapWorkerTask = getBitmapWorkerTask(imageView);
// 加载异步任务是独立的线程,保证引用imageView和异步任务有关联才可以。
if (this == bitmapWorkerTask && imageView != null) {
imageView.setImageBitmap(bitmap);
}
}
}
} 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;
} // 检查是否有另外一个执行的异步任务和imageView来绑定,前一个异步任务进行取消操作
public static boolean cancelPotntialWork(String data, ImageView imageView) {
final BitmapWorkerTask bitmapWorkerTask = getBitmapWorkerTask(imageView);
if (bitmapWorkerTask != null) {
final String bitmapData = bitmapWorkerTask.data;
if (bitmapData != data) {
bitmapWorkerTask.cancel(true);
} else {
return false;
}
}
return true;
} //加载图片
public void loadBitmap(String data, ImageView imageView) {
Bitmap placeBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.empty_photo);
if (cancelPotntialWork(data, imageView)) {
final BitmapWorkerTask task = new BitmapWorkerTask(imageView);
final AsyncDrawable asyncDrawable = new AsyncDrawable(getResources(), placeBitmap, task);
imageView.setImageDrawable(asyncDrawable);
task.execute(data);
}
}
Android 大位图加载的更多相关文章
- Android照片墙-多图加载
http://blog.csdn.net/guolin_blog/article/details/9526203 照片墙这种功能现在应该算是挺常见了,在很多应用中你都可以经常看到照片墙的身影.它的设计 ...
- 深入探索Glide图片加载框架:做了哪些优化?如何管理生命周期?怎么做大图加载?
前言 Glide可以说是最常用的图片加载框架了,Glide链式调用使用方便,性能上也可以满足大多数场景的使用,Glide源码与原理也是面试中的常客. 但是Glide的源码内容比较多,想要学习它的源码往 ...
- Android高清巨图加载方案
1.今天看了鸿洋的<Android高清巨图加载方案>一文,对加载高清巨图时的解决方案有了一定的认识. 思路为: 提供一个设置图片的入口. 重写onTouchEvent,在里面根据用户移动的 ...
- Android 高清加载巨图方案 拒绝压缩图片
Android 高清加载巨图方案 拒绝压缩图片 转载请标明出处: http://blog.csdn.net/lmj623565791/article/details/49300989: 本文出自:[张 ...
- Android 高清加载巨图方案, 拒绝压缩图片
源地址:http://blog.csdn.net/lmj623565791/article/details/49300989 一.概述 距离上一篇博客有段时间没更新了,主要是最近有些私事导致的,那么就 ...
- Android 使用Glide加载网络图片等比例缩放
在做android图片加载的时候,由于手机屏幕受限,很多大图加载过来的时候,我们要求等比例缩放,比如按照固定的宽度,等比例缩放高度,使得图片的尺寸比例得到相应的缩放,但图片没有变形.显然按照andro ...
- Android 使用Picasso加载网络图片等比例缩放
在做android图片加载的时候,由于手机屏幕受限,很多大图加载过来的时候,我们要求等比例缩放,比如按照固定的宽度,等比例缩放高度,使得图片的尺寸比例得到相应的缩放,但图片没有变形.显然按照andro ...
- Android之批量加载图片OOM问题解决方案
一.OOM问题出现的场景和原因 一个好的app总少不了精美的图片,所以Android开发中图片的加载总是避免不了的,而在加载图片过程中,如果处理不当则会出现OOM的问题.那么如何彻底解决这个问题呢?本 ...
- Android大神 博客
https://github.com/yeungeek/awesome-android-person Android大神 受Trinea的开源项目的启发和参考,也准备列一列Android圈里的大神们. ...
随机推荐
- weblogic监控
http://wenku.baidu.com/link?url=tQPQ-dgm7NOkEGj_vemwtsPd6TJ6W3x6_0UBLgw61N982SwPlz-QFxqncsmPGqHwJAEF ...
- Android bluetooth用户自定义数据
default mac: [btif_core.c] btif_fetch_local_bdaddr() default device name: [btif_dm.c] btif_get_defau ...
- JavaWeb学习之什么是Servlet、如何使用servlet、为什么这样使用、servlet的虚拟路径、关于缺省Servlet(2)
1.什么是Servlet? * 服务器端Java程序,servlet需要交给服务器来运行. * 与javax.servlet.Servlet接口有关的java程序 2.如何使用servlet?[必须] ...
- Android - 控件android:ems属性
Android - 控件android:ems属性http://blog.csdn.net/caroline_wendy/article/details/41684255?utm_source=tui ...
- Win10 UAP 标题栏
//自定义标题栏 var view = Windows.UI.ViewManagement.ApplicationView.GetForCurrentView(); ApplicationViewTi ...
- thinkphp自动验证中的静态验证和动态验证和批量验证
1.静态定义 在模型类里面预先定义好该模型的自动验证规则,我们称为静态定义. 举例说明,我们在模型类里面定义了$_validate属性如下: class UserModel extends Model ...
- AgileEAS.NET SOA 中间件2013第四季度发布&部分功能开源预告
一.前言 AgileEAS.NET SOA 中间件平台是一款基于基于敏捷并行开发思想和Microsoft .Net构件(组件)开发技术而构建的一个快速开发应用平台.用于帮助中小型软件企业建立一条适合市 ...
- 如何实现Outlook 2010 下载邮件后自动删除服务器上的邮件
outlook2010---文件---信息---账户设置---选中要设置的帐号---双击点选要设置的邮箱---其他设置---高级---在服务器上保留邮件的副本---14天后删除服务器上的邮件副本,修改 ...
- 硬盘格式是MBR、GPT
装win7 64位要求硬盘格式是MBR 现在预装 Win8 的电脑大多是采用新版 UEFI 固件 + GPT 格式磁盘 GPT模式是针对整个硬盘的初始化而言,因此不存在某一个分区是GPT模式的说法.转 ...
- button hot key 热键
<Button x:Name="ScanIDButton" Margin="11,0,0,0" IsEnabled="{Binding Elem ...