ImageView的适屏与裁剪
适屏是指将图片以合适的大小显示在手机的屏幕上。如果图片的大小不符合手机屏幕的显示要求,那么就需要对图片进行适当的裁剪,以满足显示需求。
在对图片进行适屏显示和裁剪之间,我们首先需要做的是从手机的图库中获取图片资源。
获取手机的图片库
在Android中,通过如下方法就可以打开手机的图片库:
Intent intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
适屏的实现
很显然,要将图片以合适的尺寸显示在手机屏幕上,我们首先需要知道手机屏幕的大小。我们可以通过以下两个函数方法来获得手机屏幕的长和宽。
int dw = getWindowManager().getDefaultDisplay().getWidth();//获得手机屏幕的宽度 int dh = getWindowManager().getDefaultDisplay().getHeight();//获得手机屏幕的高度
裁剪的实现
private Intent getImageClipIntent() { // TODO Auto-generated method stub Intent intent = new Intent(Intent.ACTION_GET_CONTENT, null); // 实现对图片的裁剪,必须要设置图片的属性和大小 intent.setType("image/*");// 获取任意的图片类型 intent.putExtra("crop", "true");// 以滑动形式来选中图片区域 intent.putExtra("aspectX", 1);// 表示剪切框的比例为:1 intent.putExtra("aspectY", 1); intent.putExtra("outputX", 80);//指定输出图片的大小为:80*80 intent.putExtra("outputY", 80); intent.putExtra("return_data", true); return intent; }
效果
具体代码如下:

1 package com.leaf.android; 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.text.Spannable.Factory; 10 import android.view.View; 11 import android.view.View.OnClickListener; 12 import android.widget.Button; 13 import android.widget.ImageView; 14 15 public class Main extends Activity implements OnClickListener { 16 /** Called when the activity is first created. */ 17 private Button selectImageBtn; 18 private Button cutImageBtn; 19 private ImageView imageView; 20 // 声明两个静态的整形变量,主要是用于意图的返回的标志 21 private static final int IMAGE_SELECT = 1; 22 private static final int IMAGE_CUT = 2; 23 24 @Override 25 public void onCreate(Bundle savedInstanceState) { 26 super.onCreate(savedInstanceState); 27 setContentView(R.layout.main); 28 selectImageBtn = (Button) this.findViewById(R.id.selectImageBtn); 29 cutImageBtn = (Button) this.findViewById(R.id.cutImageBtn); 30 imageView = (ImageView) this.findViewById(R.id.imageview); 31 selectImageBtn.setOnClickListener(this); 32 cutImageBtn.setOnClickListener(this); 33 } 34 35 @Override 36 protected void onActivityResult(int requestCode, int resultCode, Intent data) { 37 // TODO Auto-generated method stub 38 super.onActivityResult(requestCode, resultCode, data); 39 if (resultCode == RESULT_OK) { 40 // 处理图片按照手机的屏幕大小显示 41 if (requestCode == IMAGE_SELECT) { 42 Uri uri = data.getData();// 获得图片的路径 43 int dw = getWindowManager().getDefaultDisplay().getWidth();// 获得屏幕的宽度 44 int dh = getWindowManager().getDefaultDisplay().getHeight() / 2;// 获得屏幕的高度 45 try { 46 // 实现对图片的裁剪的类,是一个匿名内部类 47 BitmapFactory.Options factory = new BitmapFactory.Options(); 48 factory.inJustDecodeBounds = true;// 如果设置为true,允许查询图片不是按照像素分配给内存 49 Bitmap bmp = BitmapFactory.decodeStream( 50 getContentResolver().openInputStream(uri), null, 51 factory); 52 // 对图片的宽度、高度对应手机的屏幕进行匹配 53 int hRatio = (int) Math 54 .ceil(factory.outHeight / (float) dh); 55 // 如果大于1表示图片的高度大于手机屏幕的高度 56 int wRatio = (int) Math.ceil(factory.outWidth / (float) dw); 57 // 如果大于1表示图片的宽度大于手机屏幕的宽度 58 // 缩放到1/radio的尺寸和1/radio^2像素 59 if (hRatio > 1 || wRatio > 1) { 60 if (hRatio > wRatio) { 61 factory.inSampleSize = hRatio; 62 } else { 63 factory.inSampleSize = wRatio; 64 } 65 } 66 67 factory.inJustDecodeBounds = false; 68 // 使用BitmapFactory对图片进行适屏的操作 69 bmp = BitmapFactory.decodeStream(getContentResolver() 70 .openInputStream(uri), null, factory); 71 imageView.setImageBitmap(bmp); 72 } catch (Exception e) { 73 // TODO: handle exception 74 } 75 // 表示裁剪图片 76 } else if (requestCode == IMAGE_CUT) { 77 Bitmap bitmap = data.getParcelableExtra("data"); 78 imageView.setImageBitmap(bitmap); 79 } 80 } 81 } 82 83 public void onClick(View v) { 84 // TODO Auto-generated method stub 85 switch (v.getId()) { 86 case R.id.selectImageBtn: 87 // 如何提取手机的图片,并且进行选择图片的功能 88 Intent intent = new Intent( 89 Intent.ACTION_PICK, 90 android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);// 打开手机的图片库 91 startActivityForResult(intent, IMAGE_SELECT); 92 break; 93 case R.id.cutImageBtn: 94 Intent intent2 = getImageClipIntent(); 95 startActivityForResult(intent2, IMAGE_CUT); 96 break; 97 } 98 } 99 100 private Intent getImageClipIntent() { 101 // TODO Auto-generated method stub 102 Intent intent = new Intent(Intent.ACTION_GET_CONTENT, null); 103 // 实现对图片的裁剪,必须要设置图片的属性和大小 104 intent.setType("image/*");// 获取任意的图片类型 105 intent.putExtra("crop", "true");// 以滑动形式来选中图片区域 106 intent.putExtra("aspectX", 1);// 表示剪切框的比例为:1 107 intent.putExtra("aspectY", 1); 108 intent.putExtra("outputX", 80);//指定输出图片的大小为:80*80 109 intent.putExtra("outputY", 80); 110 intent.putExtra("return_data", true); 111 return intent; 112 } 113 }

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:id="@+id/selectImageBtn" 9 android:layout_width="fill_parent" 10 android:layout_height="wrap_content" 11 android:text="选择图片" /> 12 13 <Button 14 android:id="@+id/cutImageBtn" 15 android:layout_width="fill_parent" 16 android:layout_height="wrap_content" 17 android:text="裁剪图片" /> 18 19 <!-- 用于显示图片的信息 --> 20 21 <ImageView 22 android:id="@+id/imageview" 23 android:layout_width="wrap_content" 24 android:layout_height="wrap_content" /> 25 26 </LinearLayout>