Paint paint = new Paint();
paint.setStyle(Paint.Style.FILL);
paint.setAntiAlias(true);
Shader linerGradient0 = new LinearGradient(0, 100, 200, 100,
Color.parseColor("#E91E63"), Color.parseColor("#2196F3"),
Shader.TileMode.CLAMP);
paint.setShader(linerGradient0);
canvas.drawCircle(100, 100, 100, paint);
Shader linerGradient0 = new LinearGradient(0, 100, 200, 100,
Color.parseColor("#E91E63"), Color.parseColor("#2196F3"),
Shader.TileMode.CLAMP);
- 辐射渐变 RadialGradient
- 从中间向外,辐射型渐变
Shader radialGradient0 = new RadialGradient(100, 350, 100,
Color.parseColor("#E91E63"), Color.parseColor("#2196F3"),
Shader.TileMode.CLAMP);
- 扫描渐变 SweepGradient
- 扇面扫描类型的渐变
Shader sweepGradient = new SweepGradient(300, 300, Color.parseColor("#E91E63"),
Color.parseColor("#2196F3"));
paint.setShader(sweepGradient);
- 其中线性线性渐变和辐射渐变有一个 TileMode 属性,CLAMP、MIRROR、REPEAT 三种样式 加载、镜像、重复模式
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.batman);
Shader shader = new BitmapShader(bitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
paint.setShader(shader);
- 其中第二、三个参数,是决定水平和竖直方向上的图片显示模式同上
setStrokeWidth(float width) 设置线条宽度
setStrokeCap(Paint.Cap cap) 设置线头的形状
setStrokeJoin(Paint.Join join) 设置拐角的形状
setStrokeMiter(float miter)
PathEffect pathEffect = new CornerPathEffect(20);
paint.setPathEffect(pathEffect);
PathEffect pathEffect = new DiscretePathEffect(20, 5);
paint.setPathEffect(pathEffect);
PathEffect pathEffect = new DashPathEffect(new float[]{20, 10, 5, 10}, 0);
paint.setPathEffect(pathEffect);
Path dashPath = new Path();
dashPath.moveTo(0, 0);
dashPath.lineTo(10, 0);
dashPath.lineTo(5, 10);
dashPath.close();
PathEffect pathDashPathEffect = new PathDashPathEffect(dashPath, 40, 0,
PathDashPathEffect.Style.ROTATE);
paint.setPathEffect(pathDashPathEffect);
PathEffect sumPathEffect = new SumPathEffect(discretePathEffect, dashPathEffect);
paint.setPathEffect(sumPathEffect);
PathEffect composePathEffect = new ComposePathEffect(discretePathEffect, dashPathEffect);
paint.setPathEffect(composePathEffect);
paint.setShadowLayer(20, 10, 10, Color.RED);
MaskFilter blurMaskFilter = new BlurMaskFilter(50, BlurMaskFilter.Blur.OUTER);
new EmbossMaskFilter(new float[]{0, 1, 1}, 0.2f, 8, 10)
paint.setFlags(Paint.ANTI_ALIAS_FLAG | Paint.DITHER_FLAG);
等同于
paint.setAntiAlias(true);
paint.setDither(true);