【问题标题】:real intersection of 2 paths in android ( new path )android中2条路径的真正交集(新路径)
【发布时间】:2013-03-07 10:08:39
【问题描述】:

我尝试在 Android 中与 2 条路径相交。这在第一次尝试时效果很好。当我放大绘图( 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);

}

【问题讨论】:

    标签: android canvas drawing


    【解决方案1】:

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

    这是我的代码:

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

    【讨论】:

    • 这是个好主意,唯一的问题是区域交叉点是如何工作的,即它是否会创建一个临时位图,在您的情况下可以比屏幕大 10 倍?如果不是,那么我也可以使用此解决方案来平滑边缘。真正的大问题是如何提取这个结果路径的点并保存它,我在这里尝试:gamedev.stackexchange.com/questions/49239/…
    • 您的解决方案解决了我的问题。所以如果你把这个答案贴在我上面贴的链接里,我会奖励你300分!
    【解决方案2】:

    不幸的是,区域交点、联合等会产生非抗锯齿路径!

    取决于你想用这个做什么,你可以将形状添加到相同的路径,但将路径设置为

    myPath.setFillType(FillType.EVEN_ODD);
    

    这样在绘制的时候,就可以在形状上创建一个“洞”。

    【讨论】:

    • 非常感谢。但这没有帮助。我对路径了解更多,我猜 :-) 当使用 moveto、lineto 等绘制路径时...创建了一个位图,在缩放时看起来很难看。我在交叉路口前发布了我的解决方案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-26
    • 1970-01-01
    • 2017-08-10
    • 1970-01-01
    相关资源
    最近更新 更多