Android异步加载图片详解之方式一(3)
来源:互联网 发布:svs软件 编辑:程序博客网 时间:2024/06/09 15:29
Utils.java如下:
package cn.loadImages;import java.io.InputStream;import java.io.OutputStream;import android.graphics.Bitmap;import android.graphics.BitmapFactory;public class Utils { public static void copyStream(InputStream is, OutputStream os){ final int buffer_size=1024; try{ byte[] bytes=new byte[buffer_size]; for(;;) { int count=is.read(bytes, 0, buffer_size); if(count==-1){ break; } os.write(bytes, 0, count); } } catch(Exception ex){} } //获取图片的缩略图public static Bitmap getBitmapThumbnail(String filePath,int minSideLength, int maxNumOfPixels ){ BitmapFactory.Options options=new BitmapFactory.Options(); //true那么将不返回实际的bitmap对象 //不给其分配内存空间但是可以得到一些解码边界信息即图片大小等信息 options.inJustDecodeBounds=true; //此时rawBitmap为nullBitmap rawBitmap = BitmapFactory.decodeFile(filePath, options);if (rawBitmap==null) {System.out.println("此时rawBitmap为null");}//inSampleSize表示缩略图大小为原始图片大小的几分之一,若该值为3//则取出的缩略图的宽和高都是原始图片的1/3,图片大小就为原始大小的1/9//计算sampleSizeint sampleSize=computeSampleSize(options, minSideLength, maxNumOfPixels);//为了读到图片,必须把options.inJustDecodeBounds设回falseoptions.inJustDecodeBounds = false;options.inSampleSize = sampleSize;//原图大小为625x690 90.2kB //测试调用computeSampleSize(options, 100, 200*100);//得到sampleSize=8//得到宽和高位79和87//79*8=632 87*8=696 Bitmap thumbnailBitmap=BitmapFactory.decodeFile(filePath, options);return thumbnailBitmap;}//参考资料://http://my.csdn.net/zljk000/code/detail/18212//第一个参数:原本Bitmap的options//第二个参数:希望生成的缩略图的宽高中的较小的值//第三个参数:希望生成的缩量图的总像素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;}}}
- Android异步加载图片详解之方式一(3)
- Android异步加载图片详解之方式一(4)
- Android异步加载图片详解之方式一(2)
- Android异步加载图片详解之方式一(1)
- Android异步加载图片详解之方式二(3)
- Android异步加载图片详解之方式二(2)
- Android异步加载图片详解之方式二(1)
- android之图片异步加载
- Android图片异步加载之Universal-Image-Loader(一)
- android多种方式实现异步加载图片
- android开发-异步图片加载(一)
- Android之handle异步加载图片
- android 之图片异步加载,带缓存。
- Android开发之ListView异步加载图片
- Android之异步加载网络图片
- Android异步图片加载组件之 Universal_image_loader
- Android有效加载图片 之 使用AsyncTask异步加载图片
- android异步加载之Handler、AsyncTask(一)
- tiny6410 适用于win7 64bit的dnw 的USB下载驱动
- Windows线程的同步与互斥
- Android 文件的浏览(类似于FileDialog的功能)
- ExtJs技术应用之PropertyGrid使用ComboBox选择值
- 基于Qt的OpenGL视图矩阵的计算
- Android异步加载图片详解之方式一(3)
- 欧拉计划:相似的18题和67题,以及简单的20题100!,以及19题
- NSDATA如何转成NSString
- sprintf(c++)
- 如何培养软件工程人才——参加“第一届高等学校软件工程人才培养高峰论坛”有感
- Android异步加载图片详解之方式一(2)
- 触发器中New与Old的区别
- C#数字千分位问题
- 使用Xcode和Instruments调试解决iOS内存泄露