1. 布局文件

view plain copy to clipboard print ?
  1. <?xml version = "1.0" encoding = "utf-8" ?>
  2. <LinearLayout xmlns:android = "http://schemas.android.com/apk/res/android"
  3. android:orientation = "vertical" android:layout_width = "fill_parent"
  4. android:layout_height = "fill_parent" >
  5. <Button android:id = "@+id/btn_hello" android:layout_width = "fill_parent"
  6. android:layout_height = "wrap_content" android:text = "@string/hello" />
  7. </LinearLayout>

2. 代码

view plain copy to clipboard print ?
  1. package com.roiding.sample;
  2. import android.app.Activity;
  3. import android.os.Bundle;
  4. import android.util.Log;
  5. import android.view.MotionEvent;
  6. import android.view.View;
  7. import android.view.View.OnTouchListener;
  8. import android.widget.Button;
  9. public class Touch extends Activity{
  10. /**Calledwhentheactivityisfirstcreated.*/
  11. @Override
  12. public void onCreate(BundlesavedInstanceState){
  13. super .onCreate(savedInstanceState);
  14. setContentView(R.layout.main);
  15. final Buttonbtn=(Button)findViewById(R.id.btn_hello);
  16. btn.setOnTouchListener( new OnTouchListener(){
  17. int []temp= new int []{ 0 , 0 };
  18. public boolean onTouch(Viewv,MotionEventevent){
  19. int eventaction=event.getAction();
  20. Log.i( "&&&" , "onTouchEvent:" +eventaction);
  21. int x=( int )event.getRawX();
  22. int y=( int )event.getRawY();
  23. switch (eventaction){
  24. case MotionEvent.ACTION_DOWN: //touchdownsocheckifthe
  25. temp[ 0 ]=( int )event.getX();
  26. temp[ 1 ]=y-v.getTop();
  27. break ;
  28. case MotionEvent.ACTION_MOVE: //touchdragwiththeball
  29. v.layout(x-temp[ 0 ],y-temp[ 1 ],x+v.getWidth()
  30. -temp[ 0 ],y-temp[ 1 ]+v.getHeight());
  31. v.postInvalidate();
  32. break ;
  33. case MotionEvent.ACTION_UP:
  34. break ;
  35. }
  36. return false ;
  37. }
  38. });
  39. }
  40. }

更多相关文章

  1. Android入门之相对布局(RelativeLayout)
  2. ViewPager添加动画效果(一行代码)
  3. android source code下载源代码时出错
  4. 网页源代码查看器
  5. Android小项目之欢迎滑动页面(页面布局&底部点点)
  6. Android 颜色代码
  7. Android source code[ 源代码]下载,问题和解决办法

随机推荐

  1. 第二部分:开发简要指南-第五章 支持不同的
  2. 关于android单元测试结合SAX解析xml文件
  3. 【Android的从零单排开发日记】之入门篇(
  4. 《Android移动应用基础教程》(Android Stu
  5. 年后陆续面试Android 15 家大厂,这个问题
  6. Android利用WebService查询手机号码归属
  7. android 玩转ContentProvider之三--实现一
  8. 转:Android文件操作总结
  9. 北京android待遇 月薪过万秘籍分享
  10. linux下android开发环境的配置