在Canvas

时间:2015-08-03 18:57:48

标签: android canvas bitmap

我正在使用一种方法,该方法在画布的外部空间中返回带有填充:透明颜色的圆角位图。我在自定义适配器中使用该方法在列表中显示圆角形状的图像但由于某种原因填充适配器正在抛出" OutOfMemoryError"异常。

public static Bitmap getRoundedShape(Bitmap scaleBitmapImage, int width){
    try {
        int targetWidth = width;
        int targetHeight = width;

        Bitmap targetBitmap = Bitmap.createBitmap(targetWidth, targetHeight, Bitmap.Config.ARGB_8888);

        Canvas canvas = new Canvas(targetBitmap);

        Path path = new Path();
        float x = ((float) targetWidth - 1) / 2;
        float y = ((float) targetHeight - 1) / 2;
        float radius = (Math.min(((float) targetWidth), ((float)targetHeight)) / 2);
        path.addCircle(x, y, radius, Path.Direction.CCW);
        canvas.clipPath(path);

        Bitmap sourceBitmap = scaleBitmapImage;
        Rect sourceRect = new Rect(0, 0, sourceBitmap.getWidth(), sourceBitmap.getHeight());
        Rect destRect = new Rect(0, 0, targetWidth, targetHeight);
        canvas.drawBitmap(sourceBitmap, sourceRect, destRect, null);
        return targetBitmap;
    } catch (Exception ex) {
        return scaleBitmapImage;
    }
}

1 个答案:

答案 0 :(得分:0)

尝试将变量设置为null。这有助于GC释放一些内存。