【问题标题】:Android fill in part of a path?Android填写路径的一部分?
【发布时间】:2014-08-25 14:26:43
【问题描述】:

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

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

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

    }

【问题讨论】:

    标签: android graphics drawing


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 2023-04-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-01
      • 1970-01-01
      • 2019-09-11
      • 1970-01-01
      相关资源
      最近更新 更多