Android非同步載入圖片詳解之方式一(3)

來源:互聯網
上載者:User

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;}}}

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.