android 可移动控件

时间:2019-02-10 10:13:27
【文件属性】:

文件名称:android 可移动控件

文件大小:7.9MB

文件格式:ZIP

更新时间:2019-02-10 10:13:27

android

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<0){//判断控件是否半隐藏状态 lParams.leftMargin = 0; }else if(lParams.topMargin<0){ lParams.topMargin=0; }else if(X -_xDelta> 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<0 - width/2){ lParams.leftMargin = 0- width/2; }else if(X -_xDelta> screenWidth - width/2){ lParams.leftMargin = screenWidth - width/2; }else{ lParams.leftMargin = X - _xDelta; } if(Y - _yDelta<0 - height/2){ lParams.topMargin = 0 - height/2; }else 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; }


网友评论