1.使用OpenGl实现,在片元着色器中针对不同的s(纹理坐标)值,设置不同的alpha值。
2.利用Canvas绘制
canvas.drawBitmap(mBitmap, 0, 0, new Paint());
Paint p = new Paint();
p.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN));
canvas.saveLayer(new RectF(0, 0, mBitmap.getWidth(), mBitmap.getHeight()), p);
canvas.drawPaint(getAlphaPaint(mBitmap.getWidth(), mBitmap.getHeight()));
canvas.restore();
getAlphaPaint可以是透明度从左到右逐渐变化的paint,类似以下代码。
private Paint getAlphaPaint(int imageW, int imageH) {
Paint paint = new Paint();
if (mProgress > 0f && mProgress < 0.2f) {
paint.setShader(new LinearGradient(0, imageH / 2, imageW * mProgress, imageH / 2, Color.TRANSPARENT, Color.BLACK, Shader.TileMode.CLAMP));
} else {
paint.setShader(new LinearGradient(0, imageH / 2, imageW / 5, imageH / 2, Color.TRANSPARENT, Color.BLACK, Shader.TileMode.CLAMP));
}
return paint;
}
2017年3月29日 14:14