5

私のプロジェクトでは、画面全体をビットマップで埋めています。このビットマップで、パスを描画します

android.graphics.Canvas.drawPath(Path path, Paint paint)

ペイントは、パスのコンテンツをストロークして塗りつぶすために設定されます。私が達成することは、パスと交差するビットアンプの部分を消去することです。パスの代わりに別のビットマップを使用し、ポーター ダフ ルールを使用して、同じ動作を得ることができました。パスで同じことをする機会はありますか?

    mPaintPath.setARGB(100, 100, 100, 100);// (100, 100, 100, 100)
    mPaintPath.setStyle(Paint.Style.FILL_AND_STROKE);
    mPaintPath.setAntiAlias(true);
    mPath.moveTo(x0, y0));
    mPath.lineTo(x1, y1);
    mPath.lineTo(x2, y2);
    mPath.lineTo(x3, y3);
    mPath.lineTo(x0, y0);
    mPath.close();
    c.drawPath(mPath, mPaintPath);
4

1 に答える 1

7

もちろん、オフスクリーン バッファへのパスを描画するだけで、次のようにビットマップを描画するときにマスクとして使用できます。

// Create an offscreen buffer
int layer = c.saveLayer(0, 0, width, height, null,
        Canvas.HAS_ALPHA_LAYER_SAVE_FLAG | Canvas.FULL_COLOR_LAYER_SAVE_FLAG);

// Setup a paint object for the path
mPaintPath.setARGB(255, 255, 255, 255);
mPaintPath.setStyle(Paint.Style.FILL_AND_STROKE);
mPaintPath.setAntiAlias(true);

// Draw the path onto the offscreen buffer
mPath.moveTo(x0, y0);
mPath.lineTo(x1, y1);
mPath.lineTo(x2, y2);
mPath.lineTo(x3, y3);
mPath.lineTo(x0, y0);
mPath.close();
c.drawPath(mPath, mPaintPath);

// Draw a bitmap on the offscreen buffer and use the path that's already
// there as a mask
mBitmapPaint.setXfermode(new PorterDuffXfermode(Mode.SRC_OUT));
c.drawBitmap(mBitmap, 0, 0, mBitmapPaint);

// Composit the offscreen buffer (a masked bitmap) to the canvas
c.restoreToCount(layer);

エイリアシングに耐えることができる場合は、より簡単な方法があります: クリップ パスを設定するだけです (これを使用するRegion.Op.DIFFERENCEと、パスの外側のすべてが切り取られるのではなく、パスの内側が切り取られることに注意してください)。

// Setup a clip path
mPath.moveTo(x0, y0);
mPath.lineTo(x1, y1);
mPath.lineTo(x2, y2);
mPath.lineTo(x3, y3);
mPath.lineTo(x0, y0);
mPath.close();
c.clipPath(mPath, Op.DIFFERENCE);

// Draw the bitmap using the path clip
c.drawBitmap(mBitmap, 0, 0, mBitmapPaint);
于 2011-11-28T16:11:15.793 に答える