我的目标是为我的app设置圆形个人资料照片。用户可以裁剪图像并将其作为个人资料照片。我的代码可以裁剪图片,但无法将其放在图像视图中。请帮助! 请找到以下代码:
Intent photoPickerIntent = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
photoPickerIntent.setType("image/*");
photoPickerIntent.putExtra("crop", "true");
photoPickerIntent.putExtra("return-data", true);
photoPickerIntent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());
startActivityForResult(photoPickerIntent,SELECT_PICTURE_PROFILE);
}
protected void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent, Intent data) {
super.onActivityResult(requestCode, resultCode, imageReturnedIntent);
switch (requestCode) {
case SELECT_PICTURE_PROFILE:
if (resultCode == RESULT_OK) {
if (imageReturnedIntent!=null) {
Bundle extras = imageReturnedIntent.getExtras();
Bitmap selectedBitmap = extras.getParcelable("data");
BitmapDrawable bd=new BitmapDrawable(getResources(), selectedBitmap);
imageViewRound = (ImageView) findViewById(R.id.cimgv1);
imageViewRound.setImageDrawable(bd);
}
}
}
答案 0 :(得分:1)
public static Bitmap getCroppedBitmap(Bitmap bitmap) {
Bitmap output = Bitmap.createBitmap(bitmap.getWidth(),
bitmap.getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(output);
final int color = 0xff424242;
final Paint paint = new Paint();
final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
paint.setAntiAlias(true);
canvas.drawARGB(0, 0, 0, 0);
paint.setColor(color);
// canvas.drawRoundRect(rectF, roundPx, roundPx, paint);
canvas.drawCircle(bitmap.getWidth() / 2, bitmap.getHeight() / 2,
bitmap.getWidth() / 2, paint);
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
canvas.drawBitmap(bitmap, rect, rect, paint);
//Bitmap _bmp = Bitmap.createScaledBitmap(output, 60, 60, false);
//return _bmp;
return output;
}
使用此功能获取裁剪图像。