How to fill a Path in Android with a linear gradient?

后端 未结 2 481
一生所求
一生所求 2020-12-07 16:30

Given a closed Path object result is like this:

\"enter

Although that is a re

相关标签:
2条回答
  • 2020-12-07 17:21

    While steelbytes' answer will probably give you more control over the individual sections of the gradient, you can do it without the path:

    Paint m_Paint = new Paint();
    protected void onDraw(Canvas canvas)
    {
        super.onDraw(canvas);
        // start at 0,0 and go to 0,max to use a vertical
        // gradient the full height of the screen.
        m_Paint.setShader(new LinearGradient(0, 0, 0, getHeight(), Color.BLACK, Color.WHITE, Shader.TileMode.MIRROR));
        canvas.drawPaint(m_Paint);
    }
    
    0 讨论(0)
  • 2020-12-07 17:22

    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);
    }   
    
    0 讨论(0)
提交回复
热议问题