我正在使用下面的代码使用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
答案 0 :(得分: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";
}
}
答案 1 :(得分: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";
}
}