2

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

}

4

2 に答える 2

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);
}
于 2013-03-09T07:46:30.093 に答える
0

残念ながら、リージョンの交差点、和集合などは、アンチエイリアス処理されていないパスを生成します。

これで何をしたいかに応じて、同じパスに図形を追加することができますが、パスをに設定します

myPath.setFillType(FillType.EVEN_ODD);

このように描くと、形に「穴」を作ることができます。

于 2013-03-08T10:37:37.813 に答える