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(安卓)SDCard操作(文件读写,容量计算)
  2. android source code下载源代码时出错
  3. ViewPager添加动画效果(一行代码)
  4. Android(安卓)模拟登陆 保存密码(信息)到手机中 文件信息读取
  5. Android字体设置
  6. 编译代码报出Android(安卓)library projects cannot be launched
  7. android上传图片至服务器
  8. android读取SDCard任意路径下的文件
  9. NPM 和webpack 的基础使用

随机推荐

  1. Activity之间传递对象
  2. android通过USB的MTP模式下,禁止用户在根
  3. 我的Android之旅——UI界面六大布局之认
  4. android Can't create handler inside th
  5. Android中main.xml界面参数笔记
  6. android 2.3 wifi (二)
  7. [转]android animation的应用实例
  8. mvvm android 下的简单实践
  9. Android 访问Webservice接口,参数对象不能
  10. 申请 android google map API key