Android Ui控件的拖动实现

实现思路

  • 1 获取当前window窗体的height /width
  • 2 ui控件的滑动触摸监听事件(OnTouchListener())(action_down/action_move/action_up)
  • 3 获取手势按下控件的坐标
  • 4 在移动move监听时算出坐标差 控件原始的坐标加上坐标差就是移动过后的新坐标值
 public classTouchButtonActivityextendsAppCompatActivity{

    privateImageViewicon;

    private intscreenHegiht ;//高度

    private intscreenWidth ;//宽度

    privateSharedPreferencessp;//存储文件的方式

@Override

protected voidonCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_toch_button);

    //获取window宽度和高度(sdk13版本使用的方法官方已经废弃)
    
    /* this.screenHegiht = this.getWindowManager().getDefaultDisplay().getHeight();
    
    this.screenWidth = this.getWindowManager().getDefaultDisplay().getWidth();*/
    
    //官方推荐的方法

    DisplayMetrics metrics = newDisplayMetrics();

    this.getWindowManager().getDefaultDisplay().getMetrics(metrics);

    this.screenHegiht = metrics.heightPixels;

    this.screenWidth = metrics.widthPixels;

    icon = (ImageView) findViewById(R.id.icon_imag);

    //在存储文件中读取坐标数据

    sp = this.getSharedPreferences("config", MODE_PRIVATE);//获取文件存储方式

    intlastx = this.sp.getInt("lastx", 0);

    intlasty = this.sp.getInt("lasty", 0);

//设置坐标数据到ImageView控件上

    RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) this.icon

            .getLayoutParams();

    params.leftMargin = lastx;

    params.topMargin = lasty;

    this.icon.setLayoutParams(params);

/**

 * ImageView的滑动监听事件

 */

    this.icon.setOnTouchListener(newView.OnTouchListener() {

        intstartX;

        intstartY;

@Override

public booleanonTouch(View v, MotionEvent event) {

            switch (event.getAction()) {

                caseMotionEvent.ACTION_DOWN://按下

                this.startX = (int) event.getRawX();

                this.startY = (int) event.getRawY();

                break;

                caseMotionEvent.ACTION_MOVE://移动

                intnewX = (int) event.getRawX();

                intnewY = (int) event.getRawY();

                intdx = newX - this.startX;

                intdy = newY - this.startY;

                intl = TouchButtonActivity.this.icon.getLeft();

                intr = TouchButtonActivity.this.icon.getRight();

                intt = TouchButtonActivity.this.icon.getTop();

                intb = TouchButtonActivity.this.icon.getBottom();

                intnewt = t + dy;

                intnewb = b + dy;

                intnewl = l + dx;

                intnewr = r + dx;

                if ((newl < 0) || (newt < 0)

                        || (newr > TouchButtonActivity.this.screenWidth)

                        || (newb > TouchButtonActivity.this.screenHegiht)) {

                    break;

                }

                TouchButtonActivity.this.icon.layout(newl, newt, newr, newb);

                this.startX = (int) event.getRawX();

                this.startY = (int) event.getRawY();

                break;

                caseMotionEvent.ACTION_UP://手指抬起

                intlastx = TouchButtonActivity.this.icon.getLeft();

                intlasty = TouchButtonActivity.this.icon.getTop();

                SharedPreferences.Editor editor = TouchButtonActivity.this.sp.edit();

                editor.putInt("lastx", lastx);

                editor.putInt("lasty", lasty);

                editor.commit();

                break;

                default:

                    break;

            }

            return true;//true事件传递OnTouch消费不再向上一级传递false在一次传递到上一级操作页面

        }

    });

}

}
    原文作者:MrZang
    原文地址: https://www.jianshu.com/p/604cc70bd7df
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞