android控件可以拖动处理方法

来源:互联网 发布:淘宝手机充值代理加盟 编辑:程序博客网 时间:2024/04/30 07:13
        点击和触摸的区别是什么?
        点击: 一组动作的集合 手指按下着按钮 手指要在按钮停留一段时间 手指离开按钮
       
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;@Overrideprotected 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);}@Overrideprotected 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);}@Overridepublic 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如下:
<?xml version="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>


触摸: 手指一挨着屏幕 手指移动 手指离开屏幕
原创粉丝点击