Android 圖片剪下後儲存為圓角圖片踩的坑
Bitmap bitmap = BitmapFactory.decodeResource(getResources(),R.mipmap.back);
/**
* 儲存Bitmap到檔案
*/
public void saveBitmap(Bitmap bitmap) {
Log.e(“tiancb”, “儲存圖片”);
File file = new File(“/sdcard/namecard”);
if (!file.exists()) {
try {
//按照指定的路徑建立資料夾
file.mkdirs();
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
File dir = new File(“/sdcard/namecard” + “/” + “test.png”);
if (!dir.exists()) {
try {
//在指定的資料夾中建立檔案
dir.createNewFile();
} catch (Exception e) {
e.printStackTrace();
}
}
try {
FileOutputStream out = new FileOutputStream(dir);
bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
out.flush();
out.close();
Log.i("tiancb", "已經儲存");
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* 獲取圓角Bitmap
* @param bitmap
* @return
*/
public Bitmap getImage(Bitmap bitmap){ Bitmap result = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(result); Paint paint1 = new Paint(); paint1.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR)); canvas.drawPaint(paint1); paint1.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC)); canvas.drawColor(Color.parseColor("#00ff0000")); final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight()); RectF rectF = new RectF(rect); Paint paint = new Paint(); paint.setAntiAlias(true); paint.setFilterBitmap(true); paint.setColor(Color.YELLOW); final float roundPx = 44; Path path = new Path(); path.addRoundRect(rectF, roundPx, roundPx, Path.Direction.CW); canvas.clipPath(path, Region.Op.INTERSECT); canvas.drawBitmap(bitmap, rect, rectF, paint); return result; }