【问题标题】:Alternative to deprecated `canvas.clipRect` with `Region.Op.REPLACE`?用 `Region.Op.REPLACE` 替代已弃用的`canvas.clipRect`?
【发布时间】:2020-04-03 19:19:36
【问题描述】:

我有以下代码:

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

标签: android canvas clip


【解决方案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();
    }

【讨论】:

    猜你喜欢
    • 2016-01-17
    • 2020-03-03
    • 2013-04-28
    • 2012-12-01
    • 2012-12-03
    • 2020-03-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多