imageView實現圖片適屏和裁剪圖片的功能

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;
	@Override
	protected 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);

	}

	@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 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);
			}
		}
	}

	@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;
		}
	}

	private Intent getImageClipIntent() {
		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", 180);//指定輸出圖片的大小
		intent.putExtra("outputY", 180);
		intent.putExtra("return-data",true);
		return intent;
	}





}

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