我有一些带有白色背景的图像和一些带有透明背景的图像。当创建它们的圆形图像时,我希望它们都具有白色背景,所以基本上我想用白色像素替换图像的透明像素。
有没有办法实现这一点而不必逐个替换像素?这是我用于创建圆角图像的转换:
package com.mobilefootie.util;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.graphics.Shader;
public class RoundedTransformation implements com.squareup.picasso.Transformation {
public RoundedTransformation() {
}
@Override
public Bitmap transform(final Bitmap source) {
int size = Math.min(source.getWidth(), source.getHeight());
int x = (source.getWidth() - size) / 2;
int y = (source.getHeight() - size) / 2;
Bitmap squaredBitmap = Bitmap.createBitmap(source, x, y, size, size);
if (squaredBitmap != source) {
source.recycle();
}
Bitmap bitmap = Bitmap.createBitmap(size, size, source.getConfig());
Canvas canvas = new Canvas(bitmap);
Paint paint = new Paint();
BitmapShader shader = new BitmapShader(squaredBitmap, BitmapShader.TileMode.CLAMP, BitmapShader.TileMode.CLAMP);
paint.setShader(shader);
paint.setAntiAlias(true);
float r = size/2f;
canvas.drawCircle(r, r, r, paint);
squaredBitmap.recycle();
return bitmap;
}
@Override
public String key() {
return "rounded";
}
}
答案 0 :(得分:10)
您必须为ColorFilter
设置Paint
(我应用绿色以获得更好的可视化效果):
BitmapShader shader = new BitmapShader(squaredBitmap, BitmapShader.TileMode.CLAMP, BitmapShader.TileMode.CLAMP);
paint.setShader(shader);
paint.setAntiAlias(true);
PorterDuff.Mode mMode = PorterDuff.Mode.OVERLAY;
paint.setColorFilter(new PorterDuffColorFilter(Color.GREEN, mMode));