from: http://my.oschina.net/castusz/blog/66338

下面是activity的代码:

 
  
  1. public class DraftTest extends Activity { 
  2.  
  3.     @Override 
  4.     protected void onCreate(Bundle savedInstanceState) { 
  5.         super.onCreate(savedInstanceState); 
  6.         setContentView(R.layout.activity_draft_test); 
  7.  
  8.         DisplayMetrics dm = getResources().getDisplayMetrics(); 
  9.         final int screenWidth = dm.widthPixels; 
  10.         final int screenHeight = dm.heightPixels - 50
  11.         // 拖动的按钮 
  12.         final Button b = (Button) findViewById(R.id.startBtn); 
  13.  
  14.         // 添加触摸事件 
  15.         b.setOnTouchListener(new OnTouchListener() { 
  16.             int lastX, lastY; // 记录移动的最后的位置 
  17.  
  18.             public boolean onTouch(View v, MotionEvent event) { 
  19.                 // 获取Action 
  20.                 int ea = event.getAction(); 
  21.                 Log.i("TAG""Touch:" + ea); 
  22.                 switch (ea) { 
  23.                 case MotionEvent.ACTION_DOWN: // 按下 
  24.                     lastX = (int) event.getRawX(); 
  25.                     lastY = (int) event.getRawY(); 
  26.                     break
  27.                 /** 
  28.                  * layout(l,t,r,b) l Left position, relative to parent t Top 
  29.                  * position, relative to parent r Right position, relative to 
  30.                  * parent b Bottom position, relative to parent 
  31.                  * */ 
  32.                 case MotionEvent.ACTION_MOVE: // 移动 
  33.                     // 移动中动态设置位置 
  34.                     int dx = (int) event.getRawX() - lastX; 
  35.                     int dy = (int) event.getRawY() - lastY; 
  36.                     int left = v.getLeft() + dx; 
  37.                     int top = v.getTop() + dy; 
  38.                     int right = v.getRight() + dx; 
  39.                     int bottom = v.getBottom() + dy; 
  40.                     if (left < 0) { 
  41.                         left = 0
  42.                         right = left + v.getWidth(); 
  43.                     } 
  44.                     if (right > screenWidth) { 
  45.                         right = screenWidth; 
  46.                         left = right - v.getWidth(); 
  47.                     } 
  48.                     if (top < 0) { 
  49.                         top = 0
  50.                         bottom = top + v.getHeight(); 
  51.                     } 
  52.                     if (bottom > screenHeight) { 
  53.                         bottom = screenHeight; 
  54.                         top = bottom - v.getHeight(); 
  55.                     } 
  56.                     v.layout(left, top, right, bottom); 
  57.                     Log.i("aaa""position:" + left + ", " + top + ", " + right 
  58.                             + ", " + bottom); 
  59.                     // 将当前的位置再次设置 
  60.                     lastX = (int) event.getRawX(); 
  61.                     lastY = (int) event.getRawY(); 
  62.                     break
  63.                 case MotionEvent.ACTION_UP: // 脱离 
  64.                     break
  65.                 } 
  66.                 return false
  67.             } 
  68.         }); 
  69.     } 
  70.  
  71.     @Override 
  72.     public boolean onCreateOptionsMenu(Menu menu) { 
  73.         // Inflate the menu; this adds items to the action bar if it is present. 
  74.         getMenuInflater().inflate(R.menu.activity_draft_test, menu); 
  75.         return true
  76.     } 
  77.  

配置文件activity_draft_test.xml的内容:

 
  
  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
  2.     xmlns:tools="http://schemas.android.com/tools" 
  3.     android:layout_width="match_parent" 
  4.     android:layout_height="match_parent" 
  5.     android:padding="0px" 
  6.     tools:context=".DraftTest" > 
  7.  
  8.     <Button 
  9.         android:id="@+id/startBtn" 
  10.         android:text="tuodongdeanniu" 
  11.         android:layout_centerInParent="true" 
  12.         android:layout_width="wrap_content" android:layout_height="wrap_content"/> 
  13.  
  14. </RelativeLayout>