【问题标题】:Canvas; How to remove shapes retroactively帆布;如何追溯删除形状
【发布时间】:2016-06-20 00:42:22
【问题描述】:

所以我在自定义 View 类中使用 onDrawRelativeLayout + 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);
        }

    }

只是一些简单的假删除圈子

在此先感谢各位。如果需要,可以提供更多代码。

编辑:如果我能以任何其他方式完成此操作,请随时分享。 (在网格上画圆圈并删除我用手指画线的圆圈)

【问题讨论】:

  • objectAobjectB 是什么? delete 方法究竟是什么?
  • @BartekLipinski - 我很抱歉。每个圆圈都是 DotView 类的表示。我更新了问题的更多细节......如果仍然不清楚,请告诉我。

标签: java android canvas motionevent


【解决方案1】:

首先尝试将您的 delete() 方法更改为:

public void delete(){
    mPaint.setColor(Color.TRANSPARENT);
    invalidate();
}

您需要让您的View 知道您希望它自己重绘(这就是invalidate() 调用的原因)。

【讨论】:

  • 嗯,有道理。现在我想离开工作回家测试一下。几个小时后我会给你一个更新。
猜你喜欢
  • 1970-01-01
  • 2020-11-24
  • 2012-02-15
  • 2019-04-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多