码迷,mamicode.com
首页 > 移动开发 > 详细

21.移动控件

时间:2015-10-30 16:50:52      阅读:217      评论:0      收藏:0      [点我收藏+]

标签:

效果:
技术分享

  1. <TextView
  2. android:id="@+id/tv_top"
  3. android:layout_width="match_parent"
  4. android:layout_height="wrap_content"
  5. android:layout_alignParentTop="true"
  6. android:layout_centerHorizontal="true"
  7. android:background="@drawable/call_locate_blue"
  8. android:gravity="center"
  9. android:padding="15dp"
  10. android:text="按住提示框拖到任意位置\n按手机返回键立即生效"
  11. android:textColor="@color/black"
  12. android:textSize="18sp" />
  13. <TextView
  14. android:id="@+id/tv_bottom"
  15. android:layout_width="match_parent"
  16. android:layout_height="wrap_content"
  17. android:layout_alignParentBottom="true"
  18. android:layout_centerHorizontal="true"
  19. android:background="@drawable/call_locate_blue"
  20. android:gravity="center"
  21. android:padding="10dp"
  22. android:text="按住提示框拖到任意位置\n按手机返回键立即生效"
  23. android:textColor="@color/black"
  24. android:textSize="18sp"
  25. android:visibility="invisible" />
  26. <ImageView
  27. android:id="@+id/iv_drag"
  28. android:layout_width="wrap_content"
  29. android:layout_height="wrap_content"
  30. android:layout_marginTop="90dp"
  31. android:src="@drawable/drag" />
  1. /**
  2. * 修改归属地显示位置
  3. *
  4. * @author Kevin
  5. *
  6. */
  7. public class DragViewActivity extends Activity {
  8. private TextView tvTop;
  9. private TextView tvBottom;
  10. private ImageView ivDrag;
  11. private int startX;
  12. private int startY;
  13. private SharedPreferences mPref;
  14. long[] mHits = new long[2];// 数组长度表示要点击的次数
  15. @Override
  16. protected void onCreate(Bundle savedInstanceState) {
  17. super.onCreate(savedInstanceState);
  18. setContentView(R.layout.activity_drag_view);
  19. mPref = getSharedPreferences("config", MODE_PRIVATE);
  20. tvTop = (TextView) findViewById(R.id.tv_top);
  21. tvBottom = (TextView) findViewById(R.id.tv_bottom);
  22. ivDrag = (ImageView) findViewById(R.id.iv_drag);
  23. int lastX = mPref.getInt("lastX", 0);
  24. int lastY = mPref.getInt("lastY", 0);
  25. // ivDrag.getWidth();
  26. // onMeasure(测量view), onLayout(安放位置), onDraw(绘制)
  27. // ivDrag.layout(lastX, lastY, lastX + ivDrag.getWidth(),
  28. // lastY + ivDrag.getHeight());//不能用这个方法,因为还没有测量完成,就不能安放位置
  29. // 获取屏幕宽高
  30. final int winWidth = getWindowManager().getDefaultDisplay().getWidth();
  31. final int winHeight = getWindowManager().getDefaultDisplay()
  32. .getHeight();
  33. if (lastY > winHeight / 2) {// 上边显示,下边隐藏
  34. tvTop.setVisibility(View.VISIBLE);
  35. tvBottom.setVisibility(View.INVISIBLE);
  36. } else {
  37. tvTop.setVisibility(View.INVISIBLE);
  38. tvBottom.setVisibility(View.VISIBLE);
  39. }
  40. RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) ivDrag
  41. .getLayoutParams();// 获取布局对象
  42. layoutParams.leftMargin = lastX;// 设置左边距
  43. layoutParams.topMargin = lastY;// 设置top边距
  44. ivDrag.setLayoutParams(layoutParams);// 重新设置位置
  45. ivDrag.setOnClickListener(new OnClickListener() {
  46. @Override
  47. public void onClick(View v) {
  48. System.arraycopy(mHits, 1, mHits, 0, mHits.length - 1);
  49. mHits[mHits.length - 1] = SystemClock.uptimeMillis();// 开机后开始计算的时间
  50. if (mHits[0] >= (SystemClock.uptimeMillis() - 500)) {
  51. // 把图片居中
  52. ivDrag.layout(winWidth / 2 - ivDrag.getWidth() / 2,
  53. ivDrag.getTop(), winWidth / 2 + ivDrag.getWidth()
  54. / 2, ivDrag.getBottom());
  55. }
  56. }
  57. });
  58. // 设置触摸监听
  59. ivDrag.setOnTouchListener(new OnTouchListener() {
  60. @Override
  61. public boolean onTouch(View v, MotionEvent event) {
  62. switch (event.getAction()) {
  63. case MotionEvent.ACTION_DOWN:
  64. // 初始化起点坐标
  65. startX = (int) event.getRawX();
  66. startY = (int) event.getRawY();
  67. break;
  68. case MotionEvent.ACTION_MOVE:
  69. int endX = (int) event.getRawX();
  70. int endY = (int) event.getRawY();
  71. // 计算移动偏移量
  72. int dx = endX - startX;
  73. int dy = endY - startY;
  74. // 更新左上右下距离
  75. int l = ivDrag.getLeft() + dx;
  76. int r = ivDrag.getRight() + dx;
  77. int t = ivDrag.getTop() + dy;
  78. int b = ivDrag.getBottom() + dy;
  79. // 判断是否超出屏幕边界, 注意状态栏的高度
  80. if (l < 0 || r > winWidth || t < 0 || b > winHeight - 20) {
  81. break;
  82. }
  83. // 根据图片位置,决定提示框显示和隐藏
  84. if (t > winHeight / 2) {// 上边显示,下边隐藏
  85. tvTop.setVisibility(View.VISIBLE);
  86. tvBottom.setVisibility(View.INVISIBLE);
  87. } else {
  88. tvTop.setVisibility(View.INVISIBLE);
  89. tvBottom.setVisibility(View.VISIBLE);
  90. }
  91. // 更新界面
  92. ivDrag.layout(l, t, r, b);
  93. // 重新初始化起点坐标
  94. startX = (int) event.getRawX();
  95. startY = (int) event.getRawY();
  96. break;
  97. case MotionEvent.ACTION_UP:
  98. // 记录坐标点
  99. Editor edit = mPref.edit();
  100. edit.putInt("lastX", ivDrag.getLeft());
  101. edit.putInt("lastY", ivDrag.getTop());
  102. edit.commit();
  103. break;
  104. default:
  105. break;
  106. }
  107. return false;//事件要向下传递,让onclick(双击事件)可以响应
  108. }
  109. });
  110. }
  111. }










21.移动控件

标签:

原文地址:http://www.cnblogs.com/liuyu0529/p/4923662.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!