1

我正在使用下面的代码使用 Square 的 Picasso 库将圆形蒙版应用于位图。它在 android 版本 3.0 及更高版本的设备上运行良好,但无法在姜饼上运行。在运行时没有显示错误或异常,相反,掩码似乎根本没有应用于原始图像。

public class CircleTransformation implements Transformation {

    @Override
    public Bitmap transform(Bitmap source) {
        int size = Math.min(source.getWidth(), source.getHeight());

        Bitmap finalBitmap = Bitmap.createBitmap(size, size, source.getConfig());

        Canvas canvas = new Canvas(finalBitmap);
        Paint paint = new Paint();
        BitmapShader shader = new BitmapShader(source, BitmapShader.TileMode.CLAMP, BitmapShader.TileMode.CLAMP);
        paint.setShader(shader);
        paint.setAntiAlias(true);

        float radius = size / 2f;
        canvas.drawCircle(radius, radius, radius, paint);

        source.recycle();

        return finalBitmap;
    }

    @Override
    public String key() {
        return "circle";
    }
}

自 API 级别 1 起,我使用的所有方法似乎都可用,所以我有点卡住了。知道可能是什么问题吗?

PD。此代码基于:https ://gist.github.com/julianshen/5829333

4

2 回答 2

2

我仍然不知道为什么这段代码在姜饼中不起作用,但我能够通过使用圆形蒙版图像让它工作:

public class PreHoneycombCircleTransformation implements Transformation {

    @Override
    public Bitmap transform(Bitmap source) {

        int dim = Constants.BUBBLE_WIDTH;

        Canvas canvas = new Canvas();

        // placeholder for final image
        Bitmap result = Bitmap.createBitmap(dim, dim, Bitmap.Config.ARGB_8888);
        canvas.setBitmap(result);
        Paint paint = new Paint();
        paint.setFilterBitmap(false);

        // resize image fills the whole canvas
        canvas.drawBitmap(source, null, new Rect(0,  0, dim, dim), paint);

        paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN));
        canvas.drawBitmap(Util.getMaskImage(), 0, 0, paint);
        paint.setXfermode(null);

        if(result != source) {
            source.recycle();
        }

        return result;
    }

    @Override
    public String key() {
        return "pre_circle";
    }
} 
于 2013-12-31T15:33:05.507 回答
1

下面的类是另一种选择,在 Gingerbread 和 ICS 中都可以使用。CircleTransformation中的代码对我来说在 ICS 中不起作用。

public class CircleTransform implements Transformation {
@Override
public Bitmap transform(Bitmap source) {
    int size = Math.min(source.getWidth(), source.getHeight());
    float radius = size / 2f;

    final Paint paint = new Paint();
    paint.setAntiAlias(true);
    paint.setShader(new BitmapShader(source, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP));

    Bitmap output = Bitmap.createBitmap(source.getWidth(), source.getHeight(), Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(output);
    canvas.drawCircle(radius, radius, radius, paint);

    if (source != output) {
        source.recycle();
    }

    return output;
}

@Override
public String key() {
    return "circle";
}
}
于 2014-03-20T08:56:04.443 回答