当前位置:主页 > 移动开发 > Android代码 >

Android实现拖动效果的两种方法

时间:2020-12-14 14:37:39 | 栏目:Android代码 | 点击:

因为最近项目的需要,需要实现一个拖动效果,看了一下网上刚好有这种拖动效果的demo,代码大概如下:

private void initListener(){
    screenWidth = getScreenWidth(this);//获取屏幕宽度
    screenHeight = getScreenHeight(this) - getStatusHeight(MainActivity.this);//屏幕高度-状态栏
    testTv.setOnTouchListener(new View.OnTouchListener() {
      @Override
      public boolean onTouch(View v, MotionEvent event) {
        switch (event.getAction()) {

          case MotionEvent.ACTION_DOWN:
            lastX = (int) event.getRawX();
            lastY = (int) event.getRawY();
            break;

          case MotionEvent.ACTION_MOVE:
            int dx = (int) event.getRawX() - lastX;
            int dy = (int) event.getRawY() - lastY;

            int top = v.getTop() + dy;


            int left = v.getLeft() + dx;


            if (top <= 0) {
              top = 0;
            }
            if (top >= screenHeight - testTv.getHeight() ) {
              top = screenHeight - testTv.getHeight();
            }
            if (left >= screenWidth - testTv.getWidth()) {
              left = screenWidth - testTv.getWidth();
            }

            if (left <= 0) {
              left = 0;
            }


            v.layout(left, top, left+v.getWidth(), top+v.getHeight());

            v.postInvalidate();

            lastX = (int) event.getRawX();
            lastY = (int) event.getRawY();

        break;
        case MotionEvent.ACTION_UP:
        break;

        }
        return true;//这里根据项目需要选择false与true
      }
    });
  }

可是我的项目本身是有涂鸦的界面,还有很多复杂的事件,发现会有很奇怪的效果,抖动很厉害,而且时不时回到原来的位置,后来发现这个v.layout(left, top, left+v.getWidth(), top+v.getHeight());可能不适合项目的需求,所以我想了另一种解法。代码如下:

 private void initListener(){
    screenWidth = getScreenWidth(this);//获取屏幕宽度
    screenHeight = getScreenHeight(this) - getStatusHeight(MainActivity.this);//屏幕高度-状态栏
    testTv.setOnTouchListener(new View.OnTouchListener() {
      @Override
      public boolean onTouch(View v, MotionEvent event) {
        switch (event.getAction()) {

          case MotionEvent.ACTION_DOWN:
            lastX = (int) event.getRawX();
            lastY = (int) event.getRawY();
            break;

          case MotionEvent.ACTION_MOVE:
            int dx = (int) event.getRawX() - lastX;
            int dy = (int) event.getRawY() - lastY;

            int top = v.getTop() + dy;


            int left = v.getLeft() + dx;


            if (top <= 0) {
              top = 0;
            }
            if (top >= screenHeight - testTv.getHeight() ) {
              top = screenHeight - testTv.getHeight();
            }
            if (left >= screenWidth - testTv.getWidth()) {
              left = screenWidth - testTv.getWidth();
            }

            if (left <= 0) {
              left = 0;
            }

            RelativeLayout.LayoutParams param = new RelativeLayout.LayoutParams(v.getWidth(), v.getHeight());
            param.leftMargin = left;
            param.topMargin = top;
            v.setLayoutParams(param);
//            v.layout(left, top, left+v.getWidth(), top+v.getHeight());

            v.postInvalidate();

            lastX = (int) event.getRawX();
            lastY = (int) event.getRawY();

        break;
        case MotionEvent.ACTION_UP:
        break;

        }
        return true;
      }
    });
  }

效果如下图:

觉得可以了,不过要注意一些事项:键盘弹出需要自己处理一下(比如处理成弹出键盘就不能移动这些,不然会变形的),我觉得这种写法可以应用于各种复杂的场景。好了,今天一些项目问题总结就到这里了,希望对大家有帮助,代码也上传了,地址如下:Android实现拖动效果

您可能感兴趣的文章:

相关文章