Android在一个路径的一部分填? [英] Android fill in part of a path?
问题描述
我有一个形状我与路径绘制。我填补了形状渐变,然后我需要把另外一个灰色地带ontop的梯度依赖于%的。我使用path.quadTo,使我的造型,所以我不知道顶线的y坐标正确相交的。这是我得到什么,当我只是将其设置为最大Y:
白色的行程是我试图在部分填充图像,右边灰色区域我想保留,但我需要摆脱留下灰色地带。有任何想法吗?这就是我想到目前为止:
@覆盖
公共无效的onDraw(帆布油画){
路径path =新路径();
路径grayPath =新路径();
浮X1,Y1,X3,Y3,X2,Y2;
浮x1g,x2g; INT宽度= canvas.getWidth();
INT高度= canvas.getHeight(); gradientPaint.setShader(新的LinearGradient(0,高度,宽度,高度,新的INT [] {Color.RED,Color.YELLOW,Color.GREEN},新的浮动[] {0,0.6f,1},Shader.TileMode。重复)); X1 = 0;
Y1 =(浮点)(高* 0.90); X2 =(浮点)(宽* .75)在;
Y2 =(浮点)(高* 0.50); X3 =宽度;
Y3 =(浮点)(高* 0.10); x2g =(浮点)(宽* 0.50); //斜坡
path.moveTo(X1,Y1);
path.quadTo(X2,Y2,X3,Y3);
//下
path.lineTo(X3,Y1 + 50);
//背部
path.lineTo(X1,Y1 + 50);
//向上
path.lineTo(X1,Y1); //斜坡
grayPath.moveTo(X1,Y1);
grayPath.quadTo(X2,Y2,X3,Y3);
//下
grayPath.lineTo(X3,Y1 + 50);
//背部
grayPath.lineTo(x2g,Y1 + 50);
//向上
grayPath.lineTo(x2g,Y3); grayPath.setFillType(FillType.WINDING);
//画出闪亮的填充
//canvas.drawPath(path,的GradientPaint);
//画出灰暗
canvas.drawPath(grayPath,grayPaint);
//画出行程!
canvas.drawPath(路径,strokePaint); }
剪切就是我一直在寻找的,是一个更简单的解决方案:
@覆盖
公共无效的onDraw(帆布油画){
路径path =新路径();
浮X1,Y1,X3,Y3,X2,Y2; INT宽度= canvas.getWidth();
INT高度= canvas.getHeight(); gradientPaint.setShader(新的LinearGradient(0,高度,宽度,高度,新的INT [] {Color.RED,Color.YELLOW,Color.GREEN},新的浮动[] {0,0.6f,1},Shader.TileMode。重复)); //在左侧开始,10%最高
X1 = 0;
Y1 =(浮点)(高* 0.90); X2 =(浮点)(宽* .75)在;
Y2 =(浮点)(高* 0.50); X3 =宽度;
Y3 =(浮点)(高* 0.10); //斜坡
path.moveTo(X1,Y1);
path.quadTo(X2,Y2,X3,Y3);
//下
path.lineTo(X3,Y1 + 50);
//背部
path.lineTo(X1,Y1 + 50);
//向上
path.lineTo(X1,Y1); //使用%创建灰色矩形
RECT RECT =新的矩形((INT)(宽* 0.50),0,(INT)X3(INT)Y1 + 50); //它夹
canvas.clipPath(路径); //画出闪亮的填充
canvas.drawPath(路径的GradientPaint);
//画出灰暗
canvas.drawRect(RECT,grayPaint);
//画出行程!
canvas.drawPath(路径,strokePaint); }
I have a shape that I'm drawing with a path. I'm filling that shape in with a gradient and then I need to put another gray area ontop of that gradient dependent upon a %. I'm using path.quadTo to make my shape so I don't know the y coordinate of the top line to properly intersect it. This is what I'm getting when I just set it to the maximum y:
The white stroke is the image I'm trying to partially fill in. The right gray area I want to keep, but I need to get rid of the left gray area. Any ideas? This is what I'm trying so far:
@Override
public void onDraw(Canvas canvas) {
Path path = new Path();
Path grayPath = new Path();
float x1,y1,x3,y3,x2,y2;
float x1g,x2g;
int width = canvas.getWidth();
int height = canvas.getHeight();
gradientPaint.setShader(new LinearGradient(0, height,width,height, new int[]{Color.RED, Color.YELLOW, Color.GREEN}, new float[] {0,0.6f,1}, Shader.TileMode.REPEAT));
x1 = 0;
y1 = (float) (height * .90);
x2 = (float) (width * .75);
y2 = (float) (height * .50);
x3 = width;
y3 = (float) (height * .10);
x2g = (float) (width*.50);
//Ramp
path.moveTo(x1, y1);
path.quadTo(x2, y2, x3, y3);
//Down
path.lineTo(x3, y1+50);
//Back
path.lineTo(x1, y1+50);
//Up
path.lineTo(x1, y1);
//Ramp
grayPath.moveTo(x1, y1);
grayPath.quadTo(x2, y2, x3, y3);
//Down
grayPath.lineTo(x3, y1+50);
//Back
grayPath.lineTo(x2g, y1+50);
//Up
grayPath.lineTo(x2g, y3);
grayPath.setFillType(FillType.WINDING);
//Draw for shiny fill
//canvas.drawPath(path, gradientPaint);
//Draw for grayness
canvas.drawPath(grayPath, grayPaint);
//Draw for stroke!
canvas.drawPath(path, strokePaint);
}
Clipping is what I was looking for and is a much simpler solution:
@Override
public void onDraw(Canvas canvas) {
Path path = new Path();
float x1,y1,x3,y3,x2,y2;
int width = canvas.getWidth();
int height = canvas.getHeight();
gradientPaint.setShader(new LinearGradient(0, height,width,height, new int[]{Color.RED, Color.YELLOW, Color.GREEN}, new float[] {0,0.6f,1}, Shader.TileMode.REPEAT));
//Start at the left side, 10% up
x1 = 0;
y1 = (float) (height * .90);
x2 = (float) (width * .75);
y2 = (float) (height * .50);
x3 = width;
y3 = (float) (height * .10);
//Ramp
path.moveTo(x1, y1);
path.quadTo(x2, y2, x3, y3);
//Down
path.lineTo(x3, y1+50);
//Back
path.lineTo(x1, y1+50);
//Up
path.lineTo(x1, y1);
//Create Gray Rect with %
Rect rect = new Rect((int)(width*.50),0,(int) x3, (int) y1+50);
//CLIP IT
canvas.clipPath(path);
//Draw for shiny fill
canvas.drawPath(path, gradientPaint);
//Draw for grayness
canvas.drawRect(rect, grayPaint);
//Draw for stroke!
canvas.drawPath(path, strokePaint);
}
这篇关于Android在一个路径的一部分填?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!