![](/img/trans.png)
[英]Android UnsupportedOperationException at Canvas.clipPath
[英]Canvas.clipPath(Path) not clipping as expected
我正在嘗試將畫布繪制操作剪切成弧形楔形。 但是,在將剪切路徑設置為畫布后,我沒有得到預期的結果。
為了說明,這是我正在做的事情:
path.reset();
//Move to point #1
path.moveTo(rect.centerX(), rect.centerY());
//Per the documentation, this will draw a connecting line from the current
//position to the starting position of the arc (at 0 degrees), add the arc
//and my current position now lies at #2.
path.arcTo(rect, 0, -30);
//This should then close the path, finishing back at the center point (#3)
path.close();
這是有效的,當我簡單地繪制這個路徑( canvas.drawPath(path, paint)
)時,它會繪制如上所示的楔形。 但是,當我將此路徑設置為畫布的剪切路徑並將其繪制到其中時:
//I've tried it with and without the Region.Op parameter
canvas.clipPath(path, Region.Op.REPLACE);
canvas.drawColor(Color.BLUE);
我得到以下結果(僅留下楔形以顯示參考):
所以它似乎剪切到Path
的邊界矩形,而不是Path
本身。 有什么想法在這里發生了什么?
編輯就像更新一樣,我發現了一種更有效的方法,可以實現硬件加速。 首先,將整個圖像(您要裁剪)繪制到屏幕外位圖中。 使用此Bitmap
創建一個BitmapShader
,將該着色器設置為Paint
,然后使用該Paint
對象繪制楔形路徑:
drawMyBitmap(bitmap);
Shader shader = new BitmapShader(bitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
paint.setShader(shader);
@Override
public void onDraw(Canvas canvas) {
canvas.drawArc(rect, //The rectangle bounding the circle
startAngle, //The angle (CW from 3 o'clock) to start
sweepAngle, //The angle (CW from 3 o'clock) of the arc
true, //Boolean of whether to draw a filled arc (wedge)
paint //The paint with the shader attached
);
}
您使用的是HC還是以上或其他使用硬件加速?
如果是這樣,則不支持clipPath並且存在問題。
developer.android.com/guide/topics/graphics/hardware-accel.html 。
OP的問題是關於使用裁剪區域的問題,@Simon回答了這個問題。 但請記住,有一種更簡單的繪制填充弧的方法:
創建一個Paint
:
mPaint = new Paint();
mPaint.setColor(Color.BLUE);
mPaint.setStyle(Style.FILL);
mPaint.setAntiAlias(true);
繪圖時,只需繪制路徑:
canvas.drawPath(path, mPaint);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.