圆形裁剪图像与透明背景 - Android

时间:2016-02-07 22:57:15

标签: android graphics

我尝试做here"我想从此位图裁剪圆形区域。圆圈外的所有像素都应该是透明的。"

我已经尝试了那个帖子中的内容,但是他们都没有提供透明的背景,只有圆形图像,我尝试了下面的内容并且它没有工作,任何想法?< / p>

public Bitmap getCroppedCircleImage(Bitmap bmp, View view) {
    Bitmap bitmap = getCroppedImage(bmp, view);

    Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(output);

    final int color = 0xff227722;
    final Paint paint = new Paint();
    final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());

    paint.setAntiAlias(true);
    canvas.drawARGB(0, 0, 0, 0);
    paint.setColor(color);

    //canvas.drawRoundRect(rectF, roundPx, roundPx, paint);
    canvas.drawCircle(bitmap.getWidth() / 2, bitmap.getHeight() / 2, bitmap.getWidth() / 2, paint);
    paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
    canvas.drawBitmap(bitmap, rect, rect, paint);

    Paint paint1 = new Paint();
    paint1.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
    Rect rectTransparent=new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
    canvas.drawRect(rectTransparent,paint1);
    paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC));
    canvas.drawBitmap(bitmap, rect, rect, paint);

    //Bitmap _bmp = Bitmap.createScaledBitmap(output, 60, 60, false);
    //return _bmp;
     OutputStream stream = null;
    try {
        stream = new FileOutputStream("/sdcard/test.png");
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
    bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
    try {
        stream.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return output;
}

1 个答案:

答案 0 :(得分:0)

您可以使用此库来实现目标

https://github.com/hdodenhof/CircleImageView