Pergunta

I'm using the code below to apply a round mask to a bitmap using Square's Picasso library. It works great on devices with android version 3.0 and higher, but it fails to work on gingerbread. There are no errors or exceptions displayed during runtime, instead, the mask simply does not seem to be applied to the original image.

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

All of the methods I'm using seem to be available since API level 1 so I'm kinda stuck. Any idea what could be the issue?

pd. this code was based on: https://gist.github.com/julianshen/5829333

Foi útil?

Solução

I still don't know why this code doesn't work in gingerbread, but I was able to get it to work by using a round mask image:

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

Outras dicas

The class below is another alternative, works both in Gingerbread and ICS. The code in the CircleTransformation doesn't work in ICS for me.

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";
}
}
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top