Android异步加载图片详解之方式一(3)

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为null
			Bitmap rawBitmap = BitmapFactory.decodeFile(filePath, options);
			if (rawBitmap==null) {
				System.out.println("此时rawBitmap为null");
			}

			//inSampleSize表示缩略图大小为原始图片大小的几分之一,若该值为3
			//则取出的缩略图的宽和高都是原始图片的1/3,图片大小就为原始大小的1/9
			//计算sampleSize
			int sampleSize=computeSampleSize(options, minSideLength, maxNumOfPixels);
			//为了读到图片,必须把options.inJustDecodeBounds设回false
			options.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;
		}
	}
}

 

时间: 2024-08-28 15:32:46

Android异步加载图片详解之方式一(3)的相关文章

Android异步加载图片详解之方式二(2)

FileCache.java如下: package com.cn.loadImages; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import android.content.Conte

Android异步加载图片详解之方式一(1)

MainActivity.java如下: package cn.ideallistview; import java.util.ArrayList; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.

Android异步加载图片详解之方式一(2)

FileCache.java如下: package cn.loadImages; import java.io.File; import android.content.Context; import android.net.Uri; import android.os.Environment; public class FileCache { private File fileCacheDir; public FileCache(Context context){ if (Environmen

Android异步加载图片详解之方式一(4)

main.xml如下: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" androi

Android异步加载图片详解之方式二(3)

main.xml如下: <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" >

Android LayoutInflater加载布局详解及实例代码

Android  LayoutInflater加载布局详解 对于有一定Android开发经验的同学来说,一定使用过LayoutInflater.inflater()来加载布局文件,但并不一定去深究过它的原理,比如 1.LayoutInflater为什么可以加载layout文件? 2.加载layout文件之后,又是怎么变成供我们使用的View的? 3.我们定义View的时候,如果需要在布局中使用,则必须实现带AttributeSet参数的构造方法,这又是为什么呢? 既然在这篇文章提出来,那说明这三

Android 异步加载图片,使用LruCache和SD卡或手机缓存,效果非常的流畅

异步加载图片的例子,网上也比较多,大部分用了HashMap<String, SoftReference<Drawable>> imageCache ,但是现在已经不再推荐使用这种方式了,因为从 Android 2.3 (API Level 9)开始,垃圾回收器会更倾向于回收持有软引用或弱引用的对象,这让软引用和弱引用变得不再可靠.另外,Android 3.0 (API Level 11)中,图片的数据会存储在本地的内存当中,因而无法用一种可预见的方式将其释放,这就有潜在的风险造成应

我的Android进阶之旅------&amp;gt;android异步加载图片显示,并且对图片进行缓存实例

先来看看效果示意图: step1:新建项目DataAsyncLoad,如下图所示 step2:设置应用的UI界面 a.应用的主界面    main.xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="

实例演示Android异步加载图片(转)

本文给大家演示异步加载图片的分析过程.让大家了解异步加载图片的好处,以及如何更新UI.首先给出main.xml布局文件:简单来说就是 LinearLayout 布局,其下放了2个TextView和5个ImageView. 1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout 3 xmlns:android="http://schemas.android.com/apk/res/a