【问题标题】:Selecting the right path objects and move it选择正确的路径对象并移动它
【发布时间】:2014-01-18 18:37:45
【问题描述】:

我在数组列表中存储了一些路径对象。我在尝试根据触摸的坐标 x、y 选择正确的路径对象并在此之后在画布周围移动路径对象时遇到问题。

我现在可以选择用户的触摸坐标。 Android 的路径删除了 contains 方法,因此我无法使用 .contains(x,y) 来确定正确的路径对象并移动它。

另外,由于我的路径对象不仅仅是矩形对象,我也不能使用 .computebounds。

这是我的 onTouch 方法:

 @Override
 public boolean onTouchEvent(MotionEvent event) {
     // TODO Auto-generated method stub
        switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN:
            // screen touch get x of the touch event
            x = event.getX();
            // screen touch get y of the touch event
            y = event.getY();

            break;

        }

        invalidate();

     return true;
 }

现在我在从数组列表中选择包含屏幕触摸 x 和 y 的路径时遇到问题。我知道的唯一方法是 .contains(x,y) 在这种情况下不起作用。

【问题讨论】:

  • 给我们看一些代码。到目前为止,您尝试过什么?
  • @Robin Dijkhof 我已经添加了我的代码。

标签: android object arraylist path


【解决方案1】:

我会使用保存在 ArrayList 中的点

@Override
 public boolean onTouchEvent(MotionEvent event) {
 // TODO Auto-generated method stub
    switch (event.getAction()) {
    case MotionEvent.ACTION_DOWN:
        // screen touch point of the touch event
        myPointArrayList.add(event.getX(), event.getY());



        break;

    }

    invalidate();

 return true;
}

您可以通过遍历列表来检查位置。

for(int i = 0; i<myPointArrayList.size(); i++){
    if(myPointArrayList.get(i).getX() == myIntX && myPointArrayList.get(i).getY() == myIntY){
        doSomething();
    }
}

很遗憾,Android 点类不包含 getX。这意味着我们需要创建自己的点类。

public class MyPoint{
    private float x;
    private float y;

    public MyPoint(float x, float y){
        this.x = x;
        this.y = y;
    }

    public float getX(){
        return x;
    }
    public float getY(){
        return y;
    }
}

也可以创建一个新点并进行比较。

for(int i = 0; i<myPointArrayList.size(); i++){
    if(myPointArrayList.get(i).equals(new PointF(myIntX, myIntY)){
        doSomething();
    }
}

我会选择第二个选项,因为它可以节省一些时间。

【讨论】:

  • 我试过了,但我得到了这个错误“方法 getX() 未定义类型 ArrayList
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-03-07
  • 1970-01-01
  • 2011-09-03
  • 2011-08-26
  • 2013-08-26
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多