Canvas;如何追溯删除形状
Canvas; How to remove shapes retroactively
所以我在自定义 View
class 中使用 onDraw
在 RelativeLayout
+ TableLayout
上绘制形状一切正常,我还有另一个 class 用来画从 A 点到 B 点等的线。示例如下:
我的目标:
如果我将手指从 A 点(objectA)拖动到 B 点(objectB),如何从 canvas 中删除这 2 个视图对象?我添加了一个方法:
objectA.delete();
objectB.delete();
当我拖动手指通过 MotionEvent
时应该删除 A 和 B,但它只删除一个而没有删除另一个,所以我认为它不具有追溯力?
以下代码:
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
/// get the child that corresponds to the first touch
DotView objectA = getChildForTouch((TableLayout) v, x, y);
return true;
case MotionEvent.ACTION_MOVE:
///used the x - y to get the object for every other shape the user`S finger passes over.
DotView objectB = getChildForTouch((TableLayout) v, x, y);
/// just update positions
line.setCoords(mStartX, mStartY, (int) x, (int) y);
objectA.delete(); ///Delete first shape
objectB.delete(); ///Delete second shape
break;
case MotionEvent.ACTION_UP:
///Gets the last shape where the user released their fingers
endView = getChildForTouch((TableLayout) v, x, y);
break;
里面的删除方法:DotView extends View
class:
private static class DotView extends View {
private static final int DEFAULT_SIZE = 100;
private Paint mPaint = new Paint();
private Rect mBorderRect = new Rect();
private Paint mCirclePaint = new Paint();
private int mRadius = DEFAULT_SIZE / 4;
public DotView(Context context) {
super(context);
mPaint.setStrokeWidth(2.0f);
mPaint.setStyle(Style.STROKE);
mPaint.setColor(Color.RED);
mCirclePaint.setColor(Color.CYAN);
}
@Override
protected void onDraw(Canvas canvas) {
canvas.drawColor(Color.parseColor("#0099cc"));
mBorderRect.left = 0;
mBorderRect.top = 0;
mBorderRect.right = getMeasuredWidth();
mBorderRect.bottom = getMeasuredHeight();
canvas.drawRect(mBorderRect, mPaint);
canvas.drawCircle(getMeasuredWidth() / 2, getMeasuredHeight() / 2,
mRadius, mCirclePaint);
}
public void delete(){
mPaint.setColor(Color.TRANSPARENT);
}
}
只是一些简单的假删除圈子
提前谢谢大家..如果需要可以提供更多代码。
编辑: 如果我能以任何其他方式完成此任务,请随时分享。 (在网格上画圆圈并删除我用手指画线的圆圈)
首先尝试将您的 delete()
方法更改为:
public void delete(){
mPaint.setColor(Color.TRANSPARENT);
invalidate();
}
您需要让您的 View
知道您希望它自己重绘(这就是 invalidate()
调用的原因)。
所以我在自定义 View
class 中使用 onDraw
在 RelativeLayout
+ TableLayout
上绘制形状一切正常,我还有另一个 class 用来画从 A 点到 B 点等的线。示例如下:
我的目标:
如果我将手指从 A 点(objectA)拖动到 B 点(objectB),如何从 canvas 中删除这 2 个视图对象?我添加了一个方法:
objectA.delete();
objectB.delete();
当我拖动手指通过 MotionEvent
时应该删除 A 和 B,但它只删除一个而没有删除另一个,所以我认为它不具有追溯力?
以下代码:
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
/// get the child that corresponds to the first touch
DotView objectA = getChildForTouch((TableLayout) v, x, y);
return true;
case MotionEvent.ACTION_MOVE:
///used the x - y to get the object for every other shape the user`S finger passes over.
DotView objectB = getChildForTouch((TableLayout) v, x, y);
/// just update positions
line.setCoords(mStartX, mStartY, (int) x, (int) y);
objectA.delete(); ///Delete first shape
objectB.delete(); ///Delete second shape
break;
case MotionEvent.ACTION_UP:
///Gets the last shape where the user released their fingers
endView = getChildForTouch((TableLayout) v, x, y);
break;
里面的删除方法:DotView extends View
class:
private static class DotView extends View {
private static final int DEFAULT_SIZE = 100;
private Paint mPaint = new Paint();
private Rect mBorderRect = new Rect();
private Paint mCirclePaint = new Paint();
private int mRadius = DEFAULT_SIZE / 4;
public DotView(Context context) {
super(context);
mPaint.setStrokeWidth(2.0f);
mPaint.setStyle(Style.STROKE);
mPaint.setColor(Color.RED);
mCirclePaint.setColor(Color.CYAN);
}
@Override
protected void onDraw(Canvas canvas) {
canvas.drawColor(Color.parseColor("#0099cc"));
mBorderRect.left = 0;
mBorderRect.top = 0;
mBorderRect.right = getMeasuredWidth();
mBorderRect.bottom = getMeasuredHeight();
canvas.drawRect(mBorderRect, mPaint);
canvas.drawCircle(getMeasuredWidth() / 2, getMeasuredHeight() / 2,
mRadius, mCirclePaint);
}
public void delete(){
mPaint.setColor(Color.TRANSPARENT);
}
}
只是一些简单的假删除圈子
提前谢谢大家..如果需要可以提供更多代码。
编辑: 如果我能以任何其他方式完成此任务,请随时分享。 (在网格上画圆圈并删除我用手指画线的圆圈)
首先尝试将您的 delete()
方法更改为:
public void delete(){
mPaint.setColor(Color.TRANSPARENT);
invalidate();
}
您需要让您的 View
知道您希望它自己重绘(这就是 invalidate()
调用的原因)。