android圆形头像:相机相册加载图片到圆形头像

时间:2022-08-06 23:16:19

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

源码下载地址:

http://download.csdn.net/detail/zheng_jiao/9508966