我試圖將畫布繪製操作剪裁成弧形的楔形。但是,在將剪切路徑設置爲畫布後,我沒有得到預期的結果。Canvas.clipPath(路徑)不按預期裁剪
爲了說明,下面是我在做什麼:
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
本身。任何想法發生了什麼?
編輯就像更新一樣,我發現了一個更有效的方法來實現硬件加速。首先,將整個圖像(您將要裁剪的)繪製到離屏位圖中。做一個BitmapShader
使用此Bitmap
,該着色器設置爲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
);
}
是否使用
一個
Paint
創建慧聰或以上或以其他方式使用硬件加速? http://developer.android.com/guide/topics/graphics/hardware-accel.html。如果是這樣,clipPath不受支持並且存在問題。 – Simon@Simon:噢,天哪。嘿。不幸的是,本週我一直在提及這份文件,但我完全忽略了這一點。禁用硬件加速完美工作!如果您將此作爲答案發布,我會接受它。你是一個救星! – kcoppock
很高興有幫助。祝你好運。 – Simon