欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

android开发PathEffect问题处理

程序员文章站 2023-11-17 19:20:22
我要画出一块由不规则path组成的区域,并且这个path因为视觉效果需要设置圆角,我就通过paint.setpatheffect(new cornerpatheffect(...

我要画出一块由不规则path组成的区域,并且这个path因为视觉效果需要设置圆角,我就通过paint.setpatheffect(new cornerpatheffect(300)); 给设置了圆角,但是在通过这个path组合成区域的时候因为这个圆角的设置整个区域都有圆角的效果了,这不是我想要的。

android开发PathEffect问题处理

如图,黑色是我的path,红色是我设置了圆角的由path组成的区域。四个角落都有了圆角的效果,这不是我想要的。

而蓝色区域是未设置圆角的,此时path一侧的区域就不符合标准了。

以下是我的全部代码

public class patheffectview extends view {

  private path line;
  private paint linepaint;


  private path range;
  private paint rangepaint;
  private final paint paint3;

  public patheffectview(context context, @nullable attributeset attrs) {
    super(context, attrs);

    linepaint = new paint(paint.anti_alias_flag);

    linepaint.setstrokewidth(10);
    linepaint.setcolor(color.black);
    linepaint.setstyle(paint.style.stroke);
    linepaint.setpatheffect(new cornerpatheffect(300));

    rangepaint = new paint(linepaint);

    rangepaint.setcolor(color.blue);
    rangepaint.setstyle(paint.style.fill);


    paint3 = new paint(linepaint);
    paint3.setcolor(color.red);

  }

  @override
  protected void onsizechanged(int w, int h, int oldw, int oldh) {
    super.onsizechanged(w, h, oldw, oldh);

    line = new path();
    range = new path();

    line.moveto(0,1000);
    line.rlineto(200,-150);
    line.rlineto(200,300);
    line.rlineto(200,-350);
    line.rlineto(200,320);
    line.rlineto(200,-370);
    line.lineto(w,800);


    range.addpath(line);
    range.lineto(w,h);
    rangepaint.setpatheffect(null);
    range.lineto(0,h);
    range.close();
  }

  @override
  protected void ondraw(canvas canvas) {
    super.ondraw(canvas);
    canvas.save();
    canvas.translate(0,-20);
    canvas.drawpath(line, linepaint);
    canvas.restore();


    canvas.drawpath(range, rangepaint);
    canvas.drawpath(range,paint3);
  }
}