导航显示按钮

This commit is contained in:
wangcongtao
2020-08-12 10:41:30 +08:00
parent a4f50facae
commit c55bf3bb23
12 changed files with 90 additions and 30 deletions

View File

@@ -29,6 +29,11 @@ class DialogImpl implements IWindowManagerView {
return dialog.isShowing();
}
@Override
public void handleTouchEvent(OnViewClickListener listener) {
// do nothings.
}
@Override
public void show() {
dialog.show();

View File

@@ -1,18 +1,27 @@
package com.mogo.module.common.wm;
import android.view.View;
interface IWindowManagerView {
/**
* 初始化
*
* @param params contentView包装类
*/
void init(WindowManagerView.WMViewParams params);
void init( WindowManagerView.WMViewParams params );
/**
* 是否显示
*
* @return true - 显示中
*/
boolean isShowing();
/**
* 支持手势拖动,该操作只支持触发点击操作
*/
void handleTouchEvent(OnViewClickListener listener);
/**
* 显示
*/
@@ -22,4 +31,8 @@ interface IWindowManagerView {
* 隐藏
*/
void hide();
interface OnViewClickListener {
void onClick( View view );
}
}

View File

@@ -3,12 +3,9 @@ package com.mogo.module.common.wm;
import android.content.Context;
import android.graphics.PixelFormat;
import android.os.Build;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.WindowManager;
import com.mogo.module.common.utils.CarSeries;
import com.mogo.utils.WindowUtils;
/**
* 采用windowManager实现接口
*/
@@ -19,10 +16,13 @@ class WindowManagerImpl implements IWindowManagerView {
private WindowManagerView.WMViewParams mParams;
private boolean isShowing;
private float mLastX, mLastY;
private int mOldOffsetX, mOldOffsetY;
@Override
public void init(WindowManagerView.WMViewParams params) {
public void init( WindowManagerView.WMViewParams params ) {
mParams = params;
mWindowManager = (WindowManager) mParams.mContext.getApplicationContext().getSystemService( Context.WINDOW_SERVICE );
mWindowManager = ( WindowManager ) mParams.mContext.getApplicationContext().getSystemService( Context.WINDOW_SERVICE );
mLayoutParams = new WindowManager.LayoutParams();
if ( Build.VERSION.SDK_INT >= Build.VERSION_CODES.O ) {
mLayoutParams.type = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY;
@@ -35,8 +35,8 @@ class WindowManagerImpl implements IWindowManagerView {
mLayoutParams.width = mParams.mWidth;
mLayoutParams.height = mParams.mHeight;
mLayoutParams.x = mParams.mX;
mLayoutParams.y = mParams.mY;
mOldOffsetX = mLayoutParams.x = mParams.mX;
mOldOffsetY = mLayoutParams.y = mParams.mY;
mLayoutParams.dimAmount = 0.5f;
}
@@ -45,18 +45,46 @@ class WindowManagerImpl implements IWindowManagerView {
return isShowing;
}
@Override
public void handleTouchEvent(OnViewClickListener listener) {
mParams.mContentView.setOnTouchListener( ( v, event ) -> {
final int action = event.getAction();
float x = event.getX();
float y = event.getY();
if ( action == MotionEvent.ACTION_DOWN ) {
mLastX = x;
mLastY = y;
} else if ( action == MotionEvent.ACTION_MOVE ) {
mLayoutParams.x += ( int ) ( x - mLastX ) / 3; // 减小偏移量,防止过度抖动
mLayoutParams.y += ( int ) ( y - mLastY ) / 3; // 减小偏移量,防止过度抖动
mWindowManager.updateViewLayout( mParams.mContentView, mLayoutParams );
} else if ( action == MotionEvent.ACTION_UP ) {
int newOffsetX = mLayoutParams.x;
int newOffsetY = mLayoutParams.y;
// 只要按钮一动位置不是很大,就认为是点击事件
if ( Math.abs( mOldOffsetX - newOffsetX ) <= 20
&& Math.abs( mOldOffsetY - newOffsetY ) <= 20 ) {
listener.onClick( mParams.mContentView );
}
mOldOffsetX = newOffsetX;
mOldOffsetY = newOffsetY;
}
return true;
} );
}
@Override
public void show() {
if(!isShowing){
if ( !isShowing ) {
isShowing = true;
mWindowManager.addView(mParams.mContentView,mLayoutParams);
mWindowManager.addView( mParams.mContentView, mLayoutParams );
}
}
@Override
public void hide() {
if (isShowing && mParams != null) {
mWindowManager.removeView(mParams.mContentView);
if ( isShowing && mParams != null ) {
mWindowManager.removeView( mParams.mContentView );
isShowing = false;
}
}

View File

@@ -33,6 +33,10 @@ public class WindowManagerView {
return mParams.mContentView.findViewById( id );
}
public void attachTouchEvent( IWindowManagerView.OnViewClickListener listener ) {
mManagerView.handleTouchEvent(listener);
}
public void show() {
mManagerView.show();
}