android基础学习之android加载大图片
来源:互联网 发布:wifi登陆器源码 编辑:程序博客网 时间:2024/06/04 19:13
在网上查看了很多关于这些的资料,发现大体都差不多,自己根据自己的理解来使用,觉得最关键的就是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内存,所以不会出现内存溢出的现象。大体都是这个思路,可根据自己的需求来实现自己的代码
- android基础学习之android加载大图片
- Android基础之加载大图片
- Android 加载大图片
- android 加载大图片
- android加载大图片
- Android-加载大图片
- android加载大图片
- android 加载大图片
- android加载大图片
- Android 加载大图片
- Android 加载大图片
- Android 多媒体开发学习之加载大图片
- Android自助餐之大图片加载
- android缩放大图片加载
- Android 高效加载大图片
- Android 高效加载大图片
- android 加载大图片处理
- Android 高效加载大图片
- Linux加载DTS设备节点的过程(以高通8974平台为例)
- MYSQL快速备份工具xtrabackup测试(I)
- spring学习之Spring的事务配置配置篇
- 让页面加载的时候自动获得当前时间加载到DateBox中
- URL中的+,空格,/,?,%,#,&,= 及转义处理
- android基础学习之android加载大图片
- 浅析 Cordova for iOS
- cocos 触摸机制
- UVA All in All(水题)
- 基于james3.0 的邮件系统(struts2.3.2 +spring3.0.1+jpa(hibernate3.6.5)实现)b/s模式--java邮件系统
- TCHAR 转换 string
- regmap使用介绍
- WPF--TextBox样式使用(文字居中、背景、圆角...)
- 凝聚 • 融合 • 协作——记webpower2015新春年会