1. 程式人生 > >android圓形頭像:相機相簿載入圖片到圓形頭像

android圓形頭像:相機相簿載入圖片到圓形頭像

這是現在很多軟體裡面很常用的功能;

首先說下佈局:佈局檔案非常簡單,使用的是一個自定義的圓形頭像;

當點選圓形頭像的時候彈出一個popWindow,然後點選拍照,相簿的時候呼叫系統的相機和系統的相簿

佈局檔案:(circleImageView是一個自定義的控制元件整合ImageView)

    <!-- xmlns:app="http://schemas.android.com/apk/res-auto"  這個必須寫上,不然不生效
     app:border_color="#00000000"                        去掉黑邊框 -->
    <com.circle.www.view.CircleImageView
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/circleImageView"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_centerInParent="true"
        app:border_color="#00000000" 
        android:src="@drawable/g"
        />

activity:呼叫系統相機相簿:
int REQUESTCODE_CAMERA = 0;
	int REQUESTCODE_GALLERY = 1;
	int REQUESTCODE_CROP = 2;
	Bitmap changedImage;
	String newPath = null;
	Uri outputUri = null;

	// 啟用系統相機
	protected void showCamera() {
		Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
		startActivityForResult(intent, REQUESTCODE_CAMERA);
	}

	// 啟用系統
	protected void showPhone() {
		Intent intent = new Intent(Intent.ACTION_PICK, null);
		intent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
				"image/*");
		startActivityForResult(intent, REQUESTCODE_GALLERY);
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {

		super.onActivityResult(requestCode, resultCode, data);

		// 相機回撥
		if (requestCode == REQUESTCODE_CAMERA) {

			if (resultCode == RESULT_OK) {
				Bundle bundle = data.getExtras();
				changedImage = (Bitmap) bundle.get("data");
				circleImageView.setImageBitmap(changedImage);
			}
		}// 相簿回撥
		else if (requestCode == REQUESTCODE_GALLERY) {

			if (resultCode == RESULT_OK) {

				String path = FileUtils.getPictureSelectedPath(data, this);
				newPath = CacheUtils.getImagePath(this, "sendImage/"
						+ TypeConverter.getUUID() + ".jpg");
				try {
					Bitmap bitmap = ImageResizer.decodeSampledBitmapFromFile(
							path, 400, 400);
					FileUtils.compressAndWriteFile(bitmap, this, newPath);

					startPhotoZoom(this, Uri.fromFile(new File(newPath)));

				} catch (IOException e) {
					e.printStackTrace();
				}

			}
		}
		// 裁剪回撥
		else if (requestCode == REQUESTCODE_CROP) {
			if (resultCode == RESULT_OK) {
				try {
					changedImage = BitmapFactory
							.decodeStream(getContentResolver().openInputStream(
									outputUri));
					circleImageView.setImageBitmap(changedImage);
				} catch (FileNotFoundException e) {
					e.printStackTrace();
				}

			}
		}
	}

	private void startPhotoZoom(Activity activity, Uri uri) {
		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", 100);
		intent.putExtra("outputY", 100);
		intent.putExtra("scale", true);
		intent.putExtra("return-data", false);
		intent.putExtra(MediaStore.EXTRA_OUTPUT, outputUri);
		intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());
		intent.putExtra("noFaceDetection", true);
		activity.startActivityForResult(intent, REQUESTCODE_CROP);
	}

原始碼下載地址: