图片比例缩放以及bitMap转BitmapDrawable

来源:互联网 发布:spark连接巨杉数据库 编辑:程序博客网 时间:2024/05/22 03:07


  1. import android.app.Activity;  
  2. import android.graphics.Bitmap;  
  3. import android.graphics.BitmapFactory;  
  4. import android.graphics.Matrix;  
  5. import android.graphics.drawable.BitmapDrawable;  
  6. import android.os.Bundle;  
  7. import android.view.ViewGroup.LayoutParams;  
  8. import android.widget.ImageView;  
  9. import android.widget.LinearLayout;  
  10. import android.widget.ImageView.ScaleType;  
  11.   
  12. /** 
  13.  * This example shows how to resize an image 
  14.  * @author FaYnaSoft Labs 
  15.  * 
  16.  */  
  17. public class Main extends Activity {  
  18.   
  19.     @Override  
  20.     public void onCreate(Bundle icicle) {  
  21.         super.onCreate(icicle);  
  22.         LinearLayout linearLayout = new LinearLayout(this);  
  23.   
  24.         Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.icon);  
  25.   
  26.         int width = bitmap.getWidth();  
  27.         int height = bitmap.getHeight();  
  28.         int newWidth = 640;  
  29.         int newHeight = 480;  
  30.   
  31.         float scaleWidth = ((float) newWidth) / width;  
  32.         float scaleHeight = ((float) newHeight) / height;  
  33.   
  34.         Matrix matrix = new Matrix();  
  35.         matrix.postScale(scaleWidth, scaleHeight);  
  36.   
  37.         // create the new Bitmap object  
  38.         Bitmap resizedBitmap = Bitmap.createBitmap(bitmap, 00, width,  
  39.                 height, matrix, true);  
  40.         BitmapDrawable bmd = new BitmapDrawable(resizedBitmap);  
  41.   
  42.         ImageView imageView = new ImageView(this);  
  43.         imageView.setImageDrawable(bmd);  
  44.         imageView.setScaleType(ScaleType.CENTER);  
  45.   
  46.         linearLayout.addView(imageView, new LinearLayout.LayoutParams(  
  47.                 LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));  
  48.         setContentView(linearLayout);  
  49.     }  
  50. }  

 转自:http://wang-peng1.iteye.com/blog/669532

原创粉丝点击