天天看點

android困惑之UI---imageView實作對圖檔的選取,和裁剪功能。。。。

跟着老羅的視訊做了這樣一個安卓程式,卻發現截取圖檔功能根本沒有實作到,這是何故?其中也寫了一些疑問在代碼的注釋中。。

MainActivity檔案:

package com.example.imageviewdemo02;

import android.support.v7.app.ActionBarActivity;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Point;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;

@SuppressLint("NewApi")
public class MainActivity extends ActionBarActivity implements OnClickListener {
	private Button choseButton;
	private Button cutButton;
	private ImageView imageView;
	// 意圖傳回标志
	private static final int CUT_BUTTON = 1;
	private static final int CHOSE_BUTTON = 2;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		choseButton = (Button) findViewById(R.id.chose);
		cutButton = (Button) findViewById(R.id.cut);
		imageView = (ImageView) findViewById(R.id.imageView);
		choseButton.setOnClickListener(this);
		cutButton.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		// 判斷是哪個Button
		switch (v.getId()) {
		case R.id.chose: {
			// action_pick是一個選擇的動作意圖,傳回我們所選擇的。第二個參數是圖檔庫url,是打開的它
			// 該intent傳回的是所選擇内容的路徑,使用getData()方法獲得該路徑
			Intent intent = new Intent(Intent.ACTION_PICK,
					android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
			startActivityForResult(intent, CHOSE_BUTTON);

			break;
		}
		case R.id.cut: {
			// 由于這個intent建構較長,是以弄個方法存放。
			Intent intent = getImageClipIntent();
			startActivityForResult(intent, CUT_BUTTON);
			break;
		}

		}

	}

	private Intent getImageClipIntent() {
		Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
		//要裁剪就要設定圖檔的屬性,大小
		//設定擷取的内容的類型為圖檔
		intent.setType("image/*");
		//放入extra資料設定可滑動截取圖檔
		intent.putExtra("crop", "true");
		//設定剪切的x,y軸的比例
		intent.putExtra("aspectX", 1);	//x方向
		intent.putExtra("aspectY", 1);	//y方向
		//設定輸出圖檔大小
		intent.putExtra("outputX", 90);
		intent.putExtra("outputY", 90);
		//設定有傳回值
		intent.putExtra("return-data", true);
		
		return intent;
	}

	// 接受執行intent後傳回的值并處理
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		if (resultCode == RESULT_OK) {
			switch (requestCode) {
			case CHOSE_BUTTON: {
				Uri uri = data.getData();	//獲得圖檔路徑
				//獲得手機螢幕的寬度和高度,存放于point中(其實螢幕的應用程式部分要比這個小因為有其他的部分占用了手機螢幕)
				Point point = new Point();
				getWindowManager().getDefaultDisplay().getSize(point);
				//對圖檔進行适應螢幕的操作
				
					try {
						//執行個體化圖檔裁剪類...這不是選擇并傳回圖檔嗎?執行個體化它幹嘛?
						BitmapFactory.Options factory = new BitmapFactory.Options();
						//還是搞不清楚這個是拿來幹嘛的。。。
						factory.inJustDecodeBounds = true;
						Bitmap bitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(uri), null, factory);
						//試驗bitmap是否為空		結果不為空
						//imageView.setImageBitmap(bitmap);
						
						//											問:為何getHeght()會出現空指針異常?
						//擷取圖檔的尺寸并和螢幕的大小作比較
						
						int height = factory.outHeight;
						int width = factory.outWidth;
						
						//做比較,若大于一這圖檔高或寬大于螢幕尺寸
						int hRadio = (int) Math.ceil(height/(float)point.y);
						int wRadio = (int)Math.ceil(width/(float)point.x);
						
						if(hRadio>1||wRadio>1){
							if(hRadio>wRadio){
								//把圖檔的大小縮放為1/hRadio
								factory.inSampleSize = hRadio;
							}else{
								factory.inSampleSize = wRadio;
							}
						}
						
						factory.inJustDecodeBounds = false;
						//為什麼要二次調用這個?何用?  上面的那個bitmap是拿來幹嘛的?
						Bitmap bitmap2 = BitmapFactory.decodeStream(getContentResolver().openInputStream(uri), null, factory);
						
						imageView.setImageBitmap(bitmap2);
					} catch (Exception e) {
						
						e.printStackTrace();
					}
					
				
				
				break;
			}

			case CUT_BUTTON: {
				//搞不懂咯   。。。為什麼選去圖檔反而比裁剪圖檔的代碼要長那麼多。。。。
				Bitmap bitmap = data.getParcelableExtra("data");
				imageView.setImageBitmap(bitmap);
			}
			}
		}
	}
}
           

xml檔案:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
    <Button 
        android:id="@+id/chose"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="選擇圖檔"
        />
    <Button 
        android:id="@+id/cut"
        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>
           

如果解決後,這篇文章就會放入已解決的困惑分類中。