gpt4 book ai didi

Android填写路径的一部分?

转载 作者:行者123 更新时间:2023-11-29 21:02:49 25 4
gpt4 key购买 nike

我有一个正在绘制的带有路径的形状。我正在用渐变填充该形状,然后我需要根据 % 在该渐变之上放置另一个灰色区域。我正在使用 path.quadTo 来制作我的形状,所以我不知道顶线的 y 坐标是否正确地与它相交。这是我将它设置为最大 y 时得到的结果:

Screenshot

白色笔划是我要部分填充的图像。我想保留右边的灰色区域,但我需要去掉左边的灰色区域。有任何想法吗?这是我目前正在尝试的:

@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);

}

最佳答案

裁剪是我一直在寻找的并且是一个更简单的解决方案:

@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填写路径的一部分?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25488151/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com