0

我正在画圆圈,并希望当线条匹配在一起时会产生错误。我已经做了一些编码,这段代码能够在触摸时画线,但问题是当线条匹配在一起时它无法产生错误。所以有人可以建议我吗我做错了什么。

代码

      public class DrawView extends View implements View.OnTouchListener {
        private static final String TAG = "DrawView";

        List<Point> points = new ArrayList<Point>();
        Paint paint = new Paint();

        public DrawView(Context context) {
            super(context);
            setFocusable(true);
            setFocusableInTouchMode(true);

            DrawView.this.setOnTouchListener(DrawView.this);

            paint.setColor(Color.WHITE);
            paint.setAntiAlias(true);
        }

        public void onDraw(Canvas canvas) {
            for (Point point : points) {
                canvas.drawCircle(point.x, point.y, 5, paint);
                // canvas.drawLine(point.x, point.y, 5, 0, paint);
                // canvas.drawPaint(p);
                // Log.d(TAG, "Painting: "+point);
            }
        }

        public boolean onTouch(View view, MotionEvent event) {
            // if(event.getAction() != MotionEvent.ACTION_DOWN)
            // return super.onTouchEvent(event);
            Point point = new Point();
            point.x = event.getX();
            point.y = event.getY();
            for (int i = 0; i < points.size(); i++) {
                if (point.equals(points.get(i))) {
                    System.out
                            .println("*****************************ERROR*******************************************");
                } else {
                    System.out.println("Value of points" + points);
                    System.out
                            .println("***********************ELSE***********************************");
                }
            }
            points.add(point);
            invalidate();
            Log.d(TAG, "point: " + point);
            return true;
        }
    }

   class Point {
    float x, y;

    public boolean equals(Object o) {
        if (!(o instanceof Point))
            return false;
        Point p = (Point) o;
        return x == p.x && y == p.y;
    }

    public String toString() {
        return x + ", " + y;
    }
    }
4

1 回答 1

0

您需要为您的 Point 类实现 equals() 方法。默认实现可能不会比较您的 x 和 y 属性。

于 2012-07-05T08:10:42.370 回答