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. layout_gravity 和 gravity以及对应值的
  2. android自学笔记(1):android简介
  3. Android基础复习
  4. android4.4 安装微信7.0.6版本底部显示黑
  5. 大量正规厂商进驻Android Market,请国人务
  6. android 图片占用进程的内存算法
  7. Android 修炼之路
  8. Android Zip文件解压缩代码
  9. Android之布局属性重点
  10. 用Android Studio 运行ndk 程序