导航:首页 > 操作系统 > android悬浮按钮实现的

android悬浮按钮实现的

发布时间:2022-12-29 04:15:22

‘壹’ 如何实现android的圆形悬浮球

Android 的一种控件:FloatActionButton,直接使用

‘贰’ android 系统级的悬浮窗实现

当我们在使用的app的时候,如果需要实时观测到某个功能的实时进度并且不影响其他的操作的时候或者不影响使用其他应用的时候,系统级的悬浮球是个非常不错的选择。

public class QueueUpFloatService extends Service {

/**

* 启动服务并传值

*

* @param activity 启动服务的activity

* @param modeBean 数据对象

*/

public static void launchService(Activity activity, ModeBean modeBean) {

try {

        Intent intent =new Intent(activity, QueueUpFloatService.class);

        Bundle bundle =new Bundle();

        bundle.putSerializable(KEY_MODEL, modeBean);

        intent.putExtras(bundle);

        activity.startService(intent);

    }catch (Exception e) {

        e.printStackTrace();

    }

}

    @Override

    public void onCreate() {

            super.onCreate();

    }

    @Override

    public IBinder onBind(Intent intent) {

            return null;

    }

    @Override

    public int onStartCommand(Intent intent, int flags, int startId) {

            return super.onStartCommand(intent, flags, startId);

    }

    @Override

    public void onDestroy() {

            super.onDestroy();

    }

}

@Override

public void onCreate() {

    super.onCreate();

    //加一点简单的动画 

    buttonScale = (ScaleAnimation) AnimationUtils.loadAnimation(this, R.anim.anim_float);

    windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);

    layoutParams =new WindowManager.LayoutParams();

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {

            layoutParams.type = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY;

    }else {

            layoutParams.type = WindowManager.LayoutParams.TYPE_PHONE;

    }

    layoutParams.format = PixelFormat.RGBA_8888;

    layoutParams.gravity = Gravity.LEFT | Gravity.TOP;

    layoutParams.flags = WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL |             WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;

    layoutParams.width = ScreenUtils.dp2px(66);

    layoutParams.height = ScreenUtils.dp2px(66);

    layoutParams.x = ScreenUtils.getRealWidth() - ScreenUtils.dp2px(60);

    layoutParams.y = ScreenUtils.deviceHeight() *2 /3;

}

@Override

public int onStartCommand(Intent intent, int flags, int startId) {

    ModeBean modeBean = (ModeBean) intent.getExtras().getSerializable(KEY_MODEL);

    LayoutInflater layoutInflater = LayoutInflater.from(this);

    floatView = layoutInflater.inflate(R.layout.view_float, null);

    RelativeLayout rlFloatParent =floatView.findViewById(R.id.rl_float_parent);

    rlFloatParent.startAnimation(buttonScale);

    TextView tvIndex =floatView.findViewById(R.id.tv_queue_index);

    tvIndex.setText(modeBean.title);

    floatView.findViewById(R.id.iv_close_float).setOnClickListener(v -> stopSelf());

    //修改悬浮球的滑动实现

    floatView.setOnTouchListener(new FloatingOnTouchListener());

    windowManager.addView(floatView, layoutParams);

    return super.onStartCommand(intent, flags, startId);

}

private class View.OnTouchListener {

    private int x;

    private int y;

    private long downTime;

    @SuppressLint("ClickableViewAccessibility")

    @Override

    public boolean onTouch(View view, MotionEvent event) {

            switch (event.getAction()) {

                case MotionEvent.ACTION_DOWN:

                        downTime = System.currentTimeMillis();

                        x = (int) event.getRawX();

                        y = (int) event.getRawY();

                        break;

                case MotionEvent.ACTION_MOVE:

                        int nowX = (int) event.getRawX();

                        int nowY = (int) event.getRawY();

                        int movedX = nowX -x;

                        int movedY = nowY -y;

                        x = nowX;

                        y = nowY;

                        layoutParams.x =layoutParams.x + movedX;

                        layoutParams.y =layoutParams.y + movedY;

                        windowManager.updateViewLayout(view, layoutParams);

                        break;

                case MotionEvent.ACTION_UP:

                        /* *

                        * 这里根据手指按下和抬起的时间差来判断点击事件还是滑动事件

                        * */

                        if ((System.currentTimeMillis() -downTime) <200) {

                                //检测应用在前台还是后台

                                if (AppUtils.isAppIsInBackground()) {

                                        AppUtils.moveToFront(CloseActivityUtils.activityList.get(CloseActivityUtils.activityList.size() -1).getClass());

                                } else {

                                        //检测栈顶是否为SecondActivity 不是就打开SecondActivity

                                      if (!CloseActivityUtils.activityList.get(CloseActivityUtils.activityList.size() -1)

                                                .getClass().getSimpleName().contains("SecondActivity")) {

                                        SecondActivity.launchActivity(CloseActivityUtils.activityList.get(CloseActivityUtils.activityList.size() -1));

                                }

                      }

            }

                        break;

            default:

                        break;

        }

           return false;

    }

}

        @Override

        public void onDestroy() {

            super.onDestroy();

            if (null ==floatView) {

                return;

               }

            windowManager.removeView(floatView);

            windowManager=null;

}

‘叁’ Android桌面悬浮窗效果怎么实现

可以模仿360手机卫士悬浮窗的那份代码的基础上继续开发。
打开手机卫士主界面,然后上拉,然后点击快捷设置,然后点击桌面悬浮窗,就可以将360手机卫士安卓版桌面浮窗调出来了,具体步骤如下:
1、安装最新的360手机卫士。
2、点开隐私保护,打开右上角的三个点。
3、点开卫士设置,点开悬浮窗。
4、开启内存清理悬浮窗, 选择显示样式,安仔样式或是加速球。
5、可以选择仅在桌面显示,若开启则悬浮窗只出现在桌面,若关闭则悬浮窗会跟随打开页面一直出现。
6、可以同时开启拖动清理内存,这样直接拖动悬浮窗图标,就可以轻松清理内存了。

‘肆’ android 怎么设置 悬浮activity

Android悬浮窗实现

下面实现来自于android学习手册,里面有实现的可运行的例子还有源码。android学习手册包含9个章节,108个例子,源码文档随便看,例子都是可交互,可运行,源码采用android studio目录结构,高亮显示代码,文档都采用文档结构图显示,可以快速定位。360手机助手中下载,图标上有贝壳
实现基础
Android悬浮窗实现使用WindowManager ,WindowManager介绍
通过Context.getSystemService(Context.WINDOW_SERVICE)可以获得 WindowManager对象。
每一个WindowManager对象都和一个特定的 Display绑定。
想要获取一个不同的display的WindowManager,可以用 createDisplayContext(Display)来获取那个display的 Context,之后再使用:Context.getSystemService(Context.WINDOW_SERVICE)来获取WindowManager。
使用WindowManager可以在其他应用最上层,甚至手机桌面最上层显示窗口。
调用的是WindowManager继承自基类的addView方法和removeView方法来显示和隐藏窗口。具体见后面的实例。
另:API 17推出了Presentation,它将自动获取display的Context和WindowManager,可以方便地在另一个display上显示窗口。

WindowManager实现悬浮窗需要声明权限
首先在manifest中添加如下权限:
<!-- 显示顶层浮窗 --><uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />

注意:在MIUI上需要在设置中打开本应用的”显示悬浮窗”开关,并且重启应用,否则悬浮窗只能显示在本应用界面内,不能显示在手机桌面上。

服务获取和基本参数设置
[java] view plain print?
// 获取应用的Context
mContext = context.getApplicationContext();
// 获取WindowManager
mWindowManager = (WindowManager) mContext
.getSystemService(Context.WINDOW_SERVICE);
参数设置:
final WindowManager.LayoutParams params = new WindowManager.LayoutParams();
// 类型
params.type = WindowManager.LayoutParams.TYPE_SYSTEM_ALERT;
// WindowManager.LayoutParams.TYPE_SYSTEM_ALERT
// 设置flag
int flags = WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM;
// | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
// 如果设置了WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,弹出的View收不到Back键的事件
params.flags = flags;
// 不设置这个弹出框的透明遮罩显示为黑色
params.format = PixelFormat.TRANSLUCENT;
// FLAG_NOT_TOUCH_MODAL不阻塞事件传递到后面的窗口
// 设置 FLAG_NOT_FOCUSABLE 悬浮窗口较小时,后面的应用图标由不可长按变为可长按
// 不设置这个flag的话,home页的划屏会有问题
params.width = LayoutParams.MATCH_PARENT;
params.height = LayoutParams.MATCH_PARENT;
params.gravity = Gravity.CENTER;

// 获取应用的Context
mContext = context.getApplicationContext();
// 获取WindowManager
mWindowManager = (WindowManager) mContext
.getSystemService(Context.WINDOW_SERVICE);
//参数设置:
final WindowManager.LayoutParams params = new WindowManager.LayoutParams();
// 类型
params.type = WindowManager.LayoutParams.TYPE_SYSTEM_ALERT;
// WindowManager.LayoutParams.TYPE_SYSTEM_ALERT
// 设置flag
int flags = WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM;
// | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
// 如果设置了WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,弹出的View收不到Back键的事件
params.flags = flags;
// 不设置这个弹出框的透明遮罩显示为黑色
params.format = PixelFormat.TRANSLUCENT;
// FLAG_NOT_TOUCH_MODAL不阻塞事件传递到后面的窗口
// 设置 FLAG_NOT_FOCUSABLE 悬浮窗口较小时,后面的应用图标由不可长按变为可长按
// 不设置这个flag的话,home页的划屏会有问题
params.width = LayoutParams.MATCH_PARENT;
params.height = LayoutParams.MATCH_PARENT;
params.gravity = Gravity.CENTER;
点击和按键事件
除了View中的各个控件的点击事件之外,弹窗View的消失控制需要一些处理。
点击弹窗外部可隐藏弹窗的效果,首先,悬浮窗是全屏的,只不过最外层的是透明或者半透明的:

具体实现
[java] view plain print?
package com.robert.floatingwindow;
import android.content.Context;
import android.graphics.PixelFormat;
import android.graphics.Rect;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnKeyListener;
import android.view.View.OnTouchListener;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.view.WindowManager.LayoutParams;
import android.widget.Button;
/**
* 弹窗辅助类
*
* @ClassName WindowUtils
*
*
*/
public class WindowUtils {
private static final String LOG_TAG = "WindowUtils";
private static View mView = null;
private static WindowManager mWindowManager = null;
private static Context mContext = null;
public static Boolean isShown = false;
/**
* 显示弹出框
*
* @param context
* @param view
*/
public static void showPopupWindow(final Context context) {
if (isShown) {
LogUtil.i(LOG_TAG, "return cause already shown");
return;
}
isShown = true;
LogUtil.i(LOG_TAG, "showPopupWindow");
// 获取应用的Context
mContext = context.getApplicationContext();
// 获取WindowManager
mWindowManager = (WindowManager) mContext
.getSystemService(Context.WINDOW_SERVICE);
mView = setUpView(context);
final WindowManager.LayoutParams params = new WindowManager.LayoutParams();
// 类型
params.type = WindowManager.LayoutParams.TYPE_SYSTEM_ALERT;
// WindowManager.LayoutParams.TYPE_SYSTEM_ALERT
// 设置flag
int flags = WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM;
// | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
// 如果设置了WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,弹出的View收不到Back键的事件
params.flags = flags;
// 不设置这个弹出框的透明遮罩显示为黑色
params.format = PixelFormat.TRANSLUCENT;
// FLAG_NOT_TOUCH_MODAL不阻塞事件传递到后面的窗口
// 设置 FLAG_NOT_FOCUSABLE 悬浮窗口较小时,后面的应用图标由不可长按变为可长按
// 不设置这个flag的话,home页的划屏会有问题
params.width = LayoutParams.MATCH_PARENT;
params.height = LayoutParams.MATCH_PARENT;
params.gravity = Gravity.CENTER;
mWindowManager.addView(mView, params);
LogUtil.i(LOG_TAG, "add view");
}
/**
* 隐藏弹出框
*/
public static void hidePopupWindow() {
LogUtil.i(LOG_TAG, "hide " + isShown + ", " + mView);
if (isShown && null != mView) {
LogUtil.i(LOG_TAG, "hidePopupWindow");
mWindowManager.removeView(mView);
isShown = false;
}
}
private static View setUpView(final Context context) {
LogUtil.i(LOG_TAG, "setUp view");
View view = LayoutInflater.from(context).inflate(R.layout.popupwindow,
null);
Button positiveBtn = (Button) view.findViewById(R.id.positiveBtn);
positiveBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
LogUtil.i(LOG_TAG, "ok on click");
// 打开安装包
// 隐藏弹窗
WindowUtils.hidePopupWindow();
}
});
Button negativeBtn = (Button) view.findViewById(R.id.negativeBtn);
negativeBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
LogUtil.i(LOG_TAG, "cancel on click");
WindowUtils.hidePopupWindow();
}
});
// 点击窗口外部区域可消除
// 这点的实现主要将悬浮窗设置为全屏大小,外层有个透明背景,中间一部分视为内容区域
// 所以点击内容区域外部视为点击悬浮窗外部
final View popupWindowView = view.findViewById(R.id.popup_window);// 非透明的内容区域
view.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
LogUtil.i(LOG_TAG, "onTouch");
int x = (int) event.getX();
int y = (int) event.getY();
Rect rect = new Rect();
popupWindowView.getGlobalVisibleRect(rect);
if (!rect.contains(x, y)) {
WindowUtils.hidePopupWindow();
}
LogUtil.i(LOG_TAG, "onTouch : " + x + ", " + y + ", rect: "
+ rect);
return false;
}
});
// 点击back键可消除
view.setOnKeyListener(new OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
switch (keyCode) {
case KeyEvent.KEYCODE_BACK:
WindowUtils.hidePopupWindow();
return true;
default:
return false;
}
}
});
return view;
}
}

‘伍’ Android实现类似qq,微信消息悬浮窗通知

实现方法:(需要开启悬浮窗通知权限、允许应用在其他应用上显示)

悬挂式Notification,他是5.0中新增的,也就是API中的Headsup的Notification,可以在不打断用户操作的时候,给用户通知

注意:在某些rom下使用headsup并不会显示桌面悬浮窗,而是直接跳转到相应的界面,亲测华为,小米都是这种情况,这种情况下需要自己实现悬浮窗

具体实现:

‘陆’ 安卓的悬浮球怎么设置的啊

打开屏幕上的设置功能。
在下拉菜单中找到【辅助功能】,点击打开。
1、找到【触控】,点击进去会发现,这个功能是处于关闭的状态,点击一下【关闭】。
2、在辅助触控的后方有一个小开关,点击打开右侧的开关按钮,这个悬浮球就出现在手机屏幕上了。
3、在辅助触控的后方有一个小开关,点击打开右侧的开关按钮,这个悬浮球就出现在手机屏幕上了。
4、在悬触控设置下方,有一个【自定顶层菜单】,点击进入后,选择加号便可以添加各种功能。自定顶层菜单下方【单点】、【轻点两下】、【长按】、【创建新手势】等各种设置,根据自己的需求设置悬浮球上的功能即可。

阅读全文

与android悬浮按钮实现的相关的资料

热点内容
编译器有几个好用的 浏览:500
数据库和网站如何搭载服务器 浏览:154
网络流理论算法与应用 浏览:795
java和matlab 浏览:388
钉钉苹果怎么下app软件 浏览:832
php网站验证码不显示 浏览:859
铝膜构造柱要设置加密区吗 浏览:344
考驾照怎么找服务器 浏览:884
阿里云服务器如何更换地区 浏览:972
手机app调音器怎么调古筝 浏览:503
锐起无盘系统在服务器上需要设置什么吗 浏览:19
红旗出租车app怎么应聘 浏览:978
如何编写linux程序 浏览:870
吉利车解压 浏览:248
java输入流字符串 浏览:341
安卓软件没网怎么回事 浏览:785
dvd压缩碟怎么导出电脑 浏览:275
冒险岛什么服务器好玩 浏览:542
如何在服务器上做性能测试 浏览:794
命令序列错 浏览:260