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>  
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章