1. 程式人生 > >改進後的顯示照片(可以剪下,上傳圓形的圖片)

改進後的顯示照片(可以剪下,上傳圓形的圖片)

@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		if (requestCode == Photo.RESULT_IMAGE && data != null) {
			Cursor c = this.getContentResolver().query(data.getData(), null,
					null, null, null);
			c.moveToFirst();
			String imagePath = c.getString(c.getColumnIndex("_data"));
			c.close();
			File file = new File(imagePath);
			Uri originalUri = Uri.fromFile(file);
			ClipImage.startPhotoZoom(originalUri, REQUEST_CODE_CLIP,MainActivity.this);
		} else if (requestCode == Photo.RESULT_CAMERA) {
			File file = new File(Photo.TEMP_IMAGE_PATH);
			Uri uri_tack = Uri.fromFile(file);
			ClipImage.startPhotoZoom(uri_tack, REQUEST_CODE_CLIP,MainActivity.this);
		}else if(requestCode==REQUEST_CODE_CLIP){
			Bitmap photo = null;
			if (photo == null) {
				Bundle bundle = data.getExtras();
				if (bundle != null) {
					photo = (Bitmap) bundle.get("data");
				}
			}
			if (photo != null) {
				image.setImageBitmap(toRoundBitmap.toRoundBitmap(photo));
			}
		}
	}


剪下的工具類

public static void startPhotoZoom(Uri uri, int witch,Context context) {
		Intent intent = new Intent("com.android.camera.action.CROP");
		intent.setDataAndType(uri, "image/*");
		// 下面這個crop=true是設定在開啟的Intent中設定顯示的VIEW可裁剪
		intent.putExtra("crop", "true");
		// aspectX aspectY 是寬高的比例
		intent.putExtra("aspectX", 1);
		intent.putExtra("aspectY", 1);
		// outputX outputY 是裁剪圖片寬高
		intent.putExtra("outputX", 300);
		intent.putExtra("outputY", 300);
		// 系統的裁剪圖片預設對圖片進行人臉識別,當識別到有人臉時,
		// 會按aspectX和aspectY為1來處理,如果想設定成自定義的裁剪比例,
		// 需要設定noFaceDetection為true。
		intent.putExtra("noFaceDetection", true);
		intent.putExtra("return-data", true);
		Activity a=(Activity)context;
		a.startActivityForResult(intent, witch);
	}

顯示圓形圖片的工具類
public class ToRoundBitmap {

	private static ToRoundBitmap toRoundBitmap;

	private ToRoundBitmap(Context context) {
	}

	public static ToRoundBitmap getInstance(Context context) {
		if (toRoundBitmap == null) {
			toRoundBitmap = new ToRoundBitmap(context);
		}

		return toRoundBitmap;
	}

	public Bitmap toRoundBitmap(Bitmap bitmap) {
		int width = bitmap.getWidth();
		int height = bitmap.getHeight();
		float roundPx;
		float left, top, right, bottom, dst_left, dst_top, dst_right, dst_bottom;
		if (width <= height) {
			roundPx = width / 2;

			left = 0;
			top = 0;
			right = width;
			bottom = width;

			height = width;

			dst_left = 0;
			dst_top = 0;
			dst_right = width;
			dst_bottom = width;
		} else {
			roundPx = height / 2;

			float clip = (width - height) / 2;

			left = clip;
			right = width - clip;
			top = 0;
			bottom = height;
			width = height;

			dst_left = 0;
			dst_top = 0;
			dst_right = height;
			dst_bottom = height;
		}

		Bitmap output = Bitmap.createBitmap(width, height, Config.ARGB_8888);
		Canvas canvas = new Canvas(output);

		final Paint paint = new Paint();
		final Rect src = new Rect((int) left, (int) top, (int) right,
				(int) bottom);
		final Rect dst = new Rect((int) dst_left, (int) dst_top,
				(int) dst_right, (int) dst_bottom);
		final RectF rectF = new RectF(dst);

		paint.setAntiAlias(true);// 設定畫筆無鋸齒

		canvas.drawARGB(0, 0, 0, 0); // 填充整個Canvas

		// 以下有兩種方法畫圓,drawRounRect和drawCircle
		canvas.drawRoundRect(rectF, roundPx, roundPx, paint);// 畫圓角矩形,第一個引數為圖形顯示區域,第二個引數和第三個引數分別是水平圓角半徑和垂直圓角半徑。
		// canvas.drawCircle(roundPx, roundPx, roundPx, paint);

		paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));// 設定兩張圖片相交時的模式,參考http://trylovecatch.iteye.com/blog/1189452

		canvas.drawBitmap(bitmap, src, dst, paint);// 以Mode.SRC_IN模式合併bitmap和已經draw了的Circle

		return output;
	}
}