android 裁剪圖片大小 控製圖片尺寸

來源:互聯網
上載者:User

標籤:des   android   style   blog   http   color   io   os   ar   

用BitmapFactory擷取適合螢幕大小的圖片 和內建的圖片裁剪工具

package com.lin.image;  import android.app.Activity; import android.content.Intent; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.net.Uri; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.ImageView;  public class ImageScaleActivity extends Activity  implements View.OnClickListener{     /** Called when the activity is first created. */     private Button selectImageBtn;     private Button cutImageBtn;     private ImageView imageView;     private static final int  IMAGE_SELECT=1;     private static final int  IMAGE_CUT=2;     @Override     public void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(R.layout.main);                  selectImageBtn=(Button)findViewById(R.id.selectImageBtn);         cutImageBtn=(Button)findViewById(R.id.catImageBtn);         imageView=(ImageView)findViewById(R.id.imageView);                  cutImageBtn.setOnClickListener(this);         selectImageBtn.setOnClickListener(this);            }      @Override     public void onClick(View v) { <span style="white-space:pre">        </span>//截取適合螢幕大小的圖片         if(v==selectImageBtn){         Intent intent=new Intent(Intent.ACTION_PICK,                 android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);             startActivityForResult(intent, IMAGE_SELECT);         }//一般用於頭像等需要設定指定大小         else if(v==cutImageBtn){             Intent intent=getImageClipIntent();             startActivityForResult(intent, IMAGE_CUT);         }     }      @Override     protected void onActivityResult(int requestCode, int resultCode, Intent intent) {         // TODO Auto-generated method stub         if(resultCode==RESULT_OK){             if(requestCode==IMAGE_SELECT){             Uri imageFileUri =intent.getData();             int dw=getWindowManager().getDefaultDisplay().getWidth();             int dh=getWindowManager().getDefaultDisplay().getHeight()/2;             //已螢幕寬 和一般的高作為圖片顯示的最大尺寸             try{                 BitmapFactory.Options factory=new BitmapFactory.Options();                 factory.inJustDecodeBounds=true; //當為true時  允許查詢圖片不為 圖片像素分配記憶體                 Bitmap bmp=BitmapFactory.decodeStream(getContentResolver()                         .openInputStream(imageFileUri),null,factory);                 int hRatio=(int)Math.ceil(factory.outHeight/(float)dh); //圖片是高度的幾倍                 int wRatio=(int)Math.ceil(factory.outWidth/(float)dw); //圖片是寬度的幾倍                 System.out.println("hRatio:"+hRatio+"  wRatio:"+wRatio);                 //縮小到  1/ratio的尺寸和 1/ratio^2的像素                 if(hRatio>1||wRatio>1){                     if(hRatio>wRatio){                         factory.inSampleSize=hRatio;                      }                     else                         factory.inSampleSize=wRatio;                 }                 factory.inJustDecodeBounds=false;                  bmp=BitmapFactory.decodeStream(getContentResolver()                         .openInputStream(imageFileUri),null,factory);                  imageView.setImageBitmap(bmp);             }catch(Exception ex){                              }             }             else if(requestCode==IMAGE_CUT){                 Bitmap bmp=intent.getParcelableExtra("data");                 imageView.setImageBitmap(bmp);             }         }              }         /**         * 擷取剪下後的圖片         */         public static Intent getImageClipIntent() {              Intent intent = new Intent(Intent.ACTION_GET_CONTENT, null);             intent.setType("image/*");             intent.putExtra("crop", "true");              intent.putExtra("aspectX", 1);//裁剪框比例             intent.putExtra("aspectY", 1);             intent.putExtra("outputX", 80);//輸出圖片大小             intent.putExtra("outputY", 80);             intent.putExtra("return-data", true);             return intent;         }           }

 

摘自 dikeboy1234的專欄

android 裁剪圖片大小 控製圖片尺寸

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.