将修改后的位图存储到磁盘

时间:2012-12-11 14:59:08

标签: android

我正在进行的项目需要圆角(不幸的是,是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;

    }
}

我还将图像保存到磁盘,所以我不必下载它比onces更多。这是我保存和检索图像的方式

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

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

enter image description here

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

有人知道这是为什么吗?

1 个答案:

答案 0 :(得分:1)

saveImage中,您压缩JPEG中的位图。

要获得透明度,您必须使用Bitmap.CompressFormat.PNG