我正在尝试在我的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);
上面的圆角半径来自搜索条的值。
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消息,但图像保持不变。
答案 0 :(得分:0)
搞定了。读写权限搞砸了。