Android -- 使用图库文件并可以裁剪文件(ImageView)
1. 本例子首先教大家如何打开图库 ,然后在使用裁剪功能,来裁剪图库里面的图片并进行显示工作
2. 实现代码
public class MainActivity extends Activity implements OnClickListener { // 选择按钮以及裁剪按钮 private Button selectImageBtn , cutImageBtn; // 用于显示裁剪过后的图片 private ImageView imageView; // 声明两个整形常量,主要是用来做意图返回的标志 private static final int IMAGE_SELECT = 1;// 选择图片 private static final int IMAGE_CUT = 2;// 裁剪图片 @ Override protected void onCreate ( Bundle savedInstanceState ) { super.onCreate ( savedInstanceState ); setContentView ( R.layout.activity_main ); // 找到组件 imageView = ( ImageView ) this.findViewById ( R.id.imageview ); selectImageBtn = ( Button ) this .findViewById ( R.id.selectImageBtn ); cutImageBtn = ( Button ) this.findViewById ( R.id.cutImageBtn ); // 为组件设置监听事件 selectImageBtn.setOnClickListener ( this ); cutImageBtn.setOnClickListener ( this ); } @ Override protected void onActivityResult ( int requestCode , int resultCode , Intent data ) { // TODO Auto-generated method stub super.onActivityResult ( requestCode , resultCode , data ); if (resultCode == RESULT_OK) { // 处理图片按照手机屏幕大小显示 if (requestCode == IMAGE_SELECT) { Uri uri = data.getData ( );// 获取图片的路径 int dw = getWindowManager ( ).getDefaultDisplay ( ) .getWidth ( );// 获得屏幕的宽度 int dh = getWindowManager ( ).getDefaultDisplay ( ) .getHeight ( ) / 2;// 获得屏幕的高度,除以2是为了防止图片的拉伸 try { // 实现对图片裁剪的类,是一个匿名内部类 BitmapFactory.Options factory = new BitmapFactory.Options ( ); factory.inJustDecodeBounds = true;// 如果设置为true,允许查询图片不是按照像素分配给内存 Bitmap bmp = BitmapFactory.decodeStream ( getContentResolver ( ).openInputStream ( uri ) , null , factory ); // 对图片的宽度和高度对应手机的屏幕进行匹配 int hRatio = ( int ) Math.ceil ( factory.outHeight / ( float ) dh ); // 如果大于1表示图片的高度大于手机屏幕的高度 int wRatio = ( int ) Math.ceil ( factory.outHeight / ( float ) dw ); // 如果大于1表示图片的宽度大于手机屏幕的宽度 // 缩放1/radio的尺寸和1/radio^2像素 if (hRatio > 1 || wRatio > 1) { if (hRatio > wRatio) { factory.inSampleSize = hRatio; } else { factory.inSampleSize = wRatio; } } factory.inJustDecodeBounds = false; // 使用BitmapFactory对图片进行适应屏幕的操作 bmp = BitmapFactory.decodeStream ( getContentResolver ( ).openInputStream ( uri ) , null , factory ); imageView.setImageBitmap ( bmp ); } catch ( Exception e ) { // TODO: handle exception } } else if (requestCode == IMAGE_CUT) { Bitmap bitmap = data.getParcelableExtra ( "data" ); imageView.setImageBitmap ( bitmap ); } } } // 组件的监听事件 @ Override public void onClick ( View v ) { // TODO Auto-generated method stub switch ( v.getId ( ) ) { case R.id.selectImageBtn : // 如何提取手机的图片库,并且进行选择图片的功能 Intent intent = new Intent ( Intent.ACTION_PICK , android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI );// 打开手机的图片库 startActivityForResult ( intent , IMAGE_SELECT ); break; case R.id.cutImageBtn : Intent intent2 = getImageClipIntent ( ); startActivityForResult ( intent2 , IMAGE_CUT ); break; default : break; } } 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:1的效果 intent.putExtra ( "aspectY" , 1 ); intent.putExtra ( "outputX" , 80 );// 指定输出图片的大小 intent.putExtra ( "outputY" , 80 ); intent.putExtra ( "return-data" , true ); return intent; } }