Given a closed Path object result is like this:
给定一个封闭的Path对象结果是这样的:
Although that is a rectangle I'm looking for something which works with any closed Path.
虽然这是一个矩形,我正在寻找适用于任何封闭路径的东西。
2 个解决方案
#1
73
While steelbytes' answer will probably give you more control over the individual sections of the gradient, you can do it without the path:
虽然steelbytes的答案可能会让你更多地控制渐变的各个部分,但你可以在没有路径的情况下完成:
protected void onDraw(Canvas canvas)
{
super.onDraw(canvas);
Paint p = new Paint();
// start at 0,0 and go to 0,max to use a vertical
// gradient the full height of the screen.
p.setShader(new LinearGradient(0, 0, 0, getHeight(), Color.BLACK, Color.WHITE, Shader.TileMode.MIRROR));
canvas.drawPaint(p);
}
#2
28
this may help
这可能有所帮助
protected void onDraw(Canvas canvas)
{
super.onDraw(canvas);
int w = getWidth();
int h = getHeight();
Paint p = new Paint(Paint.ANTI_ALIAS_FLAG|Paint.FILTER_BITMAP_FLAG);
Path pth = new Path();
pth.moveTo(w*0.27f,0);
pth.lineTo(w*0.73f,0);
pth.lineTo(w*0.92f,h);
pth.lineTo(w*0.08f,h);
pth.lineTo(w*0.27f,0);
p.setColor(0xff800000);
p.setShader(new LinearGradient(0,0,0,h,0xff000000,0xffffffff,Shader.TileMode.CLAMP));
canvas.drawPath(pth,p);
}
#1
73
While steelbytes' answer will probably give you more control over the individual sections of the gradient, you can do it without the path:
虽然steelbytes的答案可能会让你更多地控制渐变的各个部分,但你可以在没有路径的情况下完成:
protected void onDraw(Canvas canvas)
{
super.onDraw(canvas);
Paint p = new Paint();
// start at 0,0 and go to 0,max to use a vertical
// gradient the full height of the screen.
p.setShader(new LinearGradient(0, 0, 0, getHeight(), Color.BLACK, Color.WHITE, Shader.TileMode.MIRROR));
canvas.drawPaint(p);
}
#2
28
this may help
这可能有所帮助
protected void onDraw(Canvas canvas)
{
super.onDraw(canvas);
int w = getWidth();
int h = getHeight();
Paint p = new Paint(Paint.ANTI_ALIAS_FLAG|Paint.FILTER_BITMAP_FLAG);
Path pth = new Path();
pth.moveTo(w*0.27f,0);
pth.lineTo(w*0.73f,0);
pth.lineTo(w*0.92f,h);
pth.lineTo(w*0.08f,h);
pth.lineTo(w*0.27f,0);
p.setColor(0xff800000);
p.setShader(new LinearGradient(0,0,0,h,0xff000000,0xffffffff,Shader.TileMode.CLAMP));
canvas.drawPath(pth,p);
}