android中的2个路径的实际交集(新路径)

时间:2013-03-07 10:08:40

标签: android canvas drawing

我尝试在Android中交叉2个路径。这在第一次尝试中工作正常。当我缩放到绘图(canvas.scale())时,相交的形状是不可思议和丑陋的。 有谁知道如何解决我的问题?

我喜欢将红场与蓝色矩形相交,红色矩形位于红场内。 enter image description here

使用我的代码时,结果是在缩放时不引人注意的绿色交叉点:

enter image description here

这是我的代码:

protected void onDraw(Canvas canvas) {
super.onDraw(canvas);

canvas.scale(20,20);
Paint paint1 = new Paint();
paint1.setColor(Color.RED); paint1.setAntiAlias(true);
Path path=new Path();
path.moveTo(10, 10); path.lineTo(20, 10); path.lineTo(20, 20); path.lineTo(10, 20); path.close();
//canvas.drawPath(path, paint1);
Path path2=new Path();
path2.moveTo(15, 10); path2.lineTo(20, 10); path2.lineTo(20, 20); path.close();
paint1.setColor(Color.BLUE);
//canvas.drawPath(path2, paint1);

Region clip = new Region();
    clip.set((int)0,(int)0, (int)300,(int)300);
Region region1 = new Region();
region1.setPath(path, clip);
Region region2 = new Region();
region2.setPath(path2, clip);
region1.op(region2, Region.Op.INTERSECT);

Path pnew=region1.getBoundaryPath();
paint1.setColor(Color.GREEN);
canvas.drawPath(pnew, paint1);

}

2 个答案:

答案 0 :(得分:1)

我的解决方案如下。 在我与我的路径交叉之前,我将它们放大。交叉后我缩小它们。我不知道这是不是一个好的解决方案,但它对我有用。它也很快。在我的例子中,我将圆形路径与自己创建的图案(条纹)相交。缩放时看起来仍然很好: - )

enter image description here

enter image description here

这是我的代码:

    Path mypath=new Path(<desiredpath to fill with a pattern>);
    String sPatternType=cpath.getsPattern();

    Path pathtempforbounds=new Path(cpath.getPath());
    RectF rectF = new RectF();
     if (sPatternType.equals("1")){
         turnPath(pathtempforbounds, -45);
     }
     pathtempforbounds.computeBounds(rectF, true);

     float ftop=rectF.top;
     float fbottom=rectF.bottom;
     float fleft=rectF.left;
     float fright=rectF.right;
     float xlength=fright-fleft;

     Path pathpattern=new Path();

     float ypos=ftop;
     float xpos=fleft;

     float fStreifenbreite=4f;

     while(ypos<fbottom){
         pathpattern.moveTo(xpos,ypos);
         xpos=xpos+xlength;
         pathpattern.lineTo(xpos, ypos);
         ypos=ypos+fStreifenbreite;
         pathpattern.lineTo(xpos, ypos);
         xpos=xpos-xlength;
         pathpattern.lineTo(xpos, ypos);
         ypos=ypos-fStreifenbreite;
         pathpattern.lineTo(xpos, ypos);
         pathpattern.close();
         ypos=ypos+2*fStreifenbreite;

     }

     // Original vergrössern

     scalepath(pathpattern,10);
     scalepath(mypath,10);

     if (sPatternType.equals("1")){
         Matrix mdrehen=new Matrix();
         RectF bounds=new RectF();
         pathpattern.computeBounds(bounds, true);
         mdrehen.postRotate(45, (bounds.right + bounds.left)/2,(bounds.bottom + bounds.top)/2);
         pathpattern.transform(mdrehen);
     }

     RectF rectF2 = new RectF();
     mypath.computeBounds(rectF2, true);

     Region clip = new Region();
     clip.set((int)(rectF2.left-100f),(int)(rectF2.top -100f), (int)(rectF2.right+100f),(int)( rectF2.bottom+100f));
     Region region1 = new Region();
     region1.setPath(pathpattern, clip);

     Region region2 = new Region();
     region2.setPath(mypath, clip);

     region1.op(region2, Region.Op.INTERSECT);


     Path pnew=region1.getBoundaryPath();


     scalepath(pnew, 0.1f);
     cpath.setPathpattern(pnew);




public void turnPath(Path p,int idegree){
     Matrix mdrehen=new Matrix();
     RectF bounds=new RectF();
     p.computeBounds(bounds, true);
     mdrehen.postRotate(idegree, (bounds.right + bounds.left)/2,(bounds.bottom + bounds.top)/2);
     p.transform(mdrehen);
}

public void scalepath(Path p,float fscale){
     Matrix mverkleinern=new Matrix();
     mverkleinern.preScale(fscale,fscale);
     p.transform(mverkleinern);
}

答案 1 :(得分:0)

遗憾的是,区域交集,union等产生了一个非反锯齿路径!

取决于你想要做什么,你可以只添加形状到同一路径,但设置路径

myPath.setFillType(FillType.EVEN_ODD);

以这种方式绘制时,可以在形状中创建“洞”。