如何用线性渐变填充Android中的路径?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2836098/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to fill a Path in Android with a linear gradient?
提问by Carl Whalley
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.
虽然这是一个矩形,但我正在寻找适用于任何封闭路径的东西。
回答by idbrii
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);
}
回答by SteelBytes
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);
}