Android悬浮窗的简单实现

2.原理

Window我们应该很熟悉,它是一个接口类,具体的实现类为PhoneWindow,它可以对View进行管理。WindowManager是一个接口类,继承自ViewManager,从名称就知道它是用来管理Window的,它的实现类是WindowManagerImpl。如果我们想要对Window(View)进行添加、更新和删除操作就可以使用WindowManagerWindowManager会将具体的工作交由WindowManagerService处理。这里我们只需要知道WindowManager能用来管理Window就好。

WindowManager是一个接口类,继承自ViewManagerViewManager中定义了3个方法,分布用来添加、更新和删除View,如下所示:

public interface ViewManager {

public void addView(View view, ViewGroup.LayoutParams params);

public void updateViewLayout(View view, ViewGroup.LayoutParams params);

public void removeView(View view);

}

WindowManager也继承了这些方法,而这些方法传入的参数都是View类型,说明了Window是以View的形式存在的。

3.具体实现

3.1浮窗布局

悬浮窗的简易布局如下的可参考下面的layout_floating_window.xml文件。顶层深色部分的FrameLayout布局是用来实现悬浮窗的拖拽功能的,点击右上角ImageView可以实现关闭悬浮窗,剩下区域显示内容,这里只是简单地显示文本内容,不做复杂的东西,故只设置TextView。

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout

xmlns:android=“http://schemas.android.com/apk/res/android”

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

android:layout_width=“match_parent”

android:layout_height=“match_parent”

android:orientation=“vertical”>

<FrameLayout

android:id=“@+id/layout_drag”

android:layout_width=“match_parent”

android:layout_height=“15dp”

android:background=“#dddddd”>

<androidx.appcompat.widget.AppCompatImageView

android:id=“@+id/iv_close”

android:layout_width=“15dp”

android:layout_height=“15dp”

android:layout_gravity=“end”

android:src=“@drawable/img_delete”/>

  • 7
    点赞
  • 30
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,关于 Android 浮窗广告的实现,需要用到 AndroidWindowManager 类,下面是一个简单实现方式: 1. 在 AndroidManifest.xml 文件中添加权限: ``` <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" /> ``` 2. 创建一个悬浮窗的布局文件,例如 float_window.xml: ```xml <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/float_layout" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@android:color/transparent" android:orientation="vertical"> <!-- 在这里添加你的广告布局 --> </LinearLayout> ``` 3. 创建一个 Service 类用于控制悬浮窗的显示和隐藏: ```java public class FloatWindowService extends Service { private WindowManager windowManager; private LinearLayout floatLayout; private WindowManager.LayoutParams params; @Override public void onCreate() { super.onCreate(); windowManager = (WindowManager) getSystemService(WINDOW_SERVICE); // 设置悬浮窗的布局参数 params = new WindowManager.LayoutParams( WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.TYPE_SYSTEM_ALERT, WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE, PixelFormat.TRANSLUCENT); // 设置悬浮窗的位置 params.gravity = Gravity.TOP | Gravity.LEFT; params.x = 0; params.y = 0; // 加载悬浮窗的布局文件 LayoutInflater inflater = LayoutInflater.from(this); floatLayout = (LinearLayout) inflater.inflate(R.layout.float_window, null); // 在这里添加你的广告布局 // 将悬浮窗添加到 WindowManagerwindowManager.addView(floatLayout, params); } @Override public void onDestroy() { super.onDestroy(); // 在 Service 销毁时移除悬浮窗 if (floatLayout != null) { windowManager.removeView(floatLayout); } } @Nullable @Override public IBinder onBind(Intent intent) { return null; } } ``` 4. 在需要显示悬浮窗的地方启动 Service: ```java startService(new Intent(this, FloatWindowService.class)); ``` 这样就可以实现一个简单Android 浮窗广告了。当然,你也可以根据自己的需求对悬浮窗进行更加复杂的实现

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值