所以我在自定义的onDraw
类中使用View
在RelativeLayout
+ TableLayout
上绘制形状,一切正常,我还有另一个类我使用从A点到B点等绘制线条如下:
我的目标:
如果我将手指从A点(对象A)拖到B点(对象B),如何从画布中删除这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
类中的方法:
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);
}
}
只是简单的假装删除圈子
先谢谢你们..如果需要,可以提供更多代码。
修改:如果我能以其他任何方式完成此操作,请随时分享。 (在网格上绘制圆圈并删除我用手指画出的线条)
答案 0 :(得分:0)
首先尝试将delete()
方法更改为:
public void delete(){
mPaint.setColor(Color.TRANSPARENT);
invalidate();
}
您需要让View
知道您希望自己重绘(这就是invalidate()
致电的原因。)