您的位置:首页 > 移动开发 > Android开发

Android笔记(七十五) Android中的图片压缩

2016-01-11 02:10 447 查看
这几天在做图记的时候遇第一次遇到了OOM,好激动~~

追究原因,是因为在ListView中加载的图片太大造成的,因为我使用的都是手机相机直接拍摄的照片,图片都比较大,所以在加载的时候会出现内存溢出,那么我们就需要将图片压缩显示了。

首先,我们可以通过Bitmap.getWidth和 Bitmap.getHeight来获取一张图片的实际宽和高

MainActivity.java

package com.example.test3;

import java.io.IOException;
import java.io.InputStream;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.Display;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

public class MainActivity extends Activity {

private ImageView iv;
private Button bt;
private int screenWidth, screenHeigh;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

bt = (Button) findViewById(R.id.bt);
iv = (ImageView) findViewById(R.id.img);

// 获取屏幕信息
WindowManager wm = (WindowManager) getSystemService(WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();
screenWidth = display.getWidth();
screenHeigh = display.getHeight();

bt.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
try {
InputStream is = getAssets().open("pic.jpg");
Bitmap bitmap = BitmapFactory.decodeStream(is);
iv.setImageBitmap(bitmap);
int realWidth = bitmap.getWidth();
int realHeight = bitmap.getHeight();
Toast.makeText(MainActivity.this, "真实图片的宽:" + realWidth + ",真实图片的高:" + realHeight + "\n 屏幕宽度:"
+ screenWidth + ",屏幕高度:" + screenHeigh, Toast.LENGTH_SHORT).show();
is.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
}
}


运行结果:



当我们使用ListView来加载这些大图的时候,往往会出现内存溢出的情况,譬如下面:

MainActivity.java

package com.example.test3;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;

public class MainActivity extends Activity {

private ListView lv;
private List list = new ArrayList();

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

addItem();

lv = (ListView) findViewById(R.id.lv);
MyAdapter adapter = new MyAdapter(list, MainActivity.this);
lv.setAdapter(adapter);
}

private void addItem() {
list.add("");
list.add("");
list.add("");
list.add("");
}

class MyAdapter extends BaseAdapter {

private List mList;
private Context mContext;

public MyAdapter(List list, Context context) {
this.mList = list;
this.mContext = context;
}

@Override
public int getCount() {
// TODO Auto-generated method stub
return mList.size();
}

@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return mList.get(position);
}

@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {

View view = LayoutInflater.from(mContext).inflate(R.layout.item, null);
ImageView iv = (ImageView) view.findViewById(R.id.image);
try {
InputStream is = getAssets().open("pic.jpg");
Bitmap bitmap = BitmapFactory.decodeStream(is);
is.close();
iv.setImageBitmap(bitmap);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return view;
}

}
}


因为我们没有对adapter做任何优化,所以我们每次滑动界面的时候,都会调用getView方法然后加载图片,这个时候就会出现OOM异常:



虽然出现OOM异常的原因有N多种,但就上个例子中,我们只需要将图片按照一定比例去缩小,然后在ListView中加载缩略图,就可以解决掉。

在Android的BitmapFactory.Options类,可以帮助我们对图片进行一系列的配置(不知道这个表述是否正确)

该类有一个成员变量inJustDecodeBounds,源码中对该变量的描述如下

If set to true, the decoder will return null (no bitmap), but the out... fields will still be set, allowing the caller to query the bitmap without having to allocate the memory for its pixels.


如果将其设置为true,那么在BitmapFactory.decodeXXX的时候将返回为null(不返回bitmap),但是允许调用者在不给这些bitmap分配内存的情况下查询该bitmap的信息。

那么我们就可以在不将其加载到内存的情况下,获取到该图片的宽高,我们修改getView方法中的代码:

public View getView(int position, View convertView, ViewGroup parent) {

View view = LayoutInflater.from(mContext).inflate(R.layout.item, null);
ImageView iv = (ImageView) view.findViewById(R.id.image);

BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;

try {
InputStream is = getAssets().open("pic.jpg");
Bitmap bitmap = BitmapFactory.decodeStream(is, null, options);
int width = options.outWidth;
int height = options.outHeight;
Log.d("TTTT", "width=" + width + ",height=" + height);
is.close();
iv.setImageBitmap(bitmap);
} catch (IOException e) {
e.printStackTrace();
}

return view;
}


输出结果为:



并且在程序界面并没有显示图片,因为decodeStream返回值为null

继续修改getView方法,这次我们加载一张5000*5000的大图

@Override
public View getView(int position, View convertView, ViewGroup parent) {

View view = LayoutInflater.from(mContext).inflate(R.layout.item, null);
ImageView iv = (ImageView) view.findViewById(R.id.image);

BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;

try {
inputStream = getAssets().open("big_pic.png");
BitmapFactory.decodeStream(inputStream, null, options);
// 图片真实宽高
int picWidth = options.outWidth;
int picHeight = options.outHeight;
Log.d("TTTT", "图片的真实width:" + picWidth + ",图片的真实height:" + picHeight);
// 手机屏幕宽高
WindowManager wm = (WindowManager) getSystemService(WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();
int screenWidth = display.getWidth();
int screenHeight = display.getHeight();
Log.d("TTTT", "屏幕的width:" + screenWidth + ",屏幕的height:" + screenHeight);

// 获取缩放比例
int scaleX = picWidth / screenWidth;
int scaleY = picHeight / screenHeight;
// 设置默认缩放比例为1
int scale = 1;
if (scaleX >= scaleY & scaleY >= 1) {
scale = scaleX;
} else if (scaleY >= scaleX & scaleX >= 1) {
scale = scaleY;
}
Log.d("TTTT", "缩放比例为:" + scale);
options.inJustDecodeBounds = false;
options.inSampleSize = scale;
inputStream = getAssets().open("big_pic.png");
Bitmap bitmap = BitmapFactory.decodeStream(inputStream, null, options);
Log.d("TTTT", "缩放后的width:" + bitmap.getWidth() + ",缩放后的height:" + bitmap.getHeight());
inputStream.close();
iv.setImageBitmap(bitmap);
} catch (IOException e) {
e.printStackTrace();
}
return view;
}


再次运行:



系统提示:

图片的真实width:5000,图片的真实height:5000
屏幕的width:1080,屏幕的height:1776
缩放比例为:4
缩放后的width:1250,缩放后的height:1250


在上面的代码中我们可以看到,我们讲injust inJustDecodeBounds设置为了false,表示我们要将bitmap加载到内存中去了,并且我们使用inSampleSize来设置了缩放比例

至此,图片缩放完成
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: