网络图片转bitmap,并设置相应参数
来源:互联网 发布:米芾字体下载 mac 编辑:程序博客网 时间:2024/06/06 01:49
参考文章:http://blog.csdn.net/appandroid/article/details/11213973
通常我们项目中用到网络图片达不到我们的既定要求时,我们想到的是将图片转成我们可以操作的对象进行处理,
那么这个流程是怎样的呢?
例如 我们想获取一个网络图片的宽和高,那么就需要
先将网络图片利用BitmapFactory.decodeFile(Sting file)方法转转成BitMap对象,然后针对BitMap对象进行相应处理,已达到我们的要求.
但是有的图片过大,需要对图片进行压缩或者美化,那么这时候就需要用到BitmapFactory.options这个类:
首先需要获取这个网络图片的宽和高,这样才能知道怎样去压缩图片才不会变形
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
Bitmap bmp = BitmapFactory.decodeFile(path, options);
/* 这里返回的bmp是null */
这段代码之后,options.outWidth和 options.outHeight就是我们想要的宽和高了。
有了宽,高的信息,我们怎样在图片不变形的情况下获取到图片指定大小的缩略图呢?
比如我们需要在图片不变形的前提下得到宽度为200的缩略图。
那么我们需要先计算一下缩放之后,图片的高度是多少
/* 计算得到图片的高度 */
/* 这里需要主意,如果你需要更高的精度来保证图片不变形的话,需要自己进行一下数学运算 */
int height = options.outHeight * 200 / options.outWidth;
options.outWidth = 200;
options.outHeight = height;
/* 这样才能真正的返回一个Bitmap给你 */
options.inJustDecodeBounds = false;
Bitmap bmp = BitmapFactory.decodeFile(path, options);
image.setImageBitmap(bmp);
这样虽然我们可以得到我们期望大小的ImageView
但是在执行BitmapFactory.decodeFile(path, options);时,并没有节约内存。要想节约内存,还需要用到BitmapFactory.Options这个类里的inSampleSize这个成员变量。
我们可以根据图片实际的宽高和我们期望的宽高来计算得到这个值。
inSampleSize = options.outWidth / 200;
另外,为了节约内存我们还可以使用下面的几个字段:
options.inPreferredConfig = Bitmap.Config.ARGB_4444; //设置该BitMap的位数值,控制内存大小
// 默认是Bitmap.Config.ARGB_8888
/* 下面两个字段需要组合使用 */
options.inPurgeable = true;
options.inInputShareable = true;
上代码(参考文章中黏贴的)
1、设置缩放大小对图片作处理
public Bitmap getBitmapFromFile(File dst, int width, int height) {
if (null != dst && dst.exists()) {
BitmapFactory.Options opts = null;
if (width > 0 && height > 0) {
opts = new BitmapFactory.Options();
opts.inJustDecodeBounds = true;
BitmapFactory.decodeFile(dst.getPath(), opts);
// 计算图片缩放比例
final int minSideLength = Math.min(width, height);
opts.inSampleSize = computeSampleSize(opts, minSideLength,
width * height);
opts.inJustDecodeBounds = false;
opts.inInputShareable = true;
opts.inPurgeable = true;
}
try {
return BitmapFactory.decodeFile(dst.getPath(), opts);
} catch (OutOfMemoryError e) {
e.printStackTrace();
}
}
return null;
}
public static int computeSampleSize(BitmapFactory.Options options,
int minSideLength, int maxNumOfPixels) {
int initialSize = computeInitialSampleSize(options, minSideLength,
maxNumOfPixels);
int roundedSize;
if (initialSize <= 8) {
roundedSize = 1;
while (roundedSize < initialSize) {
roundedSize <<= 1;
}
} else {
roundedSize = (initialSize + 7) / 8 * 8;
}
return roundedSize;
}
private static int computeInitialSampleSize(BitmapFactory.Options options,
int minSideLength, int maxNumOfPixels) {
double w = options.outWidth;
double h = options.outHeight;
int lowerBound = (maxNumOfPixels == -1) ? 1 : (int) Math.ceil(Math
.sqrt(w * h / maxNumOfPixels));
int upperBound = (minSideLength == -1) ? 128 : (int) Math.min(Math
.floor(w / minSideLength), Math.floor(h / minSideLength));
if (upperBound < lowerBound) {
// return the larger one when there is no overlapping zone.
return lowerBound;
}
if ((maxNumOfPixels == -1) && (minSideLength == -1)) {
return 1;
} else if (minSideLength == -1) {
return lowerBound;
} else {
return upperBound;
}
}
工具类:
- public class BitmapUtil {
- 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 heightRatio = Math.round((float) height / (float) reqHeight);
- final int widthRatio = Math.round((float) width / (float) reqWidth);
- // 选择宽和高中最小的比率作为inSampleSize的值,这样可以保证最终图片的宽和高 // 一定都会大于等于目标的宽和高。
- inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;
- }
- return inSampleSize;
- }
- public Bitmap decodeSampleFromSD(String path,int sdwidth,int sdheight){
- BitmapFactory.Options options=new BitmapFactory.Options();
- options.inJustDecodeBounds=true;
- BitmapFactory.decodeFile(path, options);
- options.inSampleSize=calculateInSampleSize(options, sdwidth, sdheight);
- options.inJustDecodeBounds=false;
- options.inDither=false;
- options.inPreferredConfig=Bitmap.Config.ARGB_8888;
- return BitmapFactory.decodeFile(path, options);
- }
- }
- 网络图片转bitmap,并设置相应参数
- 网络图片资源设置转换为Bitmap
- 取得并设置网络参数
- 传递Bitmap + 图片压缩处理 并保存 + 壁纸设置 总结
- 传递Bitmap + 图片压缩处理 并保存 + 壁纸设置 总结
- Android:网络图片Url 转 Bitmap
- bitmap 获取网络图片
- 网络图片url->bitmap
- BitMap设置图片透明度
- android加载网络图片并缓存,对图片进行点击打开相应链接
- 从本地或者网络读取图片,并转换为Bitmap图片
- Android bitmap打开网络图片
- 异步获取网络图片Bitmap
- 网络图片转换成Bitmap
- 网络图片url转为bitmap
- Bitmap异步加载网络图片
- android-bitmap创建设置图片
- 把网络图片转换为bitmap并显示在ImageView中
- OSS是什么
- leetcode(M) three sum
- Mat的保存和读取
- 100天土鸡饲养计划(83)
- Java中Properties对象的使用
- 网络图片转bitmap,并设置相应参数
- Ugly Number系列解题报告
- 淘宝SKU组合查询算法实现
- iOS UIWebView 的下拉刷新和上拉加载的实现
- 数据挖掘在公共建筑能耗分析中的应用研究
- 关于下载的.chm文件,点击目录无内容显示,已解决
- 百度与谷歌技术实力比较
- 371. Sum of Two Integers
- Hibernate openSession() 和 getCurrentSession的区别