当前位置:   article > 正文

Android拖动控件改变其位置_android launcher拖拽控件调整位置实现

android launcher拖拽控件调整位置实现

mainActivity如下:

  1. package cn.dragtest;
  2. import android.app.Activity;
  3. import android.graphics.Rect;
  4. import android.os.Bundle;
  5. import android.util.DisplayMetrics;
  6. import android.view.MotionEvent;
  7. import android.view.View;
  8. import android.view.View.OnTouchListener;
  9. import android.view.Window;
  10. import android.widget.Button;
  11. import android.widget.ImageView;
  12. public class MainActivity extends Activity implements OnTouchListener{
  13. private Button mButton;
  14. private ImageView mImageView;
  15. private DisplayMetrics displayMetrics;
  16. private float lastX=0;
  17. private float lastY=0;
  18. private int screenWidth=0;
  19. private int screenHeight=0;
  20. private int left;
  21. private int top;
  22. private int right;
  23. private int bottom;
  24. private boolean isFirst=true;
  25. @Override
  26. public void onCreate(Bundle savedInstanceState) {
  27. super.onCreate(savedInstanceState);
  28. setContentView(R.layout.main);
  29. initView();
  30. }
  31. private void initView() {
  32. mButton=(Button) findViewById(R.id.button);
  33. mImageView=(ImageView) findViewById(R.id.imageView);
  34. mButton.setOnTouchListener(this);
  35. mImageView.setOnTouchListener(this);
  36. }
  37. public boolean onTouch(View view, MotionEvent event) {
  38. if (isFirst) {
  39. // 得到屏幕的宽
  40. displayMetrics = getResources().getDisplayMetrics();
  41. screenWidth = displayMetrics.widthPixels;
  42. // 得到标题栏和状态栏的高度
  43. Rect rect = new Rect();
  44. Window window = getWindow();
  45. mImageView.getWindowVisibleDisplayFrame(rect);
  46. int statusBarHeight = rect.top;
  47. int contentViewTop = window.findViewById(Window.ID_ANDROID_CONTENT).getTop();
  48. int titleBarHeight = contentViewTop - statusBarHeight;
  49. // 得到屏幕的高
  50. screenHeight = displayMetrics.heightPixels- (statusBarHeight + titleBarHeight);
  51. isFirst=false;
  52. }
  53. int action=event.getAction();
  54. switch (action) {
  55. //按下
  56. case MotionEvent.ACTION_DOWN:
  57. //按下处坐标
  58. lastX=event.getRawX();
  59. lastY=event.getRawY();
  60. break;
  61. //移动
  62. case MotionEvent.ACTION_MOVE:
  63. //移动的距离
  64. float distanceX=event.getRawX()-lastX;
  65. float distanceY=event.getRawY()-lastY;
  66. //移动后控件的坐标
  67. left=(int)(view.getLeft()+distanceX);
  68. top=(int)(view.getTop()+distanceY);
  69. right=(int)(view.getRight()+distanceX);
  70. bottom=(int)(view.getBottom()+distanceY);
  71. //处理拖出屏幕的情况
  72. if (left<0) {
  73. left=0;
  74. right=view.getWidth();
  75. }
  76. if (right>screenWidth) {
  77. right=screenWidth;
  78. left=screenWidth-view.getWidth();
  79. }
  80. if (top<0) {
  81. top=0;
  82. bottom=view.getHeight();
  83. }
  84. if (bottom>screenHeight) {
  85. bottom=screenHeight;
  86. top=screenHeight-view.getHeight();
  87. }
  88. //显示图片
  89. view.layout(left, top, right, bottom);
  90. lastX=event.getRawX();
  91. lastY=event.getRawY();
  92. break;
  93. //抬起
  94. case MotionEvent.ACTION_UP:
  95. break;
  96. default:
  97. break;
  98. }
  99. return false;
  100. }
  101. }


main.xml如下:

  1. <LinearLayout
  2. xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:tools="http://schemas.android.com/tools"
  4. android:layout_width="match_parent"
  5. android:layout_height="match_parent"
  6. android:orientation="vertical"
  7. android:gravity="center_horizontal"
  8. >
  9. <ImageView
  10. android:id="@+id/imageView"
  11. android:layout_width="wrap_content"
  12. android:layout_height="wrap_content"
  13. android:src="@drawable/ic_launcher"
  14. android:clickable="true"
  15. />
  16. <Button
  17. android:id="@+id/button"
  18. android:layout_width="wrap_content"
  19. android:layout_height="wrap_content"
  20. android:text="please drag"
  21. android:clickable="true"
  22. />
  23. </LinearLayout>


 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/花生_TL007/article/detail/111327
推荐阅读
相关标签
  

闽ICP备14008679号