ImageView 实现从手机相册中获取照片,并可以进行裁剪或者适屏展示
此例子是根据观看了老罗视频整理的,贴上代码,方便大家学习
activity_main.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <Button android:id="@+id/select" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="选择图片" /> <Button android:id="@+id/cut" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="显示裁剪之后的图片" /> <ImageView android:id="@+id/image" android:layout_width="wrap_content" android:layout_height="wrap_content"/> </LinearLayout>
main.java:
package com.example.imageview_cut; import android.net.Uri; import android.os.Bundle; import android.R.anim; import android.R.integer; import android.app.Activity; import android.content.Intent; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.ImageView; public class MainActivity extends Activity implements OnClickListener{ private Button select,cut; 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); select=(Button) findViewById(R.id.select); cut=(Button) findViewById(R.id.cut); imageView=(ImageView) findViewById(R.id.image); select.setOnClickListener(this); cut.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; try { BitmapFactory.Options options=new BitmapFactory.Options(); options.inJustDecodeBounds=true;//允许查询图片不是按照像素分配给内存 Bitmap bmpBitmap=BitmapFactory.decodeStream(getContentResolver().openInputStream(uri),null,options); //对图片的宽度和高度对应手机屏幕进行匹配 int hRatio=(int)Math.ceil(options.outHeight/(float)dh); //大于1,表示图片的高宽度大于手机屏幕的高宽度, int wRatio=(int)Math.ceil(options.outWidth/(float)dw); //缩放到1/radio的尺寸 if (hRatio>1||wRatio>1) { if (hRatio>wRatio) { options.inSampleSize=hRatio; }else { options.inSampleSize=wRatio; } } options.inJustDecodeBounds=false; //对图片进行适屏 bmpBitmap=BitmapFactory.decodeStream(getContentResolver().openInputStream(uri), null, options); imageView.setImageBitmap(bmpBitmap); } catch (Exception e) { // TODO: handle exception } }else if (requestCode==image_cut) { Bitmap bit=data.getParcelableExtra("data"); imageView.setImageBitmap(bit); } } } public void onClick(View v) { switch (v.getId()) { case R.id.select: //如何提取手机的图片,并且进行选择图片的功能 Intent intent=new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); startActivityForResult(intent, image_select); break; case R.id.cut: Intent intent2=getImageClipIntent(); startActivityForResult(intent2, image_cut); break; } } private Intent getImageClipIntent() { 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);//指定输出图片的大小 intent.putExtra("outputY", 80); intent.putExtra("return-data", true); return intent; } }