Android填写部分路径?

时间:2014-08-25 14:26:44

标签: android graphics drawing

我的形状是我用路径画的。我用渐变填充该形状然后我需要在该梯度的顶部放置另一个灰色区域,取决于%。我使用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);

    }

1 个答案:

答案 0 :(得分:2)

剪辑是我正在寻找的,是一个更简单的解决方案:

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

    }