你的位置:首页 > 操作系统

[操作系统]实现控件的拖拽


如图是效果图,移动ImageView时ImageView的位置会发生改变,并且双击的时候ImageView会水平居中

 

package com.org.demo.youku;import com.org.wangfeng.R;import android.app.Activity;import android.content.SharedPreferences;import android.content.SharedPreferences.Editor;import android.os.Bundle;import android.os.SystemClock;import android.view.MotionEvent;import android.view.View;import android.view.View.OnTouchListener;import android.widget.ImageView;import android.widget.RelativeLayout;import android.widget.RelativeLayout.LayoutParams;import android.widget.TextView;public class MainDrag extends Activity {  private TextView tv_top;  private TextView tv_bottom;  private ImageView iv_drag;  // private ViewPager dPager;  private int startX;  private int startY;  private SharedPreferences sPreferences;  long[] mHits = new long[2];// 数组长度表示要点击的次数  @Override  protected void onCreate(Bundle savedInstanceState) {    // TODO Auto-generated method stub    super.onCreate(savedInstanceState);    setContentView(R.layout.maindrag);    tv_top = (TextView) findViewById(R.id.tv_top);    tv_bottom = (TextView) findViewById(R.id.tv_bottom);    iv_drag = (ImageView) findViewById(R.id.iv_drag);    // 计算手机屏的宽和高    final int winWidth = getWindowManager().getDefaultDisplay().getWidth();    final int winHeight = getWindowManager().getDefaultDisplay()        .getHeight();    // 用来记录坐标 下次进入实现移动后的坐标    sPreferences = getSharedPreferences("config", Activity.MODE_PRIVATE);    int getLastX = sPreferences.getInt("lastX", 0);    int getLastY = sPreferences.getInt("lastY", 0);    // onMeasure(测量view), onLayout(安放位置), onDraw(绘制)    // ivDrag.layout(getLastX, getLastY, getLastX + ivDrag.getWidth(),    // getLastY + ivDrag.getHeight());//不能用这个方法,因为还没有测量完成,就不能安放位置    if (getLastY > winHeight / 2) {// 上边显示,下边隐藏      tv_top.setVisibility(View.VISIBLE);      tv_bottom.setVisibility(View.INVISIBLE);    } else {      tv_top.setVisibility(View.INVISIBLE);      tv_bottom.setVisibility(View.VISIBLE);    }    RelativeLayout.LayoutParams layoutParams = (LayoutParams) iv_drag        .getLayoutParams();    layoutParams.leftMargin = getLastX;// 设置左边距    layoutParams.topMargin = getLastY;// 设置top边距    iv_drag.setLayoutParams(layoutParams); // 重新设置位置    // 设置双击居中的事件    iv_drag.setOnClickListener(new View.OnClickListener() {      @Override      public void onClick(View arg0) {        // TODO Auto-generated method stub        System.arraycopy(mHits, 1, mHits, 0, mHits.length - 1);        mHits[mHits.length - 1] = SystemClock.uptimeMillis();// 开机后开始计算的时间        if (mHits[0] >= (SystemClock.uptimeMillis() - 500)) {          // 把图片居中          System.out.println("双击了 要居中");          iv_drag.layout(winWidth / 2 - iv_drag.getWidth() / 2,              iv_drag.getTop(), winWidth / 2 + iv_drag.getWidth()                  / 2, iv_drag.getBottom());                  }      }    });    // 设置触摸监听事件    iv_drag.setOnTouchListener(new OnTouchListener() {      @Override      public boolean onTouch(View v, MotionEvent event) {        // TODO Auto-generated method stub        switch (event.getAction()) {        case MotionEvent.ACTION_DOWN:          // 初始化起点的坐标          startX = (int) event.getRawX();          startY = (int) event.getRawY();          break;        case MotionEvent.ACTION_MOVE:          // 移动后的坐标          int endX = (int) event.getRawX();          int endY = (int) event.getRawY();          // 计算移动的偏移量          int dx = endX - startX;          int dy = endY - startY;          // 更新后左上右下的距离          int l = iv_drag.getLeft() + dx;          int t = iv_drag.getTop() + dy;          int r = iv_drag.getRight() + dx;          int b = iv_drag.getBottom() + dy;          // 判断是否超出屏幕边界, 注意状态栏的高度          if (l < 0 || r > winWidth || t < 0 || b > winHeight) {            System.out.println("不符要求");            break;          }          // 根据图片位置,决定提示框显示和隐藏          if (t > winHeight / 2) {// 上边显示,下边隐藏            tv_top.setVisibility(View.VISIBLE);            tv_bottom.setVisibility(View.INVISIBLE);          } else {            tv_top.setVisibility(View.INVISIBLE);            tv_bottom.setVisibility(View.VISIBLE);          }          // 更新界面          iv_drag.layout(l, t, r, b);          // 重新初始化起点坐标          startX = (int) event.getRawX();          startY = (int) event.getRawY();          System.out.println("ACTION_MOVE坐标x" + startX              + "---iv_drag.getLeft()---" + iv_drag.getLeft());          break;        case MotionEvent.ACTION_UP:          // 记录移动后的坐标          System.out.println("ACTION_UP" + "iv_drag.getLeft()---"              + iv_drag.getLeft() + "---iv_drag.getTop()---"              + iv_drag.getTop());          Editor edit = sPreferences.edit();          edit.putInt("lastX", iv_drag.getLeft());          edit.putInt("lastY", iv_drag.getTop());          edit.commit();          break;        default:          break;        }        return false;// 事件要向下传递,让onclick(双击事件)可以响应      }    });  }}

<?  android:layout_width="match_parent"  android:layout_height="match_parent" >  <TextView    android:id="@+id/tv_top"    android:layout_width="match_parent"    android:layout_height="wrap_content"    android:layout_alignParentTop="true"    android:layout_centerHorizontal="true"    android:background="@drawable/png_3"    android:gravity="center"    android:padding="10dp"    android:text="按住提示框拖到任意位置\n按手机返回键立即生效"    android:textColor="#000"    android:textSize="16sp" />  <TextView    android:id="@+id/tv_bottom"    android:layout_width="match_parent"    android:layout_height="wrap_content"    android:layout_alignParentBottom="true"    android:layout_centerHorizontal="true"    android:background="@drawable/png_3"    android:gravity="center"    android:padding="10dp"    android:text="按住提示框拖到任意位置\n按手机返回键立即生效"    android:textColor="#000"    android:textSize="16sp"    android:visibility="invisible" />  <ImageView    android:id="@+id/iv_drag"    android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:layout_marginTop="90dp"    android:contentDescription="@null"    android:src="@drawable/drag" /></RelativeLayout>