imageView实现图片适屏和裁剪图片的功能

来源:互联网 发布:网站源码整站复制工具 编辑:程序博客网 时间:2024/05/09 04:34
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"    android:paddingBottom="@dimen/activity_vertical_margin"    android:paddingLeft="@dimen/activity_horizontal_margin"    android:paddingRight="@dimen/activity_horizontal_margin"    android:paddingTop="@dimen/activity_vertical_margin"    tools:context="com.example.imageviewfitscreenandclippicture.MainActivity$PlaceholderFragment" >    <Button        android:id="@+id/selectImageBtn"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="选择图片" />    <Button        android:id="@+id/cutImageBtn"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="选择图片进行裁剪" />    <ImageView        android:id="@+id/imageView"        android:layout_width="wrap_content"        android:layout_height="wrap_content" /></LinearLayout>

java代码:

package com.example.imageviewfitscreenandclippicture;import android.support.v7.app.ActionBarActivity;import android.support.v7.app.ActionBar;import android.support.v4.app.Fragment;import android.os.Bundle;import android.view.LayoutInflater;import android.view.Menu;import android.view.MenuItem;import android.view.View;import android.view.View.OnClickListener;import android.view.ViewGroup;import android.widget.Button;import android.widget.ImageView;import android.content.Intent;import android.graphics.Bitmap;import android.graphics.BitmapFactory;import android.net.Uri;import android.os.Build;public class MainActivity extends ActionBarActivity implements OnClickListener{private Button selectImageBtn,cutImageBtn;private ImageView imageView;private static final int IMAGE_SELECT=1;private static final int IMAGE_CUT=2;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.fragment_main);selectImageBtn=(Button) findViewById(R.id.selectImageBtn);cutImageBtn=(Button) findViewById(R.id.cutImageBtn);imageView=(ImageView) findViewById(R.id.imageView);selectImageBtn.setOnClickListener(this);cutImageBtn.setOnClickListener(this);}@Overrideprotected void onActivityResult(int requestCode, int resultCode, Intent data) {// TODO Auto-generated method stubsuper.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 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.outWidth/(float)dw);//如果大于1表示图片的宽度大于手机屏幕的宽度//缩放到1/radi=o的尺寸和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 bmp=data.getParcelableExtra("data");imageView.setImageBitmap(bmp);}}}@Overridepublic void onClick(View v) {// TODO Auto-generated method stubswitch (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;}}private Intent getImageClipIntent() {Intent intent=new Intent(Intent.ACTION_GET_CONTENT, null);//实现对图片的裁剪,必须要设置图片的属性和大小intent.setType("image/*");intent.putExtra("crop", "true");//滑动选中图片区域intent.putExtra("aspectX", 1);//表示剪切框的比例1:1intent.putExtra("aspectY", 1);intent.putExtra("outputX", 180);//指定输出图片的大小intent.putExtra("outputY", 180);intent.putExtra("return-data",true);return intent;}}

0 0