Android创建圆形和混合阴影渐变特效
这是一个Android生成渐变的例子,比如生成圆形渐变、角度渐变、混合渐变等,并叠加了位图和线性特性,如示例截图所示,仅为其中一种渐变,代码中对几种渐变给出了注释和具体代码,对于学习Android graphics类下的相关属性方法有很好的借鉴意义:
protected void onDraw(Canvas canvas) {
//位图
//shader = new BitmapShader(b, TileMode.REPEAT, TileMode.MIRROR);
//线性
int[] colors = new int[]{
Color.RED,Color.GREEN,Color.BLUE
};
//shader = new LinearGradient(0, 0, 100, 100, colors, null, TileMode.REPEAT);
//圆形渐变
//shader = new RadialGradient(100, 100, 80, colors, null, TileMode.REPEAT);
//角度渐变
//shader = new SweepGradient(160, 160, colors, null);
//混合渐变
Shader shader1 = new LinearGradient(0, 0, 100, 100, colors, null, TileMode.REPEAT);
Shader shader2 = new RadialGradient(100, 100, 80, colors, null, TileMode.REPEAT);
shader = new ComposeShader(shader1, shader2, PorterDuff.Mode.DARKEN);
paint = new Paint();
paint.setShader(shader);
paint.setColor(Color.BLUE);
canvas.drawRect(0, 0, 500, 500, paint);
}
- 2022-03-16 19:07:27下载
- 积分:1