Gradient Alpha Tranform
工作中遇到设计给的效果是,图片要从左到右实现一个 alpha 渐变的的效果。当时做的时候花费了蛮长时间的。特此记录一下。
public class GradientAlphaTransform implements Transformation {
/**
* A unique key for the transformation, used for caching purposes.
*/
private static final String KEY = "GradientAlphaTransform";
@Override
public Bitmap transform(Bitmap source) {
// Init shader
Shader gradientAlphaShader = new LinearGradient(
0,
0,
source.getWidth(),
0,
0x99ffffff,
0x00ffffff,
Shader.TileMode.CLAMP);
// Init paint
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN));
paint.setShader(gradientAlphaShader);
// Create and draw circle bitmap
Bitmap output = Bitmap.createBitmap(source.getWidth(), source.getHeight(), source.getConfig());
Canvas canvas = new Canvas(output);
canvas.drawBitmap(source, 0, 0, null);
canvas.drawRect(new RectF(0, 0, source.getWidth(), source.getHeight()), paint);
// Recycle the source bitmap, because we already generate a new one
source.recycle();
return output;
}
@Override
public String key() {
return KEY;
}
}
网友评论