簡體   English   中英

具有透明背景的圓形裁剪圖像-Android

[英]Circle crop image with transparent background - Android

我正在嘗試執行此處描述的操作:“我想從該位圖裁剪圓形區域。圓形之外的所有像素都應該是透明的。”

我已經嘗試過該帖子中的內容,但它們都不提供透明背景,僅提供圓形圖像,我嘗試了以下內容,但沒有任何效果?

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

您可以使用此庫來實現您的目標

https://github.com/hdodenhof/CircleImageView

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM