【发布时间】:2016-06-20 00:42:22
【问题描述】:
所以我在自定义 View 类中使用 onDraw 在 RelativeLayout + TableLayout 上绘制形状,一切正常,我还有另一个类用于绘制从 A 点到点的线B等例子如下:
我的目标:
如果我将手指从 A 点 (objectA) 拖动到 B(objectB) 点,如何从画布中删除这 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);
}
}
只是一些简单的假删除圈子
在此先感谢各位。如果需要,可以提供更多代码。
编辑:如果我能以任何其他方式完成此操作,请随时分享。 (在网格上画圆圈并删除我用手指画线的圆圈)
【问题讨论】:
-
objectA和objectB是什么?delete方法究竟是什么? -
@BartekLipinski - 我很抱歉。每个圆圈都是 DotView 类的表示。我更新了问题的更多细节......如果仍然不清楚,请告诉我。
标签: java android canvas motionevent