Android BitmapFactory图片压缩处理(大位图二次採样压缩处理)
Android实际开发中。在载入大量图片的时候。比方ViewPager、GridView、ListView中,载入了大量的比較大图片就easy出现OOM(内存溢出)的异常,这是由于一个应用的最大内存使用仅仅有16M。超过了这个值。就会出现OOM。
所以我们实际开发中,要想避免OOM出现就要对对应的图片进行压缩处理。
本文即使用了BitmapFactory和BitmapFactory.Option这两个类,对图片进行对应的尺寸压缩处理。
经測试,成功攻克了未压缩图片之前出现的OOM异常。
实现效果图:
本Demo使用的图片大小为2M左右(压缩曾经)。
我压缩图片之前:
我这里将压缩的代码凝视掉了:
执行结果:
进行压缩处理后的图片:
压缩后图片大小为:
大约为98KB
执行结果:
源码:
布局文件:
activity_main:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" > <ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" >
</ListView> </RelativeLayout>
list_item.xml:
<? xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" > <ImageView
android:id="@+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content" /> <TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"/> </LinearLayout>
MainActivity:
package com.android_bitmapcompressdemo; import android.app.Activity;
import android.os.Bundle;
import android.widget.ListView; public class MainActivity extends Activity {
private ListView listView;
private MyAdapter adapter;
private int[] items = new int[] { R.drawable.pc, R.drawable.pc,
R.drawable.pc, R.drawable.pc, R.drawable.pc, R.drawable.pc,
R.drawable.pc, R.drawable.pc, R.drawable.pc, R.drawable.pc,
R.drawable.pc, R.drawable.pc, R.drawable.pc, R.drawable.pc,
R.drawable.pc, R.drawable.pc, R.drawable.pc, R.drawable.pc,
R.drawable.pc, R.drawable.pc, R.drawable.pc, R.drawable.pc,
R.drawable.pc, R.drawable.pc, R.drawable.pc, R.drawable.pc,
R.drawable.pc, R.drawable.pc, R.drawable.pc, R.drawable.pc,
R.drawable.pc, R.drawable.pc, R.drawable.pc, R.drawable.pc,
R.drawable.pc, R.drawable.pc };
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); listView = (ListView) findViewById(R.id.listView);
adapter = new MyAdapter(this, items);
listView.setAdapter(adapter);
adapter.notifyDataSetChanged();
}
}
ListView适配器:MyAdapter:
package com.android_bitmapcompressdemo; import android.content.Context;
import android.graphics.Bitmap;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView; public class MyAdapter extends BaseAdapter {
private int[] items = new int[] {};
private Context context;
private String TAG = "zhongyao";
private Bitmap bitmap = null; public MyAdapter(Context context, int[] items) {
this.context = context;
this.items = items;
} @Override
public int getCount() {
return items.length;
} @Override
public Object getItem(int position) {
return items[position];
} @Override
public long getItemId(int position) {
return position;
} @Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = LayoutInflater.from(context).inflate(R.layout.list_item, null);
holder = new ViewHolder();
holder.imageView = (ImageView) convertView.findViewById(R.id.imageView);
holder.textView = (TextView) convertView.findViewById(R.id.textView); convertView.setTag(holder);
}else {
holder = (ViewHolder) convertView.getTag();
} bitmap = BitmapCompressTools.decodeSampledBitmapFromResource(
context.getResources(), R.drawable.pc, 100, 100);
Log.d(TAG, "压缩之后的图片大小为:" + bitmap.getByteCount());
holder.imageView.setImageBitmap(bitmap);
holder.textView.setText("图片"+position); return convertView;
} class ViewHolder {
ImageView imageView;
TextView textView;
}
}
BitmapCompressTools(压缩工具类):
package com.android_bitmapcompressdemo; import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory; public class BitmapCompressTools {
public static Bitmap decodeSampledBitmapFromResource(Resources res,
int resId, int reqWidth, int reqHeight) { // 给定的BitmapFactory设置解码的參数
final BitmapFactory.Options options = new BitmapFactory.Options();
// 从解码器中获取原始图片的宽高。这样避免了直接申请内存空间
options.inJustDecodeBounds = true;
BitmapFactory.decodeResource(res, resId, options); // Calculate inSampleSize
options.inSampleSize = calculateInSampleSize(options, reqWidth,
reqHeight); // 压缩完后便能够将inJustDecodeBounds设置为false了。
options.inJustDecodeBounds = false;
return BitmapFactory.decodeResource(res, resId, options);
} /**
* 指定图片的缩放比例
*
* @param options
* @param reqWidth
* @param reqHeight
* @return
*/ public static int calculateInSampleSize(BitmapFactory.Options options,
int reqWidth, int reqHeight) {
// 原始图片的宽、高
final int height = options.outHeight;
final int width = options.outWidth;
int inSampleSize = 1; // if (height > reqHeight || width > reqWidth) {
// //这里有两种压缩方式。可供选择。
// /**
// * 压缩方式二
// */
// // final int halfHeight = height / 2;
// // final int halfWidth = width / 2;
// // while ((halfHeight / inSampleSize) > reqHeight
// // && (halfWidth / inSampleSize) > reqWidth) {
// // inSampleSize *= 2;
// // }
//
/**
* 压缩方式一
*/
// 计算压缩的比例:分为宽高比例
final int heightRatio = Math.round((float) height
/ (float) reqHeight);
final int widthRatio = Math.round((float) width / (float) reqWidth);
inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;
// } return inSampleSize;
}
}
达到了预期效果。可是写到这里滑动ListView的时候,还是会有些卡顿,有这方面解决经验的能够给我留言讨论。
源码已上传到资源中,可到我的资源中下载。
Android BitmapFactory图片压缩处理(大位图二次採样压缩处理)的更多相关文章
- Android开发之大位图二次採样压缩处理(源码分享)
图片有各种形状和大小.在很多情况下这些图片是远远大于我们的用户界面(UI)且占领着极大的内存空间,假设我们不正确位图进行压缩处理,我们的程序会发生内存泄露的错误. MainActivity的代码 pa ...
- Android 中图片压缩分析(上)
作者: shawnzhao,QQ音乐技术团队一员 一.前言 在 Android 中进行图片压缩是非常常见的开发场景,主要的压缩方法有两种:其一是质量压缩,其二是下采样压缩. 前者是在不改变图片尺寸的情 ...
- Android Camera开发系列(上)——Camera的基本调用与实现拍照功能以及获取拍照图片加载大图片
Android Camera开发系列(上)--Camera的基本调用与实现拍照功能以及获取拍照图片加载大图片 最近也是在搞个破相机,兼容性那叫一个不忍直视啊,于是自己翻阅了一些基本的资料,自己实现了一 ...
- Android中图片压缩(质量压缩和尺寸压缩)
关于Android 图片压缩的学习: 自己总结分为质量压缩和像素压缩.质量压缩即:将Bitmap对象保存到对应路径下是所占用的内存减小,但是当你重新读取压缩后的file为Bitmap时,它所占用的内存 ...
- Android笔记(二十八) Android中图片之简单图片使用
用户界面很大程度上决定了APP是否被用户接收,为了提供友好的界面,就需要在应用中使用图片了,Android提供了丰富的图片处理功能. 简单使用图片 使用Drawable对象 为Android应用增加了 ...
- Android的图片压缩并上传
Android开发中上传图片很常见,一般为了节省流量会进行压缩的操作,本篇记录一下压缩和上传的方法. 图片压缩的方法 : import java.io.ByteArrayOutputStream; i ...
- Android 图片设置圆角 方法之二
Android中经常会遇到对图片进行二次处理,例如加圆角,或者显示圆形图片.接下来我们再介绍一种方法. 首先, 自定义ImageView: android:id="@+id/iv" ...
- Android bitmap图片处理
一.View转换为Bitmap 在Android中所有的控件都是View的直接子类或者间接子类,通过它们可以组成丰富的UI界面.在窗口显示的时候Android会把这些控件都加载到内存中 ...
- Android中图片的三级缓存策略
在开发过程中,经常会碰到进行请求大量的网络图片的样例.假设处理的不好.非常easy造成oom.对于避免oom的方法,无非就是进行图片的压缩.及时的回收不用的图片.这些看似简单可是处理起来事实上涉及的知 ...
随机推荐
- js作用域的理解
script:自上而下 全局变量.全局函数 函数:由里到外 浏览器: “JS解析器” 1)“找一些东西”: var function 参数 a = undefine 所有的变量,在正式运行代码之前,都 ...
- [AGC004D] Teleporter [贪心]
题面: 传送门 思路: 分析可知,这道题中的图是一个环套内向树,首都在环上 首先有一个结论:当首都的出边指向首都时,一定最优(不然首都出发可能无法按时到达首都)(可以按时到达的情况也一定有到不了的) ...
- 模块(二)——简单的log日志
简单的log日志 鉴于任何功能模块或系统在调试时都需要日志打印,这里随便写了一下,作为以后代码调试之用,只实现了不同等级的日志输出功能,其他的调试功能以后再行添加:使用方法简单,只需要在头文件里事先按 ...
- layer相关
关闭窗口 var index = parent.layer.getFrameIndex(window.name);parent.layer.close(index);
- html5拖动文件上传
使用html5的fileReader api <!DOCTYPE html><html lang="en"><head> <meta ch ...
- Swift实战-单例模式
设计模式(Design pattern)是一套被反复使用.多数人知晓的.经过分类编目的.代码设计经验的总结.GoF提出了23种设计模式,本系列将使用Swift语言来实现这些设计模式 概述 整个应用生命 ...
- ofbiz16 idea 启动
1.下载gradle并安装到本地 2.idea引入gradle 3.gradle右键选择refresh,项目会重新编译并加载gradle的task 4.可以再编译一下 5.没问题的话打开,jar ap ...
- pat 甲级 1065. A+B and C (64bit) (20)
1065. A+B and C (64bit) (20) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 HOU, Qiming G ...
- rsync Linux系统下的数据镜像备份工具
rsync是Linux系统下的数据镜像备份工具,从软件的命名上就可以看出来了——remote sync.rsync支持大多数的类Unix系统,无论是Linux.Solaris还是BSD上都经过了良好的 ...
- asp.net 字符串替换、截取。
有时候要在一段字符串里面把某些字符替换成其他字符,怎么办? 例如: string image=@"csks/news/user_top/qqqq/qqqq.jpg"; image ...