我正在实施图像裁剪功能。
我正在绘制裁剪矩形并在下面的代码示例中突出显示。这个aproach适用于android 2.x.x.但是使用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);
答案 0 :(得分:1)
尝试在绘制之前调用invalidate()方法。这将更新视图。请参阅此http://developer.android.com/reference/android/view/View.html#invalidate()