Android——超简单悬浮窗使用教程

Android——超简单悬浮窗使用教程

完全自定义悬浮窗,保证100%学会的超简单悬浮窗

先看看效果图:

图1 图2 图3

图1只需要31行代码即可完成。

我们来看看这些都是如何实现的

在使用GT库里封装的架构当然需要先依赖好GT库:

详细依赖教程请参看

GitHub - 1079374315/GTContribute to 1079374315/GT development by creating an account on GitHub.https://github.com/1079374315/GT

使用GT 悬浮窗教程

第一步:自定义的xml布局 demo_floating_window

xmlns:app="http://schemas.android.com/apk/res-auto"

android:layout_width="wrap_content"

android:layout_height="wrap_content">

android:id="@+id/view_bg"

android:layout_width="300dp"

android:layout_height="200dp"

android:background="#5B77D5FF"

app:layout_constraintEnd_toEndOf="parent"

app:layout_constraintStart_toStartOf="parent"

app:layout_constraintTop_toTopOf="parent">

android:id="@+id/tv_back"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:paddingLeft="10dp"

android:paddingRight="10dp"

android:text="×"

android:textColor="#99FFFFFF"

android:textSize="28sp"

android:textStyle="bold"

app:layout_constraintEnd_toEndOf="parent"

app:layout_constraintTop_toTopOf="parent" />

android:id="@+id/tv_data"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:background="#8D03B1FF"

android:padding="10dp"

android:text="简易自定义对话框\n支持返回数据\n支持监听返回键\n用法与 Fragment 毫无差异\n"

android:textColor="#A4FFFFFF"

android:textStyle="bold"

app:layout_constraintBottom_toTopOf="@+id/btn_ok"

app:layout_constraintEnd_toEndOf="parent"

app:layout_constraintStart_toStartOf="parent"

app:layout_constraintTop_toBottomOf="@+id/tv_back" />

android:id="@+id/btn_ok"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="好的"

app:layout_constraintBottom_toBottomOf="parent"

app:layout_constraintEnd_toStartOf="@+id/btn_cancel"

app:layout_constraintStart_toStartOf="parent" />

android:id="@+id/btn_cancel"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="取消"

app:layout_constraintBottom_toBottomOf="@+id/btn_ok"

app:layout_constraintEnd_toEndOf="parent"

app:layout_constraintStart_toEndOf="@+id/btn_ok" />

第二步:加载悬浮窗布局

//加载布局

@GT.Annotations.GT_AnnotationFloatingWindow(R.layout.demo_floating_window)

public class DemoFloatingWindow extends GT.GT_FloatingWindow.AnnotationFloatingWindow {

@Override

protected void initView(View view) {

super.initView(view);

setDrag(true);//设置可拖动

//setEventPenetration(true);//设置悬浮窗可点击穿透 ,不可以和 setDrag 同时触发

}

@GT.Annotations.GT_Click({R.id.btn_ok, R.id.tv_back, R.id.btn_cancel})

public void onClick(View view) {

switch (view.getId()) {

case R.id.btn_ok:

GT.toast("单击了ok");

break;

case R.id.tv_back:

case R.id.btn_cancel:

finish();//关闭当前悬浮窗

break;

}

}

}

第三步:使用悬浮窗(这里的布局因为只有一个按钮组件,故不贴出布局了)

public class MainActivity extends AppCompatActivity {

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

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

@Override

public void onClick(View v) {

GT.startFloatingWindow(MainActivity.this, DemoFloatingWindow.class);//启动悬浮窗

}

});

}

}

第四步:添加悬浮窗权限 与 注册写好的悬浮窗 (AndroidManifest.xml)

可直接复制粘贴:

然后就完工,可直接运行了

总结:熟悉GT库的是不是感觉特别熟悉,没错,GT内的Activity、Fragment、DialogFragment、FloatingWindow 的使用方法与结构都是一模一样的,也就是说只要你学会了其中一个,那就等同于其他的你都学会了。

非常简便的 切换三种常用悬浮窗层级:

1.绑定 Activity 层级悬浮窗 :悬浮窗 根据当前寄生的Activity进行显示和隐藏

2.桌面级悬浮窗:可在Android桌面和各种App之上显示 (缺点,无法在系统设置页面显示)

3.绑定 无障碍 层级悬浮窗:几乎所有界面都能显示包括系统设置界面。

绑定 Activity和无障碍服务 层级悬浮窗:

在之前悬浮窗代码基础上增加绑定代码

//加载布局

@GT.Annotations.GT_AnnotationFloatingWindow(R.layout.demo_floating_window)

public class DemoFloatingWindow extends GT.GT_FloatingWindow.AnnotationFloatingWindow {

//提供给外界赋值

public static WindowManager windowManagerDemo;

//绑定 activity的 WindowManager

public WindowManager bindingActivityWindowManager() {

return windowManagerDemo;

}

/**

* 设置悬浮窗类型为 Activity 层级的

* 这里设置的悬浮窗类型,可以根据上面的 bindingActivityWindowManager 方法

* 绑定不同类型的 WindowManager 进行动态改变,万能修改

* @return

*/

public int getSuspensionType() {

//这个是app内 Activity 类型的悬浮窗

return WindowManager.LayoutParams.TYPE_APPLICATION;

//下面注释的是 无障碍服务类型的 悬浮窗

// return WindowManager.LayoutParams.TYPE_ACCESSIBILITY_OVERLAY;

}

@Override

protected void initView(View view) {

super.initView(view);

setDrag(true);//设置可拖动

//setEventPenetration(true);//设置悬浮窗可点击穿透

//(注意:悬浮窗穿透API会覆盖掉 getSuspensionType方法的设置,故,进行注释掉)

}

@Override

public void loadData(Context context, Intent intent, View view) {

super.loadData(context, intent, view);

//动态设置悬浮窗位置

setXY(100,100);

}

@GT.Annotations.GT_Click({R.id.btn_ok, R.id.tv_back, R.id.btn_cancel})

public void onClick(View view) {

switch (view.getId()) {

case R.id.btn_ok:

GT.toast("单击了ok");

break;

case R.id.tv_back:

case R.id.btn_cancel:

finish();//关闭当前悬浮窗

break;

}

}

}

展示绑定activity属性后,进行启动悬浮窗

public class MainActivity extends AppCompatActivity {

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

//将Activity 的 WindowManager 给与 悬浮窗

DemoFloatingWindow.windowManagerDemo = getWindowManager();

//单击启动悬浮窗

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

@Override

public void onClick(View v) {

GT.startFloatingWindow(MainActivity.this, DemoFloatingWindow.class);//启动悬浮窗

}

});

}

}

绑定 无障碍服务的 流程一模一样,只是有两处地方需要更改。

展示绑定无障碍服务属性后,进行启动悬浮窗

public class DemoServer extends GT.DarknessMagic.AccessibilityServiceBase {

@Override

public void onCreate() {

super.onCreate();

//将无障碍服务的 WindowManager 绑定到悬浮窗上

DemoFloatingWindow.windowManagerDemo = (WindowManager) getSystemService(WINDOW_SERVICE);

//然后启动悬浮窗

/**

* 由于悬浮窗是一个独立类的,在悬浮窗上无法直接启动,

* 但启动悬浮窗方式有很多种方法:

* 1.直接交给MainActivity 去启动 (推荐)

* 2.在 Activity页面(不一定要Activity)其他常驻型class都可以 注册 GT.EventBus 监听(不明白这点的可以看博主博客)

* 然后在悬浮窗里可以直接 发布事件祈祷间接启动悬浮窗 (推荐)

* 3.在 Activity 内注册内部监听,然后在这里触发这个监听即可 (推荐)

* 4.在 Activity 注册自定义广播,在这里触发自定义广播 (不推荐)

* 5.HTTP/TCP/UDP/或线程循环监听,这些方法都可以,只是都 (不推荐)

* 6.启动的方法很多,看场景用对应的启动的方法

*/

}

@Override

protected void initView(int action, String packageName, AccessibilityNodeInfo rootInfo, AccessibilityEvent event) {

GT.err("packageName:" + packageName);//打印当前屏幕操作的app包名

}

}

如果想了解更多无障碍知识的可以戳一下这个:

Android——超简单无障碍服务_android 无障碍服务-CSDN博客文章浏览阅读8k次,点赞4次,收藏20次。超简单100%学会的无障碍服务_android 无障碍服务https://blog.csdn.net/qq_39799899/article/details/122214748?spm=1001.2014.3001.5502

补充API:

补充使用API:

setEventPenetration //设置触摸事件是否穿透悬浮窗

setType_screenType //设置悬浮窗布局模式

getWindowManager //获得悬浮窗最原始窗口,

setWidth//设置窗口宽度

setHeight//设置窗口高度

setBindingApp//设置将绑定当前APP,如果APP不在前台那就隐藏悬浮窗

setGetFocus//是否获取焦点,获取焦点后才能进行弹出软键盘

updateView(int width, int height)//更新View

setDrag//设置悬浮窗可拖动

setLayoutParamsXY//设置悬浮窗累加位置

setXY//设置悬浮窗的位置,注意:此方法需要在 重写loadData() 里进行调用

show//显示通知栏

hide//隐藏通知类

isShow//当前悬浮窗是否显示

finish//销毁悬浮窗

setEventPenetration(true);//设置悬浮窗可点击穿透

(注意:悬浮窗穿透API会覆盖掉 getSuspensionType方法的设置)

糖豆:如果想要图2、图3的源码,请直接下载最新GT库,GT库中的 util 目录中就是源码。 启动GT模拟手机版悬浮窗的代码如下:

点个关注点个赞呗(〃'▽'〃),关注博主最新发布库:GitHub - 1079374315/GT

💡 相关推荐

iPhone 自动删除 App 怎么办?教你 2 个方法解决!
365足球体育网站

iPhone 自动删除 App 怎么办?教你 2 个方法解决!

📅 10-08 👀 5101
如何下载Ons模拟器?官方网站在哪里?
《绝地求生》怎么快速找人,只要学会这些就可以了
365bet体育投注在线

《绝地求生》怎么快速找人,只要学会这些就可以了

📅 09-26 👀 8506