1. 首页
  2. 移动开发
  3. Android
  4. android 可移动控件

android 可移动控件

上传者: 2019-01-02 06:37:43上传 ZIP文件 7.9MB 热度 57次
android 可移动控件 /** * 用于判断view的移动 * @param view * @param event * @return */ @Override public boolean onTouch(View view, MotionEvent event) { final int X = (int) event.getRawX(); final int Y = (int) event.getRawY(); switch (event.getAction() & MotionEvent.ACTION_MASK) { case MotionEvent.ACTION_DOWN: if(lParams.leftMargin screenWidth - width){ lParams.leftMargin = screenWidth-width; } _xDelta = X - lParams.leftMargin; _yDelta = Y - lParams.topMargin; StartX = X; StartY = Y; // Log.e(TAG,"_xDelta "+_xDelta+" _yDelta "+_yDelta+" lParams.rightMargin "+lParams.rightMargin); break; case MotionEvent.ACTION_UP: //关键部分:移动距离较小,视为onclick点击行为 if (Math.abs(X - StartX) < 1.5 && Math.abs(Y - StartY) < 1.5){//计算器弹出 //这放点击后的逻辑代码 } break; case MotionEvent.ACTION_POINTER_DOWN: break; case MotionEvent.ACTION_POINTER_UP: break; case MotionEvent.ACTION_MOVE: // RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) view // .getLayoutParams(); if(X - _xDelta screenWidth - width/2){ lParams.leftMargin = screenWidth - width/2; }else{ lParams.leftMargin = X - _xDelta; } if(Y - _yDelta screenHeight - height-NavigationBarheight){ lParams.topMargin = screenHeight - height-NavigationBarheight; }else{ lParams.topMargin = Y - _yDelta; } lParams.rightMargin = -250; lParams.bottomMargin = -250; view.setLayoutParams(lParams); break; } background.invalidate(); return true; }
用户评论