Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/entity-framework/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
android中两条路径的实际交点(新路径)_Android_Canvas_Drawing - Fatal编程技术网

android中两条路径的实际交点(新路径)

android中两条路径的实际交点(新路径),android,canvas,drawing,Android,Canvas,Drawing,我尝试在Android中相交两条路径。这在第一次尝试中效果很好。当我放大绘图(canvas.scale())时,相交的形状是不精确和难看的。 有人知道如何解决我的问题吗 我喜欢把红色的正方形和蓝色的矩形相交,它在红色的正方形里面。 在我的代码中,缩放时的结果是不精确的绿色交点: 这是我的密码: protected void onDraw(Canvas canvas) { super.onDraw(canvas); canvas.scale(20,20); Paint paint1 = ne

我尝试在Android中相交两条路径。这在第一次尝试中效果很好。当我放大绘图(canvas.scale())时,相交的形状是不精确和难看的。 有人知道如何解决我的问题吗

我喜欢把红色的正方形和蓝色的矩形相交,它在红色的正方形里面。

在我的代码中,缩放时的结果是不精确的绿色交点:

这是我的密码:

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

}

不幸的是,区域交点、并集等会生成非抗锯齿路径

取决于您想用它做什么,您只需将形状添加到同一路径,但将路径设置为

myPath.setFillType(FillType.EVEN_ODD);
通过这种方式,当你绘制它时,你可以在形状上创建一个“孔”。

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

这是我的代码:

    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);
}
Path mypath=newpath();
字符串spatterType=cpath.getsPattern();
Path pathtempforbounds=新路径(cpath.getPath());
RectF RectF=新的RectF();
if(飞溅类型等于(“1”)){
转弯路径(pathtempforbounds,-45);
}
pathtempforbounds.computeBounds(rectF,true);
浮动ftop=rectF.top;
浮动fbottom=矩形底部;
float-fleft=rectF.left;
浮动恐惧=右旋;
float X长度=fright fleft;
路径模式=新路径();
浮动ypos=ftop;
浮点数xpos=fleft;
浮动fStreifenbreite=4f;

虽然(我非常感谢你。但是这没有帮助。我对路径有了更多的了解,我猜:-)当使用moveto、lineto等绘制路径时。。。创建了一个位图,它在缩放时看起来很难看。我发布了我的解决方案,在交叉点之前进行缩放。这是一个好主意,唯一的问题是区域交叉点是如何工作的,即它是否创建了一个临时位图,在您的情况下,它可以比屏幕大10倍?如果没有,那么我也可以使用这个解决方案来平滑边缘。真正的大问题是如何提取这个结果路径的点并保存它,我在这里尝试:您的解决方案解决了我的问题。因此,如果你在我上面发布的链接中发布这个答案,我将奖励你300分!