Android之UI學習篇七:ImageView實現適屏和裁剪圖片的功能

ImageView實現圖片適應屏幕大小顯示,和圖片裁剪的功能.

實現的效果

主界面:
 


 

適應屏幕:


 

裁剪圖片:


 


 

顯示裁剪圖片到ImagView:

 


 

源代碼:

MainActivity.java

 

  1. package com.imageview.activity;  
  2.   
  3. import java.io.FileNotFoundException;  
  4. import android.app.Activity;  
  5. import android.content.Intent;  
  6. import android.graphics.Bitmap;  
  7. import android.graphics.BitmapFactory;  
  8. import android.net.Uri;  
  9. import android.os.Bundle;  
  10. import android.util.Log;  
  11. import android.view.View;  
  12. import android.view.View.OnClickListener;  
  13. import android.widget.Button;  
  14. import android.widget.ImageView;  
  15. import android.widget.Toast;  
  16.   
  17. public class MainActivity extends Activity implements OnClickListener {  
  18.     private Button imageSelectBtn;  
  19.     private Button imageCutBtn;  
  20.     private ImageView imageView;  
  21.     // 聲明兩個靜態整型變量,用於意圖的返回標誌  
  22.     private static final int IMAGE_SELECT = 1; // 選擇圖片  
  23.     private static final int IMAGE_CUT = 2; // 裁剪圖片  
  24.   
  25.     @Override  
  26.     public void onCreate(Bundle savedInstanceState) {  
  27.         super.onCreate(savedInstanceState);  
  28.         setContentView(R.layout.main);  
  29.         setupViews();  
  30.     }  
  31.   
  32.     // 我的一貫作風呵呵  
  33.     public void setupViews() {  
  34.         imageSelectBtn = (Button) findViewById(R.id.imageSelectButton);  
  35.         imageSelectBtn.setOnClickListener(this);  
  36.         imageCutBtn = (Button) findViewById(R.id.imageCutButton);  
  37.         imageCutBtn.setOnClickListener(this);  
  38.         imageView = (ImageView) findViewById(R.id.imageView);  
  39.     }  
  40.   
  41.     @Override  
  42.     protected void onActivityResult(int requestCode, int resultCode, Intent data) {  
  43.         super.onActivityResult(requestCode, resultCode, data);  
  44.         if (resultCode == RESULT_OK) {  
  45.             // 處理圖片按照手機屏幕大小顯示  
  46.             if (requestCode == IMAGE_SELECT) {  
  47.                 // 獲得圖片的路徑  
  48.                 Uri uri = data.getData();   
  49.                 // 獲得屏幕寬度  
  50.                 int dw = getWindowManager().getDefaultDisplay().getWidth();   
  51.                 // 獲得屏幕寬度  
  52.                 int dh = getWindowManager().getDefaultDisplay().getHeight() / 2;   
  53.                 try {  
  54.                     // 實現對圖片裁剪的類,是一個匿名內部類  
  55.                     BitmapFactory.Options factory = new BitmapFactory.Options();  
  56.                     // 如果設置爲true,允許查詢圖片不是按照像素分配內存  
  57.                     factory.inJustDecodeBounds = true;  
  58.                     Bitmap bmp = BitmapFactory.decodeStream(  
  59.                             getContentResolver().openInputStream(uri), null,  
  60.                             factory);  
  61.                     // 對圖片的高度和寬度對應手機屏幕進行匹配  
  62.                     // 寬度之比  
  63.                     int wRatio = (int) Math.ceil(factory.outWidth / (float) dw);   
  64.                     // 高度之比  
  65.                     int hRatio = (int) Math.ceil(factory.outHeight / (float) dh);   
  66.                     // 如果wRatio大於1,表示圖片的寬度大於屏幕寬度,類似hRatio  
  67.                     if (wRatio > 1 || hRatio > 1) {  
  68.                         // inSampleSize>1則返回比原圖更小的圖片  
  69.                         if (hRatio > wRatio) {  
  70.                             factory.inSampleSize = hRatio;  
  71.                         } else {  
  72.                             factory.inSampleSize = wRatio;  
  73.                         }  
  74.                     }  
  75.                     // 該屬性爲false則允許調用者查詢圖片無需爲像素分配內存  
  76.                     factory.inJustDecodeBounds = false;  
  77.                     // 再次使用BitmapFactory對象圖像進行適屏操作  
  78.                     bmp = BitmapFactory.decodeStream(getContentResolver()  
  79.                             .openInputStream(uri), null, factory);  
  80.                     imageView.setImageBitmap(bmp);  
  81.                 } catch (FileNotFoundException e) {  
  82.                     e.printStackTrace();  
  83.                 }  
  84.             } else if (requestCode == IMAGE_CUT) { // 裁剪圖片  
  85.                 // 一定要和"return-data"返回的標籤"data"一致  
  86.                 Bitmap bmp = data.getParcelableExtra("data");   
  87.                 imageView.setImageBitmap(bmp);  
  88.             }  
  89.         }  
  90.     }  
  91.   
  92.     @Override  
  93.     public void onClick(View v) {  
  94.         switch (v.getId()) {  
  95.         case R.id.imageSelectButton:  
  96.             // 如何提取手機的圖片,並且進行圖片的選擇  
  97.             Intent intent = new Intent(  
  98.                     Intent.ACTION_PICK,  
  99.                     android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);  
  100.             startActivityForResult(intent, IMAGE_SELECT);  
  101.             break;  
  102.         case R.id.imageCutButton:  
  103.             Intent intent2 = getImageClipIntent();  
  104.             startActivityForResult(intent2, IMAGE_CUT);  
  105.             break;  
  106.         default:  
  107.             break;  
  108.         }  
  109.     }  
  110.   
  111.     // 獲取裁剪圖片意圖的方法  
  112.     private Intent getImageClipIntent() {  
  113.         Intent intent = new Intent(Intent.ACTION_GET_CONTENT, null);  
  114.         // 實現對圖片的裁剪,必須要設置圖片的屬性和大小  
  115.         intent.setType("image/*"); // 設置屬性,表示獲取任意類型的圖片  
  116.         intent.putExtra("crop", "true");// 設置可以滑動選選擇區域的屬性,注意這裏是字符串"true"  
  117.         intent.putExtra("aspectX", 1);// 設置剪切框1:1比例的效果  
  118.         intent.putExtra("aspectY", 1);  
  119.         intent.putExtra("outputX", 80);  
  120.         intent.putExtra("outputY", 80);  
  121.         intent.putExtra("return-data", true);  
  122.         return intent;  
  123.     }  
  124. }  



 

 

 

佈局文件main.xml

 

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent"  
  5.     android:orientation="vertical" >  
  6.   
  7.     <Button   
  8.         android:layout_width="fill_parent"  
  9.         android:layout_height="wrap_content"  
  10.         android:text="選擇圖片"  
  11.         android:id="@+id/imageSelectButton"/>  
  12.     <Button   
  13.         android:layout_width="fill_parent"  
  14.         android:layout_height="wrap_content"  
  15.         android:text="選擇圖片進行裁剪"  
  16.         android:id="@+id/imageCutButton"/>  
  17.     <!-- 用於顯示裁剪後的圖片 -->  
  18.     <ImageView   
  19.         android:layout_width="wrap_content"  
  20.         android:layout_height="wrap_content"  
  21.         android:id="@+id/imageView"/>  
  22.   
  23. </LinearLayout>  
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章