我正在尝试为我的 android 应用程序中的图像添加圆角。我用于添加圆角的代码是:
Uri selectedImage = data.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA };
Cursor cursor = getContentResolver().query(selectedImage,
filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
picturePath = cursor.getString(columnIndex);
cursor.close();
final ImageView imageView = (ImageView) findViewById(R.id.imageView1);
final SeekBar cornerRadius = (SeekBar) findViewById(R.id.seekBar1);
final TextView tip = (TextView) findViewById(R.id.tip);
bm = getRoundedCornerBitmap(BitmapFactory.decodeFile(picturePath),
cornerRadius.getProgress());
imageView.setImageBitmap(bm);
上面的圆角半径是从 seekbar 的值中得到的。
public static Bitmap getRoundedCornerBitmap(Bitmap bitmap, int pixels) {
Bitmap output = Bitmap.createBitmap(bitmap.getWidth(),
bitmap.getHeight(), 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());
final RectF rectF = new RectF(rect);
final float roundPx = pixels;
paint.setAntiAlias(true);
canvas.drawARGB(0, 0, 0, 0);
paint.setColor(color);
canvas.drawRoundRect(rectF, roundPx, roundPx, paint);
paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
canvas.drawBitmap(bitmap, rect, rect, paint);
return output;
}
我在单击保存按钮时调用的将图片保存到 sd 卡的代码是:
OutputStream fOut = null;
File file = new File(picturePath);
try {
fOut = new FileOutputStream(file);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
bm.compress(Bitmap.CompressFormat.JPEG, 85, fOut);
sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri
.parse("file://"
+ Environment.getExternalStorageDirectory())));
Toast.makeText(MainActivity.this,
"Pic save successfuly. Path : " + picturePath,
Toast.LENGTH_SHORT).show();
我面临的问题是,一旦将图像编辑为圆角。下一次,我打开相同的图像并尝试使用不同的圆角半径值保存不会保存图像。我确实第二次收到上述 Toast 消息,但图像保持不变。