0

我正在处理的项目需要圆角(是的,不幸的是来自 iphone 的副本)。下载图像时,我会绕过角落,然后显示。这是我如何绕过角落...

public class ImageRounder {

    private Paint paint = new Paint();
    private Canvas canvas;
    private Rect rect;
    private RectF rectF;
    final int color = 0xff424242;

    @Inject
    public ImageRounder() {}

    public synchronized Bitmap getRoundedCornerBitmap(Bitmap bitmap, int pixels) {
        Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap
                .getHeight(), Bitmap.Config.ARGB_8888);
        canvas = new Canvas(output);
        paint.reset();
        rect =  new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
        rectF = new RectF(rect);
        float roundPx = pixels;

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

        canvas.drawRoundRect(rectF, roundPx, roundPx, paint);

        paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
        canvas.drawBitmap(bitmap, rect, rect, paint);

        return output;

    }
}

我还将图像保存到磁盘,因此我不必多次下载。这就是我保存和检索图像的方式

@Override
public synchronized void saveImage(String id, Bitmap bitmap) throws FileNotFoundException       {
    FileOutputStream outputStream = context.openFileOutput(id, Context.MODE_PRIVATE);
    bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outputStream);
}

@Override
public synchronized Bitmap getImage(String id) throws FileNotFoundException {
    return BitmapFactory.decodeStream(context.openFileInput(id));
}

问题是当我从磁盘检索图像时,它看起来像这样......

在此处输入图像描述

很难告诉我角落是圆的,但背景是黑色的。我尝试将图像视图的背景设置为白色和透明,但角落仍然显示为黑色。

有人知道这是为什么吗?

4

1 回答 1

1

saveImage中,您将位图压缩为 JPEG。

为了透明度,您必须使用Bitmap.CompressFormat.PNG

于 2012-12-11T15:40:41.107 回答