比较简单就直接上代码了
1 package cn.itcast.mobilesafe; 2 3 import android.app.Activity; 4 import android.content.SharedPreferences; 5 import android.content.SharedPreferences.Editor; 6 import android.os.Bundle; 7 import android.os.Environment; 8 import android.util.Log; 9 import android.view.Display; 10 import android.view.MotionEvent; 11 import android.view.View; 12 import android.view.View.OnClickListener; 13 import android.view.View.OnTouchListener; 14 import android.widget.EditText; 15 import android.widget.FrameLayout; 16 import android.widget.ImageView; 17 import android.widget.RelativeLayout; 18 import android.widget.RelativeLayout.LayoutParams; 19 import android.widget.TextView; 20 21 public class DragViewActiviy extends Activity { 22 protected static final String TAG = "DragViewActiviy"; 23 private ImageView iv_dragview_location; 24 private Display display; 25 private TextView tv_dragview; 26 private long firstTime; 27 private SharedPreferences sp; 28 @Override 29 protected void onCreate(Bundle savedInstanceState) { 30 31 super.onCreate(savedInstanceState); 32 display = getWindowManager().getDefaultDisplay(); 33 sp = getSharedPreferences("config", MODE_PRIVATE); 34 setContentView(R.layout.drag_view); 35 iv_dragview_location = (ImageView) findViewById(R.id.iv_dragview_location); 36 tv_dragview = (TextView) findViewById(R.id.tv_dragview); 37 38 39 int lastx = sp.getInt("lastx", 0); 40 int lasty = sp.getInt("lasty", 0); 41 //只有当ui被完全渲染出来的时候 执行才有用. 42 //iv_dragview_location.layout(l, t, r, b); 43 //在oncreate方法里面 控件的位置 和大小现在还没有被计算显示出来. 44 45 //在布局渲染的第一个阶段进行的. 46 RelativeLayout.LayoutParams params = (LayoutParams) iv_dragview_location.getLayoutParams(); 47 params.leftMargin = lastx; 48 params.topMargin = lasty; 49 iv_dragview_location.setLayoutParams(params); 50 51 iv_dragview_location.setOnClickListener(new OnClickListener() { 52 53 public void onClick(View v) { 54 Log.i(TAG, "哥被点击了"); 55 if (firstTime > 0) {// 已经点击过了 56 long secondTime = System.currentTimeMillis(); 57 long dtime = secondTime - firstTime; 58 if (dtime < 500) { 59 Log.i(TAG, "双击事件"); 60 int tv_width = iv_dragview_location.getRight() 61 - iv_dragview_location.getLeft(); 62 int widowwidth = display.getWidth(); 63 iv_dragview_location.layout(widowwidth / 2 - tv_width 64 / 2, iv_dragview_location.getTop(), widowwidth 65 / 2 + tv_width / 2, 66 iv_dragview_location.getBottom()); 67 68 int lastx = iv_dragview_location.getLeft(); 69 int lasty = iv_dragview_location.getTop(); 70 Editor editor = sp.edit(); 71 editor.putInt("lastx", lastx); 72 editor.putInt("lasty",lasty); 73 editor.commit(); 74 75 76 } 77 } 78 79 firstTime = System.currentTimeMillis(); 80 new Thread() {// 单击的超时处理 81 public void run() { 82 try { 83 Thread.sleep(500); 84 firstTime = 0; 85 } catch (InterruptedException e) { 86 // TODO Auto-generated catch block 87 e.printStackTrace(); 88 } 89 }; 90 }.start(); 91 92 } 93 }); 94 95 iv_dragview_location.setOnTouchListener(new OnTouchListener() { 96 97 int startX;// 记录第一次手指在窗体中的位置. 98 int startY; 99 100 public boolean onTouch(View v, MotionEvent event) { 101 102 switch (event.getAction()) { 103 case MotionEvent.ACTION_DOWN: 104 Log.i(TAG, "摸到"); 105 startX = (int) event.getRawX(); 106 startY = (int) event.getRawY(); 107 108 break; 109 case MotionEvent.ACTION_MOVE: 110 Log.i(TAG, "移动"); 111 int movedX = (int) event.getRawX(); 112 int movedY = (int) event.getRawY(); 113 int dx = movedX - startX; 114 int dy = movedY - startY; 115 // 立刻的更新 imageview在窗体中的位置. 116 int l = iv_dragview_location.getLeft(); 117 int t = iv_dragview_location.getTop(); // 得到imageview在窗体中上面的坐标 118 int r = iv_dragview_location.getRight(); 119 int b = iv_dragview_location.getBottom(); 120 int tv_height = tv_dragview.getBottom() 121 - tv_dragview.getTop(); 122 123 if (t > (display.getHeight() / 2)) {// imageview 移动到了屏幕的下面 124 // 更新 textview 设置在屏幕的上面 125 tv_dragview.layout(tv_dragview.getLeft(), 0, 126 tv_dragview.getRight(), tv_height); 127 128 } else { 129 // 更新 textview 设置在屏幕的下面 130 tv_dragview.layout(tv_dragview.getLeft(), 131 display.getHeight() - tv_height - 15, 132 tv_dragview.getRight(), 133 display.getHeight() - 15); 134 } 135 int newl = l + dx; 136 int newr = r +dx; 137 int newt = t+dy; 138 int newb = b+dy; 139 if(newl < 0|| newr > display.getWidth() || newt < 0 || newb >display.getHeight()){ 140 break; 141 } 142 143 144 iv_dragview_location.layout(newl, newt, newr, newb);// 更新了iv在窗体中的位置 145 146 // 重新记录新的手指的初始位置. 147 startX = (int) event.getRawX(); 148 startY = (int) event.getRawY(); 149 150 break; 151 case MotionEvent.ACTION_UP: 152 Log.i(TAG, "放手"); 153 int lastx = iv_dragview_location.getLeft(); 154 int lasty = iv_dragview_location.getTop(); 155 Editor editor = sp.edit(); 156 editor.putInt("lastx", lastx); 157 editor.putInt("lasty",lasty); 158 editor.commit(); 159 break; 160 161 } 162 163 return false;// 是否消费掉这个事件 164 } 165 }); 166 167 } 168 }