`canvas.clipRect`中`Region.Op.REPLACE`已被弃用,有什么替代方案?

4

我有以下代码:

 private RectF tmpRect = new RectF();

 public void drawClipedBitmap(Bitmap bmp, Polygon tmpPoly, int x, int y) {
    this.canvas.clipPath(getPath(tmpPoly));
    this.canvas.drawBitmap(bmp, (float) x, (float) y, this.fillPaint);
    this.tmpRect.set(0.0f, 0.0f, (float) this.canvas.getWidth(), (float) this.canvas.getHeight());
    this.canvas.clipRect(this.tmpRect, Op.REPLACE);
}

但自从 Android SDK28 开始,clipRect 的实现已被弃用并移除了。

我试了4个小时尝试找到一个替代该方法的解决方案,但一直没有成功。

有什么方法可以实现相同的效果,因为 clipRect 已被弃用且不再可用?


不清楚 this.tmpRect() 是什么,但你可能只需使用 Canvas.save()/restore() - greeble31
1个回答

1

我已经自己找到了:

public void drawClipedBitmap(Bitmap bmp, Polygon tmpPoly, int x, int y) {
        Path p = getPath(tmpPoly);
        Log.d("POINTS","POINTS: "+p);
        this.canvas.clipPath(getPath(tmpPoly));
        this.canvas.drawBitmap(bmp, (float) x, (float) y, this.fillPaint);
        this.tmpRect.set(0.0f, 0.0f, (float) this.canvas.getWidth(), (float) this.canvas.getHeight());
        //this.canvas.clipRect(this.tmpRect, Op.REPLACE);
        this.canvas.save();
        float fWidth = (float)this.canvas.getWidth();
        float fHeight = (float)this.canvas.getHeight();
        //this.canvas.drawRect(0, 0, fWidth, fHeight, new Paint());
        this.canvas.clipRect(0.0f, 0.0f,  fWidth, fHeight);
        this.canvas.restore();
    }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接