其他分享
首页 > 其他分享> > Android 实现全局浮动弹窗,95%Android开发者已收藏的十大开源库

Android 实现全局浮动弹窗,95%Android开发者已收藏的十大开源库

作者:互联网

private WindowManager.LayoutParams mLayoutParams;

public SmallWindowView getWindowView() {

return mWindowView;

}

public WindowManager getWindowManager() {

return mWindowManager;

}

public WindowManager.LayoutParams getLayoutParams() {

return mLayoutParams;

}

@Override

public void onCreate() {

super.onCreate();

initSmallViewLayout();

}

public void initSmallViewLayout() {

mWindowView = (SmallWindowView) LayoutInflater.from(this).inflate(R.layout.small_window, null);

mWindowManager = (WindowManager) getSystemService(Context.WINDOW_SERVICE);

mLayoutParams = new WindowManager.LayoutParams(

getResources().getDimensionPixelSize(R.dimen.small_window_size), // 120dp

getResources().getDimensionPixelSize(R.dimen.small_window_size),

WindowManager.LayoutParams.TYPE_SYSTEM_ALERT,

WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,

PixelFormat.TRANSLUCENT);

mLayoutParams.gravity = Gravity.NO_GRAVITY;

mWindowView.setWm(mWindowManager);

mWindowView.setWmParams(mLayoutParams);

}

public void showWindowView() {

if (mWindowManager != null && mWindowView.getWindowId() == null) {

mWindowManager.addView(mWindowView, mLayoutParams);

}

}

public void dismissWindowView() {

if (mWindowManager != null && mWindowView != null && mWindowView.getWindowId() != null) {

mWindowManager.removeView(mWindowView);

}

}

}

定义一个View组件,内部处理滑动改变窗口位置参数:

public class SmallWindowView extends LinearLayout {

private final static String TAG = SmallWindowView.class.getSimpleName();

private final int screenHeight;

private final int screenWidth;

private int statusHeight;

private float mTouchStartX;

private float mTouchStartY;

private float mLastRawX;

private float mLastRawY;

private WindowManager wm;

public WindowManager.LayoutParams wmParams;

public WindowManager getWm() {

return wm;

}

public void setWm(WindowManager wm) {

this.wm = wm;

}

public WindowManager.LayoutParams getWmParams() {

return wmParams;

}

public void setWmParams(WindowManager.LayoutParams wmParams) {

this.wmParams = wmParams;

this.wmParams.x = 0;//screenWidth/2; // 窗口先贴附在右边

}

public SmallWindowView(Context context) {

this(context, null);

}

public SmallWindowView(Context context, AttributeSet attrs) {

this(context, attrs, 0);

}

public SmallWindowView(final Context context, AttributeSet attrs, int defStyle) {

super(context, attrs, defStyle);

statusHeight = getStatusHeight(context);

DisplayMetrics dm = getResources().getDisplayMetrics();

screenHeight = dm.heightPixels;

screenWidth = dm.widthPixels;

addOnAttachStateChangeListener(new OnAttachStateChangeListener() {

@Override

public void onViewAttachedToWindow(View v) {

//窗口内部按钮响应点击跳转打开新页面

findViewById(R.id.btn).setOnClickListener(new OnClickListener() {

@Override

public void onClick(View v) {

Intent intent = new Intent(context, SecondActivity.class);

intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

context.startActivity(intent);

}

});

removeOnAttachStateChangeListener(this);

}

@Override

public void onViewDetachedFromWindow(View v) {

}

});

}

/**

*/

public static int getStatusHeight(Context context) {

int statusHeight = -1;

try {

Class clazz = Class.forName(“com.android.internal.R$dimen”);

Object object = clazz.newInstance();

int height = Integer.parseInt(clazz.getField(“status_bar_height”)

.get(object).toString());

statusHeight = context.getResources().getDimensionPixelSize(height);

} catch (Exception e) {

e.printStackTrace();

}

return statusHeight;

}

@Override

public boolean onTouchEvent(MotionEvent event) {

switch (event.getAction()) {

case MotionEvent.ACTION_DOWN:

// 触摸点在View内的相对x坐标 相对Y坐标

//mTouchStartX = event.getX();

//mTouchStartY = event.getY();

// 触摸点相对屏幕的x坐标 y坐标

mLastRawX = event.getRawX();

mLastRawY = event.getRawY() - statusHeight;

Log.e(TAG, "startX = " + mLastRawX + " startY = " + mLastRawY);

break;

case MotionEvent.ACTION_MOVE:

updateViewPosition(event.getRawX(), event.getRawY());

mLastRawX = event.getRawX();

mLastRawY = event.getRawY();

break;

case MotionEvent.ACTION_UP:

break;

default:

break;

}

return true;

}

/** 更新浮动窗口位置参数 */

private void updateViewPosition(float x, float y) {

wmParams.gravity = Gravity.NO_GRAVITY;

//计算移动距离

int dx = (int) (x - mLastRawX);

int dy = (int) (y - mLastRawY);

Log.e(TAG, "updateViewPosition: dx = " + dx + " dy = " + dy);

//默认是以屏幕中心点为(0,0)起始坐标

wmParams.x += dx;

wmParams.y += dy;

Log.e(TAG, "updateViewPosition: wmParams.x = " + wmParams.x + " wmParams.y = " + wmParams.y);

wm.updateViewLayout(this, wmParams);

}

}

定义一个基类Activity使用,并处理权限申请:

public class BaseActivity extends AppCompatActivity {

private WindowManager wm;

private SmallWindowView windowView;

private WindowManager.LayoutParams mLayoutParams;

private int OVERLAY_PERMISSION_REQ_CODE = 2;

private boolean isRange = false;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

wm = ((App)getApplication()).getWindowManager();

windowView = ((App)getApplication()).getWindowView();

mLayoutParams = ((App)getApplication()).getLayoutParams();

}

public void alertWindow() {

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { // 7.0 以上需要引导用去设置开启窗口浮动权限

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { // 8.0 以上type需要设置成这个

mLayoutParams.type = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY;

}

requestDrawOverLays();

} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { // 6.0 动态申请

ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.SYSTEM_ALERT_WINDOW}, 1);

}

}

@Override

public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {

super.onRequestPermissionsResult(requestCode, permissions, grantResults);

if (requestCode == 1 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {

if (wm != null && windowView.getWm() == null) {

wm.addView(windowView, mLayoutParams);

}

} else {

Toast.makeText(this, "权限

《Android学习笔记总结+最新移动架构视频+大厂安卓面试真题+项目实战源码讲义》

【docs.qq.com/doc/DSkNLaERkbnFoS0ZF】 完整内容开源分享

申请失败", Toast.LENGTH_SHORT).show();

}

}

标签:wmParams,int,void,WindowManager,private,Android,95%,public,弹窗
来源: https://blog.csdn.net/m0_64603743/article/details/121899682