游戏SDK应用内悬浮窗的实现(一)
游戏SDK应用内悬浮窗的实现(二)
游戏SDK应用内悬浮窗的实现(三)
游戏SDK应用内悬浮窗的实现(四)
游戏SDK应用内悬浮窗的实现(三)
本篇继前两篇实现了悬浮窗的拖动和点击事件后,继续开发悬浮窗自动贴边功能
跟上两篇一样,只需要用到两个代码文件,FloatIconView与MainActivity
1、MainActivity与之前一样
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FloatIconView floatIconView = new FloatIconView(this);
}
}
2、activity_float_item.xml 也是和之前一样
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/floaticon_btn"
android:layout_width="100dp"
android:layout_height="100dp"
android:src="@drawable/ic_launcher" />
</RelativeLayout>
3、FloatIconView增加方法autoView()
这里的代码只是比上一篇的增加读了一个autoView方法
import android.app.Activity;
import android.content.Context;
import android.graphics.PixelFormat;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.RelativeLayout;
import android.widget.Toast;
/**
* Date:2022-10-18
* Time:11:33
* author:colin
*/
public class FloatIconView extends RelativeLayout {
private final static int LEFT = 0;
private final static int RIGHT = 1;
WindowManager.LayoutParams wmParams;
WindowManager wm;
int screenHeight;
int screenWidth;
float mTouchStartX, mTouchStartY;
float x,y;
private boolean isScroll;
private int dpi;
Activity activity;
public FloatIconView(Activity activity) {
super(activity);
this.activity = activity;
init(activity);
}
public void init(Activity activity) {
View view = LayoutInflater.from(activity).inflate(R.layout.activity_float_item, this);
DisplayMetrics dm = activity.getResources().getDisplayMetrics();
int widthPixels = dm.widthPixels;
int heightPixels = dm.heightPixels;
wm = (WindowManager) activity.getSystemService(Context.WINDOW_SERVICE);
//屏宽
screenWidth = wm.getDefaultDisplay().getWidth();
//屏高
screenHeight = wm.getDefaultDisplay().getHeight();
//通过像素密度来设置按钮的大小
dpi = dpi(dm.densityDpi);
wmParams = new WindowManager.LayoutParams();
wmParams.type = WindowManager.LayoutParams.TYPE_APPLICATION;
wmParams.format = PixelFormat.RGBA_8888;//设置背景图片
wmParams.flags = WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;//
wmParams.gravity = Gravity.LEFT | Gravity.TOP;//
wmParams.x = widthPixels; //设置位置像素
wmParams.y = heightPixels;
wmParams.width = 150; //设置图片大小
wmParams.height = 150;
wm.addView(view, wmParams);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
// 获取相对屏幕的坐标, 以屏幕左上角为原点
x = event.getRawX();
y = event.getRawY();
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
// 获取相对View的坐标,即以此View左上角为原点
mTouchStartX = event.getX();
mTouchStartY = event.getY();
break;
case MotionEvent.ACTION_MOVE:
if (isScroll) {
updateViewPosition();
} else {
// 当前不处于连续滑动状态 则滑动小于图标1/3则不滑动
if (Math.abs(mTouchStartX - event.getX()) > dpi / 3
|| Math.abs(mTouchStartY - event.getY()) > dpi / 3) {
updateViewPosition();
} else {
break;
}
}
isScroll = true;
break;
case MotionEvent.ACTION_UP:
// 拖动
if (isScroll) {
//自动贴边代码增加在此处
autoView();
} else {
//点击悬浮窗
clickView();
}
isScroll = false;
mTouchStartX = mTouchStartY = 0;
break;
}
return true;
}
/**
* 自动移动位置
*/
private void autoView() {
// 得到view在屏幕中的位置
int[] location = new int[2];
getLocationOnScreen(location);
//左侧
if (location[0] < screenWidth / 2 - getWidth() / 2) {
updateViewPosition(LEFT);
} else {
updateViewPosition(RIGHT);
}
}
/**
*更新浮动窗口位置参数
*/
private void updateViewPosition() {
wmParams.x = (int) (x - mTouchStartX);
// 不设置为全屏(状态栏存在) 标题栏是屏幕的1/25
wmParams.y = (int) (y - mTouchStartY - screenHeight / 25);
wm.updateViewLayout(this, wmParams);
}
/**
* 手指释放更新悬浮窗位置
*/
private void updateViewPosition(int l) {
switch (l) {
case LEFT:
wmParams.x = 0;
break;
case RIGHT:
int x = screenWidth - dpi ;
wmParams.x = x;
break;
}
wm.updateViewLayout(this, wmParams);
}
/**
* 根据密度选择控件大小
*/
private int dpi(int densityDpi) {
if (densityDpi <= 120) {
return 36;
} else if (densityDpi <= 160) {
return 48;
} else if (densityDpi <= 240) {
return 72;
} else if (densityDpi <= 320) {
return 96;
}
return 108;
}
//当悬浮按钮被点击
public void clickView(){
Toast.makeText(activity,"悬浮按钮被点击!",Toast.LENGTH_SHORT).show();
}
}
最终效果图:
好了,悬浮窗开发的基本功能已经大概完成。用于手游SDK的话还需要增加各种优化的功能,例如悬浮窗的半隐藏功能,点击悬浮窗弹出菜单栏等。下一篇将开发悬浮窗数秒不操作后自动半隐藏的功能
附上最后的资源链接
https://download.csdn.net/download/gsrkuang/86812918
积分不够的话可以联系我拿285184329