【问题标题】:Android - Getting the position that was touched and moving a view to that positionAndroid - 获取被触摸的位置并将视图移动到该位置
【发布时间】:2015-01-13 11:38:16
【问题描述】:

我有点想复制 Google 材料设计中的涟漪效应。现在,我注意到波纹总是从被触摸的位置开始。为此,我创建了一个drawable,它最初是不可见的,但一旦触摸视图就会变得可见。然后,包含drawableImageView 将移动到被触摸的位置,然后波纹动画将开始。现在,在尝试执行此操作时,我偶然发现了一件事,即我无法弄清楚如何找出被触摸的位置。我在stackoverflow上遇到了几个问题,说可以使用以下代码找到触摸位置。

@Override
public boolean onTouch(View v, MotionEvent e) {

      int X = (int)(e.getX());
      int Y = (int)(e.getY());

      return false;
}

我确实尝试在我的应用程序中实现此代码,但最终得到了NullPointerException。请告诉我如何找到被触摸的屏幕的确切位置并将视图移动到那里?

【问题讨论】:

标签: android android-layout android-animation


【解决方案1】:

这样使用:

@Override
public boolean onTouch(View v, MotionEvent e) {
  if(e.getAction() == MotionEvent.ACTION_DOWN) { //ACTION_DOWN for the position touched.
    int X = (int)(e.getX());
    int Y = (int)(e.getY());
  }
  return false;
}

【讨论】:

    【解决方案2】:

    您应该获得 ACTION_DOWN 事件的位置,然后移动您的视图

    public boolean onTouchEvent(MotionEvent event) {
      switch (event.getAction()) {
         case MotionEvent.ACTION_DOWN:
              float x = event.getX();
              float y = event.getY();
              break;
        case MotionEvent.ACTION_MOVE:
            break;
        case MotionEvent.ACTION_UP:
            break;
    }
    return true;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多