Android图片缩放总结及比较


在Android中对大图片进行缩放真的很不尽如人意,不知道是不是我的方法不对。下面我列出3种对图片缩放的方法,并给出相应速度。请高人指教。

第一种是BitmapFactory和BitmapFactory.Options。
首先,BitmapFactory.Options有几个Fields很有用:
inJustDecodeBounds:If set to true, the decoder will return null (no bitmap), but the out...
也就是说,当inJustDecodeBounds设成true时,bitmap并不加载到内存,这样效率很高哦。而这时,你可以获得bitmap的高、宽等信息。
outHeight:The resulting height of the bitmap, set independent of the state of inJustDecodeBounds.
outWidth:The resulting width of the bitmap, set independent of the state of inJustDecodeBounds. 
看到了吧,上面3个变量是相关联的哦。
inSampleSize : If set to a value > 1, requests the decoder to subsample the original image, returning a smaller image to save memory.
这就是用来做缩放比的。这里有个技巧:
inSampleSize=(outHeight/Height+outWidth/Width)/2
实践证明,这样缩放出来的图片还是很好的。
最后用BitmapFactory.decodeFile(path, options)生成。
由于只是对bitmap加载到内存一次,所以效率比较高。解析速度快。

第二种是使用Bitmap加Matrix来缩放。

首先要获得原bitmap,再从原bitmap的基础上生成新图片。这样效率很低。

第三种是用2.2新加的类ThumbnailUtils来做。
让我们新看看这个类,从API中来看,此类就三个静态方法:createVideoThumbnail、extractThumbnail(Bitmap source, int width, int height, int options)、extractThumbnail(Bitmap source, int width, int height)。
我这里使用了第三个方法。再看看它的源码,下面会附上。是上面我们用到的BitmapFactory.Options和Matrix等经过人家一阵加工而成。
效率好像比第二种方法高一点点。

下面是我的例子:

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:orientation="vertical"  
  4.     android:layout_width="fill_parent"  
  5.     android:layout_height="fill_parent"  
  6.     >  
  7.       
  8. <ImageView  
  9.     android:id="@+id/imageShow"  
  10.     android:layout_width="wrap_content"   
  11.     android:layout_height="wrap_content"   
  12. />     
  13. <ImageView  
  14.     android:id="@+id/image2"  
  15.     android:layout_width="wrap_content"   
  16.     android:layout_height="wrap_content"   
  17. />    
  18. <TextView    
  19.     android:id="@+id/text"  
  20.     android:layout_width="fill_parent"   
  21.     android:layout_height="wrap_content"   
  22.     android:text="@string/hello"  
  23.     />  
  24. </LinearLayout>  

 
  1. package com.linc.ResolvePicture;  
  2.   
  3. import java.io.File;  
  4. import java.io.FileNotFoundException;  
  5. import java.io.FileOutputStream;  
  6. import java.io.IOException;  
  7.   
  8. import android.app.Activity;  
  9. import android.graphics.Bitmap;  
  10. import android.graphics.BitmapFactory;  
  11. import android.graphics.Matrix;  
  12. import android.graphics.drawable.BitmapDrawable;  
  13. import android.graphics.drawable.Drawable;  
  14. import android.media.ThumbnailUtils;  
  15. import android.os.Bundle;  
  16. import android.util.Log;  
  17. import android.widget.ImageView;  
  18. import android.widget.TextView;  
  19.   
  20. public class ResolvePicture extends Activity {  
  21.     private static String tag="ResolvePicture";  
  22.     Drawable bmImg;    
  23.     ImageView imView;   
  24.     ImageView imView2;   
  25.     TextView text;  
  26.     String theTime;  
  27.     long start, stop;   
  28.     /** Called when the activity is first created. */  
  29.     @Override  
  30.     public void onCreate(Bundle savedInstanceState) {  
  31.         super.onCreate(savedInstanceState);  
  32.         setContentView(R.layout.main);  
  33.           
  34.         text=(TextView)findViewById(R.id.text);  
  35.           
  36.         imView=(ImageView) findViewById(R.id.imageShow);  
  37.         imView2=(ImageView) findViewById(R.id.image2);  
  38.           
  39.         Bitmap bitmap = BitmapFactory.decodeResource(getResources(),     
  40.                 R.drawable.pic);  
  41.           
  42.         start=System.currentTimeMillis();  
  43.           
  44. //        imView.setImageDrawable(resizeImage(bitmap, 300, 100));    
  45.           
  46.         imView2.setImageDrawable(resizeImage2("/sdcard/2.jpeg"200100));   
  47.           
  48.         stop=System.currentTimeMillis();  
  49.           
  50.         String theTime= String.format("\n1 iterative: (%d msec)",    
  51.                 stop - start);    
  52.           
  53.         start=System.currentTimeMillis();  
  54.         imView.setImageBitmap(ThumbnailUtils.extractThumbnail(bitmap,200,100));//2.2才加进来的新类,简单易用   
  55. //        imView.setImageDrawable(resizeImage(bitmap, 30, 30));    
  56.         stop=System.currentTimeMillis();  
  57.           
  58.          theTime+= String.format("\n2 iterative: (%d msec)",    
  59.                 stop - start);   
  60.           
  61.         text.setText(theTime);  
  62.     }  
  63.       
  64.     //使用Bitmap加Matrix来缩放   
  65.     public static Drawable resizeImage(Bitmap bitmap, int w, int h)   
  66.     {    
  67.         Bitmap BitmapOrg = bitmap;    
  68.         int width = BitmapOrg.getWidth();    
  69.         int height = BitmapOrg.getHeight();    
  70.         int newWidth = w;    
  71.         int newHeight = h;    
  72.   
  73.         float scaleWidth = ((float) newWidth) / width;    
  74.         float scaleHeight = ((float) newHeight) / height;    
  75.   
  76.         Matrix matrix = new Matrix();    
  77.         matrix.postScale(scaleWidth, scaleHeight);    
  78.         // if you want to rotate the Bitmap      
  79.         // matrix.postRotate(45);      
  80.         Bitmap resizedBitmap = Bitmap.createBitmap(BitmapOrg, 00, width,    
  81.                         height, matrix, true);    
  82.         return new BitmapDrawable(resizedBitmap);    
  83.     }  
  84.       
  85.     //使用BitmapFactory.Options的inSampleSize参数来缩放   
  86.     public static Drawable resizeImage2(String path,  
  87.             int width,int height)   
  88.     {  
  89.         BitmapFactory.Options options = new BitmapFactory.Options();  
  90.         options.inJustDecodeBounds = true;//不加载bitmap到内存中   
  91.         BitmapFactory.decodeFile(path,options);   
  92.         int outWidth = options.outWidth;  
  93.         int outHeight = options.outHeight;  
  94.         options.inDither = false;  
  95.         options.inPreferredConfig = Bitmap.Config.ARGB_8888;  
  96.         options.inSampleSize = 1;  
  97.           
  98.         if (outWidth != 0 && outHeight != 0 && width != 0 && height != 0)   
  99.         {  
  100.             int sampleSize=(outWidth/width+outHeight/height)/2;  
  101.             Log.d(tag, "sampleSize = " + sampleSize);  
  102.             options.inSampleSize = sampleSize;  
  103.         }  
  104.       
  105.         options.inJustDecodeBounds = false;  
  106.         return new BitmapDrawable(BitmapFactory.decodeFile(path, options));       
  107.     }  
  108.   
  109.     //图片保存   
  110.     private void saveThePicture(Bitmap bitmap)  
  111.     {  
  112.         File file=new File("/sdcard/2.jpeg");  
  113.         try  
  114.         {  
  115.             FileOutputStream fos=new FileOutputStream(file);  
  116.             if(bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos))  
  117.             {  
  118.                 fos.flush();  
  119.                 fos.close();  
  120.             }  
  121.         }  
  122.         catch(FileNotFoundException e1)  
  123.         {  
  124.             e1.printStackTrace();  
  125.         }  
  126.         catch(IOException e2)  
  127.         {  
  128.             e2.printStackTrace();  
  129.         }  
  130.     }  
  131. }  
  • 1
  • 2
  • 下一页

相关内容