main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button android:id="@+id/btn_hello" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:text="@string/hello" />
</LinearLayout>

Touch.java
public class Touch extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

final Button btn = (Button) findViewById(R.id.btn_hello);

btn.setOnTouchListener(new OnTouchListener() {
int[] temp = new int[] { 0, 0 };

public boolean onTouch(View v, MotionEvent event) {

int eventaction = event.getAction();
Log.i("&&&", "onTouchEvent:" + eventaction);

int x = (int) event.getRawX() ;
int y = (int) event.getRawY();

switch (eventaction) {
case MotionEvent.ACTION_DOWN: // touch down so check if the
temp[0] = (int) event.getX() ;
temp[1] = y - v.getTop();
break;

case MotionEvent.ACTION_MOVE: // touch drag with the ball
v.layout(x - temp[0], y - temp[1], x + v.getWidth() - temp[0], y - temp[1] + v.getHeight());
v.postInvalidate(); //redraw
break;
case MotionEvent.ACTION_UP:
break;
}

return false;
}
});
}
}

更多相关文章

  1. 代码中设置drawableleft
  2. android 3.0 隐藏 系统标题栏
  3. Android开发中activity切换动画的实现
  4. Android(安卓)学习 笔记_05. 文件下载
  5. Android中直播视频技术探究之—摄像头Camera视频源数据采集解析
  6. 技术博客汇总
  7. android 2.3 wifi (一)
  8. AndRoid Notification的清空和修改
  9. Android中的Chronometer

随机推荐

  1. android前端怎样php后台交互(基础篇)
  2. ANDRIOD学习笔记之nand、root以及主要调
  3. 3.4、Android(安卓)Studio 创建Android(
  4. 彻底解决Android(安卓)GPS没法定位这一顽
  5. 《Android和PHP开发最佳实践》一2.2 And
  6. Android之父:Android不封杀Skype等VOIP应
  7. Android(安卓)使用非阻塞的方式读写串口
  8. Android(安卓)调试:java 跨工程调试 andro
  9. android 事件分发机制详细解析
  10. Android(安卓)自定义控件打造史上最简单