我可以使用moveTo()
上的lineTo()
和Canvas
命令绘制一个矩形。我现在要做的是裁剪位于此正方形内的位图。
这是我的onDraw()
方法:
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
drawPath.moveTo(topLeft.x, topLeft.y);
drawPath.lineTo(topRight.x, topRight.y);
drawPath.lineTo(bottomRight.x, bottomRight.y);
drawPath.lineTo(bottomLeft.x, bottomLeft.y);
drawPath.lineTo(topLeft.x, topLeft.y);
drawCanvas = new Canvas(canvasBitmap);
canvas.drawPath(drawPath, drawPaint);
canvas.drawBitmap(canvasBitmap, 0, 0, canvasPaint);
}
答案 0 :(得分:0)
试试这个:
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
drawPath.moveTo(topLeft.x, topLeft.y);
drawPath.lineTo(topRight.x, topRight.y);
drawPath.lineTo(bottomRight.x, bottomRight.y);
drawPath.lineTo(bottomLeft.x, bottomLeft.y);
drawPath.lineTo(topLeft.x, topLeft.y);
canvas.clipPath(drawPath);
canvas.drawBitmap(canvasBitmap, 0, 0, canvasPaint);
}
如果drawPath
始终是矩形,则可以使用Canvas.clipRect()
。以下是使用clipRect()
重写的相同代码:
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.clipRect(left, top, right, bottom);
canvas.drawBitmap(canvasBitmap, 0, 0, canvasPaint);
}