android 集结号Android文集

Android Ui控件的拖动实现

2017-03-14  本文已影响51人  Luh的星河

实现思路


 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在一次传递到上一级操作页面

        }

    });

}

}
上一篇下一篇

猜你喜欢

热点阅读