Android控件拖动的实现

时间:2022-01-18 01:30:28

这个也是从网上得到的代码,例子比较简单,但是如果有需要此功能的,这个例子可以提供很多提示,首先,给个截图

Android控件拖动的实现

这个是拖动以后的效果,一个imageview和一个button控件,提供两份代码下载吧,一份是只有一个Button的,另一份就是像上图,就是多了一个imagview!先看下代码吧,比较简单:

  1. public class DraftTest extends Activity implements OnTouchListener{
  2. /** Called when the activity is first created. */
  3. int screenWidth;
  4. int screenHeight;
  5. int lastX;
  6. int lastY;
  7. @Override
  8. public void onCreate(Bundle savedInstanceState) {
  9. super.onCreate(savedInstanceState);
  10. setContentView(R.layout.main);
  11. DisplayMetrics dm = getResources().getDisplayMetrics();
  12. screenWidth = dm.widthPixels;
  13. screenHeight = dm.heightPixels - 50;
  14. Button button=(Button)findViewById(R.id.btn);
  15. ImageView imageView=(ImageView)findViewById(R.id.btn2);
  16. imageView.setOnTouchListener(this);
  17. button.setOnTouchListener(this);
  18. }
  19. @Override
  20. public boolean onTouch(View v, MotionEvent event) {
  21. // TODO Auto-generated method stub
  22. int action=event.getAction();
  23. Log.i("@@@@@@", "Touch:"+action);
  24. //Toast.makeText(DraftTest.this, "λ�ã�"+x+","+y, Toast.LENGTH_SHORT).show();
  25. switch(action){
  26. case MotionEvent.ACTION_DOWN:
  27. lastX = (int) event.getRawX();
  28. lastY = (int) event.getRawY();
  29. break;
  30. /**
  31. * layout(l,t,r,b)
  32. * l  Left position, relative to parent
  33. t  Top position, relative to parent
  34. r  Right position, relative to parent
  35. b  Bottom position, relative to parent
  36. * */
  37. case MotionEvent.ACTION_MOVE:
  38. int dx =(int)event.getRawX() - lastX;
  39. int dy =(int)event.getRawY() - lastY;
  40. int left = v.getLeft() + dx;
  41. int top = v.getTop() + dy;
  42. int right = v.getRight() + dx;
  43. int bottom = v.getBottom() + dy;
  44. if(left < 0){
  45. left = 0;
  46. right = left + v.getWidth();
  47. }
  48. if(right > screenWidth){
  49. right = screenWidth;
  50. left = right - v.getWidth();
  51. }
  52. if(top < 0){
  53. top = 0;
  54. bottom = top + v.getHeight();
  55. }
  56. if(bottom > screenHeight){
  57. bottom = screenHeight;
  58. top = bottom - v.getHeight();
  59. }
  60. v.layout(left, top, right, bottom);
  61. Log.i("@@@@@@", "position��" + left +", " + top + ", " + right + ", " + bottom);
  62. lastX = (int) event.getRawX();
  63. lastY = (int) event.getRawY();
  64. break;
  65. case MotionEvent.ACTION_UP:
  66. break;
  67. }
  68. return false;
  69. }
  70. }

高度减去50是减去状态栏和标题栏的高度。

  1. case MotionEvent.ACTION_DOWN:
  2. lastX = (int) event.getRawX();
  3. lastY = (int) event.getRawY();
  4. break;

然后获取控件一开始的位置,然后在ACTION_MOVIE中:

  1. int dx =(int)event.getRawX() - lastX;
  2. int dy =(int)event.getRawY() - lastY;
  3. int left = v.getLeft() + dx;
  4. int top = v.getTop() + dy;
  5. int right = v.getRight() + dx;
  6. int bottom = v.getBottom() + dy;
  7. if(left < 0){
  8. left = 0;
  9. right = left + v.getWidth();
  10. }
  11. if(right > screenWidth){
  12. right = screenWidth;
  13. left = right - v.getWidth();
  14. }
  15. if(top < 0){
  16. top = 0;
  17. bottom = top + v.getHeight();
  18. }
  19. if(bottom > screenHeight){
  20. bottom = screenHeight;
  21. top = bottom - v.getHeight();
  22. }
  23. v.layout(left, top, right, bottom);
  24. Log.i("@@@@@@", "position��" + left +", " + top + ", " + right + ", " + bottom);
  25. lastX = (int) event.getRawX();
  26. lastY = (int) event.getRawY();

getLeft()方法得到的是控件左边坐标距离父控件原点(左上角,坐标(0,0))的y轴距离,getRight()是控件右边距离父控件原点的y轴距离,同理,getTop和getButtom是距离的x轴距离。

  1. if(left < 0){
  2. left = 0;
  3. right = left + v.getWidth();
  4. }
  5. if(right > screenWidth){
  6. right = screenWidth;
  7. left = right - v.getWidth();
  8. }
  9. if(top < 0){
  10. top = 0;
  11. bottom = top + v.getHeight();
  12. }
  13. if(bottom > screenHeight){
  14. bottom = screenHeight;
  15. top = bottom - v.getHeight();
  16. }

这里的判断是为了是控件不超出屏幕以外,即:到达边界以后,不能再移动。

  1. v.layout(left, top, right, bottom);

设置View的位置。

有一点忘记说了,就是像ImageView和TextView这些控件,要想实现拖动,要在xml文件中设置它的clickable为true。

  1. android:clickable="true"

就这样,这些就是这个demo的全部内容。

最后,是代码的下载地址:

http://download.csdn.net/detail/aomandeshangxiao/4187376,

http://download.csdn.net/detail/aomandeshangxiao/4189910