android 裁剪图片大小 控制图片尺寸 .

用BitmapFactory获取适合屏幕大小的图片 和自带的图片裁剪工具

[java] view plain copy print ?
  1.   
[java] view plain copy print ?
  1.   
[java] view plain copy print ?
  1.   
[java] view plain copy print ?
  1. package com.lin.image;  
  2.   
  3. import android.app.Activity;  
  4. import android.content.Intent;  
  5. import android.graphics.Bitmap;  
  6. import android.graphics.BitmapFactory;  
  7. import android.net.Uri;  
  8. import android.os.Bundle;  
  9. import android.view.View;  
  10. import android.widget.Button;  
  11. import android.widget.ImageView;  
  12.   
  13. public class ImageScaleActivity extends Activity  implements View.OnClickListener{  
  14.     /** Called when the activity is first created. */  
  15.     private Button selectImageBtn;  
  16.     private Button cutImageBtn;  
  17.     private ImageView imageView;  
  18.     private static final int  IMAGE_SELECT=1;  
  19.     private static final int  IMAGE_CUT=2;  
  20.     @Override  
  21.     public void onCreate(Bundle savedInstanceState) {  
  22.         super.onCreate(savedInstanceState);  
  23.         setContentView(R.layout.main);  
  24.           
  25.         selectImageBtn=(Button)findViewById(R.id.selectImageBtn);  
  26.         cutImageBtn=(Button)findViewById(R.id.catImageBtn);  
  27.         imageView=(ImageView)findViewById(R.id.imageView);  
  28.           
  29.         cutImageBtn.setOnClickListener(this);  
  30.         selectImageBtn.setOnClickListener(this);  
  31.         
  32.     }  
  33.   
  34.     @Override  
  35.     public void onClick(View v) {  
[java] view plain copy print ?
  1. <SPAN style="WHITE-SPACE: pre">     </SPAN>//截取适合屏幕大小的图片   
  2.         if(v==selectImageBtn){  
  3.         Intent intent=new Intent(Intent.ACTION_PICK,  
  4.                 android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);  
  5.             startActivityForResult(intent, IMAGE_SELECT);  
  6.         }//一般用于头像等需要设置指定大小   
  7.         else if(v==cutImageBtn){  
  8.             Intent intent=getImageClipIntent();  
  9.             startActivityForResult(intent, IMAGE_CUT);  
  10.         }  
  11.     }  
  12.   
  13.     @Override  
  14.     protected void onActivityResult(int requestCode, int resultCode, Intent intent) {  
  15.         // TODO Auto-generated method stub   
  16.         if(resultCode==RESULT_OK){  
  17.             if(requestCode==IMAGE_SELECT){  
  18.             Uri imageFileUri =intent.getData();  
  19.             int dw=getWindowManager().getDefaultDisplay().getWidth();  
  20.             int dh=getWindowManager().getDefaultDisplay().getHeight()/2;  
  21.             //已屏幕宽 和一般的高作为图片显示的最大尺寸   
  22.             try{  
  23.                 BitmapFactory.Options factory=new BitmapFactory.Options();  
  24.                 factory.inJustDecodeBounds=true//当为true时  允许查询图片不为 图片像素分配内存   
  25.                 Bitmap bmp=BitmapFactory.decodeStream(getContentResolver()  
  26.                         .openInputStream(imageFileUri),null,factory);  
  27.                 int hRatio=(int)Math.ceil(factory.outHeight/(float)dh); //图片是高度的几倍   
  28.                 int wRatio=(int)Math.ceil(factory.outWidth/(float)dw); //图片是宽度的几倍   
  29.                 System.out.println("hRatio:"+hRatio+"  wRatio:"+wRatio);  
  30.                 //缩小到  1/ratio的尺寸和 1/ratio^2的像素   
  31.                 if(hRatio>1||wRatio>1){  
  32.                     if(hRatio>wRatio){  
  33.                         factory.inSampleSize=hRatio;   
  34.                     }  
  35.                     else  
  36.                         factory.inSampleSize=wRatio;  
  37.                 }  
  38.                 factory.inJustDecodeBounds=false;  
  39.                  bmp=BitmapFactory.decodeStream(getContentResolver()  
  40.                         .openInputStream(imageFileUri),null,factory);  
  41.                  imageView.setImageBitmap(bmp);  
  42.             }catch(Exception ex){  
  43.                   
  44.             }  
  45.             }  
  46.             else if(requestCode==IMAGE_CUT){  
  47.                 Bitmap bmp=intent.getParcelableExtra("data");  
  48.                 imageView.setImageBitmap(bmp);  
  49.             }  
  50.         }  
  51.           
  52.     }  
  53.         /** 
  54.          * 获取剪切后的图片 
  55.          */  
  56.         public static Intent getImageClipIntent() {  
  57.   
  58.             Intent intent = new Intent(Intent.ACTION_GET_CONTENT, null);  
  59.             intent.setType("image/*");  
  60.             intent.putExtra("crop""true");   
  61.             intent.putExtra("aspectX"1);//裁剪框比例   
  62.             intent.putExtra("aspectY"1);  
  63.             intent.putExtra("outputX"80);//输出图片大小   
  64.             intent.putExtra("outputY"80);  
  65.             intent.putExtra("return-data"true);  
  66.             return intent;  
  67.         }  
  68.       
  69.       
  70. }  
android 裁剪图片大小 控制图片尺寸 ._第1张图片  

你可能感兴趣的:(android 裁剪图片大小 控制图片尺寸 .)