本文实例讲述了Android编程之控件可拖动的实现方法。分享给大家供大家参考,具体如下:
点击和触摸的区别是什么?
点击: 一组动作的集合 手指按下着按钮 手指要在按钮停留一段时间 手指离开按钮
private static final String TAG = "DragViewActivity"; private ImageView iv_dv_view; private TextView tv_drag_view; private int startx; private int starty; private SharedPreferences sp; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.dragview); // Drawable drawable = new ColorDrawable(color.transparent); // getWindow().setBackgroundDrawable(drawable); iv_dv_view = (ImageView) this.findViewById(R.id.iv_dv_view); tv_drag_view = (TextView) this.findViewById(R.id.tv_drag_view); sp = this.getSharedPreferences("config", Context.MODE_PRIVATE); iv_dv_view.setOnTouchListener(this); } @Override protected void onResume() { super.onResume(); int x = sp.getInt("lastx", 0); int y = sp.getInt("lasty", 0); // iv_dv_view.layout(iv_dv_view.getLeft() + x, iv_dv_view.getTop() + y, // iv_dv_view.getRight() + x, iv_dv_view.getBottom() + y); // iv_dv_view.invalidate();//界面重新渲染 LayoutParams params = (LayoutParams) iv_dv_view.getLayoutParams(); params.leftMargin = x; params.topMargin = y; iv_dv_view.setLayoutParams(params); } @Override public boolean onTouch(View v, MotionEvent event) { switch (v.getId()) { // 如果手指放在imageView上拖动 case R.id.iv_dv_view: // event.getRawX(); //获取手指第一次接触屏幕在x方向的坐标 switch (event.getAction()) { case MotionEvent.ACTION_DOWN:// 获取手指第一次接触屏幕 startx = (int) event.getRawX(); starty = (int) event.getRawY(); break; case MotionEvent.ACTION_MOVE:// 手指在屏幕上移动对应的事件 int x = (int) event.getRawX(); int y = (int) event.getRawY(); if (y < 400) { // 设置TextView在窗体的下面 tv_drag_view.layout(tv_drag_view.getLeft(), 420, tv_drag_view.getRight(), 440); } else { tv_drag_view.layout(tv_drag_view.getLeft(), 60, tv_drag_view.getRight(), 80); } // 获取手指移动的距离 int dx = x - startx; int dy = y - starty; // 得到imageView最开始的各顶点的坐标 int l = iv_dv_view.getLeft(); int r = iv_dv_view.getRight(); int t = iv_dv_view.getTop(); int b = iv_dv_view.getBottom(); // 更改imageView在窗体的位置 iv_dv_view.layout(l + dx, t + dy, r + dx, b + dy); // 获取移动后的位置 startx = (int) event.getRawX(); starty = (int) event.getRawY(); break; case MotionEvent.ACTION_UP:// 手指离开屏幕对应事件 Log.i(TAG, "手指离开屏幕"); // 记录最后图片在窗体的位置 int lasty = iv_dv_view.getTop(); int lastx = iv_dv_view.getLeft(); Editor editor = sp.edit(); editor.putInt("lasty", lasty); editor.putInt("lastx", lastx); editor.commit(); break; } break; } return true;// 不会中断触摸事件的返回 }
xml如下:
<"1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#cc000000" > <ImageView android:layout_width="160dip" android:layout_height="60dip" android:background="@drawable/button_background_selected" android:id="@+id/iv_dv_view" /> <TextView android:id="@+id/tv_drag_view" android:layout_marginTop="80dip" android:layout_width="fill_parent" android:layout_height="20dip" android:text="按住绿色条拖动归属地显示的位置" /> </RelativeLayout>
触摸: 手指一挨着屏幕 手指移动 手指离开屏幕
希望本文所述对大家Android程序设计有所帮助。