進入商顯行業半年,看過每一家商顯會議系統的側拉菜單幾乎都如出一轍,但是發現友商的似乎可以實現上下移動,所以這邊也給自己的側邊欄實現上下自由移動功能,效果如圖
實現關鍵代碼:
給想實現的控件設置onTouch事件
this.setOnTouchListener(new View.OnTouchListener(){
float lastY;
@Override
public boolean onTouch(View view, MotionEvent event) {
float y = event.getRawY();
switch (event.getAction()){
case MotionEvent.ACTION_DOWN:
lastY = y; //記錄按下時的Y座標
break;
case MotionEvent.ACTION_MOVE:
//獲取移動後的座標
int dy = (int) (y - lastY);//記錄總共移動的座標
Log.i("gyx","dy="+dy);
FloatWindowManager.updateMenuLeftParams(mContext,dy);//實時更新窗口位置
FloatWindowManager.updateMenuParams(mContext,dy);
break;
case MotionEvent.ACTION_UP:
Log.e("gyx", "擡起了");
FloatWindowManager.updateDy(); //記錄擡起時控件的Y座標
break;
}
return true;
}
});
private static void initMenuLeftParams(Context context, int screenWidth, int screenHeight) {
if (menuParamsLeft == null) {
menuParamsLeft = new LayoutParams();
menuParamsLeft.x = 0;
// 默認位置
int defaultY = 294;
menuParamsLeft.y = (int) SPUtil.getData(context, DrawConsts.LAST_POINT_Y_KEY, defaultY);
updateDy(); //初始化窗口的時候記錄默認的Y座標
menuParamsLeft.type = LayoutParams.TYPE_PHONE;
// 所有其它程序是可點擊的,懸浮窗不獲取焦點
menuParamsLeft.flags = LayoutParams.FLAG_NOT_TOUCH_MODAL
| LayoutParams.FLAG_NOT_FOCUSABLE;
menuParamsLeft.format = PixelFormat.RGBA_8888;
menuParamsLeft.gravity = Gravity.LEFT | Gravity.TOP;
menuParamsLeft.width = ControlMenuLayout.viewWidth;
menuParamsLeft.height = ControlMenuLayout.viewHeight;
}
}
static int lastY;
static public void updateDy() {
lastY = menuParamsLeft.y;
}
public static void updateMenuLeftParams(Context context, int y) {
if (menuParamsLeft != null) {
// 默認位置
menuParamsLeft.y = lastY + y;
getWindowManager(context).updateViewLayout(menuWindowLeft, menuParamsLeft);
}
}
代碼很簡單,有不懂的朋友評論回覆哦。