为什么updateViewLayout在用手指移动之前总会导致掉落?

时间:2015-03-12 02:41:03

标签: android textview ontouchlistener

我第一次写一个Android应用程序。 我创建了一个浮动窗口,在textview中显示当前的毫秒数(id是fShowtime)。

然而,它有点奇怪: 每当我想移动这个漂浮的文本视图时,它会一直下降一点,然后用我的手指移动。

我真的不明白为什么,请给我一些建议,谢谢。

以下是我的代码:

mFloatLayout = (LinearLayout) inflater.inflate(R.layout.float_layout, null);

Rect frame =  new  Rect();
fShowtime.getWindowVisibleDisplayFrame(frame);
statusBarHeight = frame.top;
measuredHeight=fShowtime.getMeasuredHeight()/2;
measuredWidth=fShowtime.getMeasuredWidth()/2;


fShowtime.setOnTouchListener(new OnTouchListener()
{
    @Override
    public boolean onTouch(View v, MotionEvent event)
    {
        switch(event.getAction())
        {
            case MotionEvent.ACTION_DOWN:
                break;
            case MotionEvent.ACTION_MOVE:
                wmParams.x = (int) (event.getRawX() - measuredWidth);
                wmParams.y = (int) (event.getRawY() - measuredHeight - statusBarHeight);
                mWindowManager.updateViewLayout(mFloatLayout, wmParams);
                break;
            case MotionEvent.ACTION_UP:
                break;
        }
        return true;  //I don't need OnClickListener
    }
});

1 个答案:

答案 0 :(得分:2)

什么是

 wmParams.y = (int) (event.getRawY() - measuredHeight - statusBarHeight);

关注this 请参阅最佳触控方式:

chatHead.setOnTouchListener(new View.OnTouchListener() {
  private int initialX;
  private int initialY;
  private float initialTouchX;
  private float initialTouchY;

  @Override public boolean onTouch(View v, MotionEvent event) {
    switch (event.getAction()) {
      case MotionEvent.ACTION_DOWN:
        initialX = params.x;
        initialY = params.y;
        initialTouchX = event.getRawX();
        initialTouchY = event.getRawY();
        return true;
      case MotionEvent.ACTION_UP:
        return true;
      case MotionEvent.ACTION_MOVE:
        params.x = initialX + (int) (event.getRawX() - initialTouchX);
        params.y = initialY + (int) (event.getRawY() - initialTouchY);
        windowManager.updateViewLayout(chatHead, params);
        return true;
    }
    return false;
  }
});