0
我正在实现图像裁剪功能。Android ImageView.draw()留下痕迹
我正在绘制裁剪矩形并突出显示,如下面的代码示例。这个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);
我可以发誓我做了无效之前绘图:) –