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

猜你喜欢

转载自blog.csdn.net/wanzhuanit/article/details/52787997