Android 用“Region.Op.REPLACE”替代不推荐使用的“canvas.clipRect”?
我有以下代码:Android 用“Region.Op.REPLACE”替代不推荐使用的“canvas.clipRect”?,android,canvas,clip,Android,Canvas,Clip,我有以下代码: 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.tmpR
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);
}
但是自从安卓SDK28之后,clipRect的实现就被弃用并退出了。
我花了4个小时试图找到一种方法,用其他方法来代替这个方法,但没有成功
既然clipRect已被弃用且不再工作,我如何实现相同的结果?我自己发现:
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();
}
不清楚这个.tmpRect()是什么,但您可能只需要使用
Canvas.save()/restore()
。