0

我正在实现图像裁剪功能。

我正在绘制裁剪矩形并突出显示,如下面的代码示例所示。这种方法适用于 android 2.xx 但在 4.x 中我不得不关闭硬件加速,因为Canvas.clipPath没有按预期工作。

之后,我得到了这些踪迹。我对画布有点陌生,我会很感激任何帮助。

代码示例

Rect viewDrawingRect = new Rect();
imageView.getDrawingRect(viewDrawingRect);

path.addRect(new RectF(drawRect), Path.Direction.CW);
canvas.clipPath(path, Region.Op.DIFFERENCE);
canvas.drawRect(viewDrawingRect, hasFocus() ? focusPaint : noFocusPaint);

canvas.restore();
canvas.drawPath(path, outlinePaint);

int left = drawRect.left;
int right = drawRect.right;
int top = drawRect.top;
int bottom = drawRect.bottom;

int widthWidth = resizeDrawableWidth.getIntrinsicWidth() / 2;
int widthHeight = resizeDrawableWidth.getIntrinsicHeight() / 2;
int heightHeight = resizeDrawableHeight.getIntrinsicHeight() / 2;
int heightWidth = resizeDrawableHeight.getIntrinsicWidth() / 2;

int xMiddle = drawRect.left + ((drawRect.right - drawRect.left) / 2);
int yMiddle = drawRect.top + ((drawRect.bottom - drawRect.top) / 2);

resizeDrawableWidth.setBounds(left - widthWidth,
    yMiddle - widthHeight,
    left + widthWidth,
    yMiddle + widthHeight);
resizeDrawableWidth.draw(canvas);

resizeDrawableWidth.setBounds(right - widthWidth,
    yMiddle - widthHeight,
    right + widthWidth,
    yMiddle + widthHeight);
resizeDrawableWidth.draw(canvas);

resizeDrawableHeight.setBounds(xMiddle - heightWidth,
    top - heightHeight,
    xMiddle + heightWidth,
    top + heightHeight);
resizeDrawableHeight.draw(canvas);

resizeDrawableHeight.setBounds(xMiddle - heightWidth,
    bottom - heightHeight,
    xMiddle + heightWidth,
    bottom + heightHeight);
resizeDrawableHeight.draw(canvas);

拖动时的轨迹

4

1 回答 1

1

尝试在绘制之前调用 invalidate() 方法。这将更新视图。请参考这个http://developer.android.com/reference/android/view/View.html#invalidate()

于 2013-05-31T07:08:34.173 回答