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

android基础学习之android加载大图片

2015-02-02 16:00 246 查看
在网上查看了很多关于这些的资料,发现大体都差不多,自己根据自己的理解来使用,觉得最关键的就是bitmapfactory这个类中的decode方法和options的设置,

dedoce可从io流和file文件中解码成bitmap类的对象,并通过options的设置来确定是否加载和缩放图片,对于处理图片很是方便,举一个简单的例子如下:

此代码实现根据屏幕的宽高来缩放图片,并且解决了内存溢出的问题

// 获取屏幕的宽高

dm = new DisplayMetrics();

getActivity().getWindowManager().getDefaultDisplay().getMetrics(dm);

screenWidth = dm.widthPixels;

screenHeigh = dm.heightPixels;

/**

* 根据屏幕与图片的比例进行缩放bitmap并加载

*

* @param path

* @return

*/

public Bitmap decodeStreamByCompute(String path) {

Bitmap bitmap = null;

HttpURLConnection conn = null;

BitmapFactory.Options opts = new BitmapFactory.Options();

URL url;

try {

url = new URL(path);

conn = (HttpURLConnection) url.openConnection();

conn.setDoInput(true);// 设置输入流可读

conn.connect(); // 打开连接,必须语句

InputStream is = conn.getInputStream();

byte[] data = toByteArray(is);

opts.inJustDecodeBounds = true;

if (data == null) {

return null;

}

BitmapFactory.decodeByteArray(data, 0, data.length, opts);

int imageWidth = opts.outWidth;

int imageHeight = opts.outHeight;

if (imageWidth > 240 || imageHeight > 400) {

if (imageWidth / screenWidth > imageHeight / screenHeigh) {

opts.inSampleSize = imageWidth / screenWidth;// 宽的比例比高的比例大

} else {

opts.inSampleSize = imageHeight / screenHeigh;

}

opts.inJustDecodeBounds = false;

bitmap = BitmapFactory.decodeByteArray(data, 0, data.length,

opts);

} else {

opts.inSampleSize = 1;

opts.inJustDecodeBounds = false;

// bitmap=BitmapFactory.decodeStream(is, new Rect(), opts);

bitmap = BitmapFactory.decodeByteArray(data, 0, data.length,

opts);

}

} catch (Exception e) {

e.printStackTrace();

}

return bitmap;

}

/**

* 将InputStream读入到数组中

*

* @param input

* @return

*/

public static byte[] toByteArray(InputStream input) {

ByteArrayOutputStream output = new ByteArrayOutputStream();

byte[] buffer = new byte[1024];

int n = 0;

try {

while (-1 != (n = input.read(buffer))) {

output.write(buffer, 0, n);

}

} catch (IOException e) {

e.printStackTrace();

}

return output.toByteArray();

}

还有一个缩放图片的方法,单没有考虑到android内存溢出的问题,代码如下:

public static Drawable scaleBitmap(Bitmap bitmapOrg, int setwh, int setht) {

// / 加载需要操作的图片,这里是eoeAndroid的logo图片

// Bitmap bitmapOrg = BitmapFactory.decodeResource(getResources(), R.drawable.eoe_android);

// 获取这个图片的宽和高

int width = bitmapOrg.getWidth();

int height = bitmapOrg.getHeight();

// 定义预转换成的图片的宽度和高度

int newWidth = setwh;

int newHeight = setht;

// 计算缩放率,新尺寸除原始尺寸

float scaleWidth = ((float) newWidth) / width;

float scaleHeight = ((float) newHeight) / height;

// 创建操作图片用的matrix对象

Matrix matrix = new Matrix();

// 缩放图片动作

matrix.postScale(scaleWidth, scaleHeight);

// 旋转图片 动作

// matrix.postRotate(45);

// 创建新的图片

Bitmap resizedBitmap = Bitmap.createBitmap(bitmapOrg, 0, 0, width,

height, matrix, true);

// 将上面创建的Bitmap转换成Drawable对象,使得其可以使用在ImageView, ImageButton中

BitmapDrawable bmd = new BitmapDrawable(resizedBitmap);

return bmd;

}

这其中要特别注意的是decodeinputstream方法,它是直接调用的c,所以不消耗android内存,所以不会出现内存溢出的现象。大体都是这个思路,可根据自己的需求来实现自己的代码
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: