ThumbnailUtils.extractThumbnail Android图片缩略图显示总结及比较

博客源址Android图片缩放总结及比较

博客时间2011-08-01 17:42

在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.OptionsMatrix等经过人家一阵加工而成。
效率好像比第二种方法高一点点。

下面是我的例子:

[html]   view plain copy
  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>  

[java]   view plain copy
  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. /**pic大时(2M+)直接OOM,后面还裁剪个屁啊*/
  40.         Bitmap bitmap = BitmapFactory.decodeResource(getResources(),     
  41.                 R.drawable.pic);  
  42.           
  43.         start=System.currentTimeMillis();  
  44.           
  45. //        imView.setImageDrawable(resizeImage(bitmap, 300, 100));   
  46.           
  47.         imView2.setImageDrawable(resizeImage2("/sdcard/2.jpeg"200100));   
  48.           
  49.         stop=System.currentTimeMillis();  
  50.           
  51.         String theTime= String.format("\n1 iterative: (%d msec)",    
  52.                 stop - start);    
  53.           
  54.         start=System.currentTimeMillis();  
  55.         imView.setImageBitmap(ThumbnailUtils.extractThumbnail(bitmap,200,100));//2.2才加进来的新类,简单易用  
  56. //        imView.setImageDrawable(resizeImage(bitmap, 30, 30));   
  57.         stop=System.currentTimeMillis();  
  58.           
  59.          theTime+= String.format("\n2 iterative: (%d msec)",    
  60.                 stop - start);   
  61.           
  62.         text.setText(theTime);  
  63.     }  
  64.       
  65.     //使用Bitmap加Matrix来缩放  
  66.     public static Drawable resizeImage(Bitmap bitmap, int w, int h)   
  67.     {    
  68.         Bitmap BitmapOrg = bitmap;    
  69.         int width = BitmapOrg.getWidth();    
  70.         int height = BitmapOrg.getHeight();    
  71.         int newWidth = w;    
  72.         int newHeight = h;    
  73.   
  74.         float scaleWidth = ((float) newWidth) / width;    
  75.         float scaleHeight = ((float) newHeight) / height;    
  76.   
  77.         Matrix matrix = new Matrix();    
  78.         matrix.postScale(scaleWidth, scaleHeight);    
  79.         // if you want to rotate the Bitmap     
  80.         // matrix.postRotate(45);     
  81.         Bitmap resizedBitmap = Bitmap.createBitmap(BitmapOrg, 00, width,    
  82.                         height, matrix, true);    
  83.         return new BitmapDrawable(resizedBitmap);    
  84.     }  
  85.       
  86.     //使用BitmapFactory.Options的inSampleSize参数来缩放  
  87.     public static Drawable resizeImage2(String path,  
  88.             int width,int height)   
  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值