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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值