服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - Android - 圣诞节,写个程序练练手————Android 全界面悬浮按钮实现

圣诞节,写个程序练练手————Android 全界面悬浮按钮实现

2021-04-25 15:10Android教程网 Android

这篇文章主要介绍了圣诞节,写个程序练练手————Android 全界面悬浮按钮实现的相关资料,需要的朋友可以参考下

开始我以为悬浮窗口,可以用Android中得PopupWindow 来实现,虽然也实现了,但局限性非常大。比如PopupWindow必须要有载体View,也就是说,必须要指定在那个View的上面来实现。以该View为相对位置,来显示PopupWindow。这就局限了其智能在用户交互的窗口上,相对的显示。而无法自由的拖动位置和在桌面显示。

于是查阅了一些资料,有两种实现方法。一种是自定义Toast,Toast是运行于所有界面之上的,也就是说没有界面可以覆盖它。另一种是Android中得CompatModeWrapper类,ConmpatModeWrapper是基类,实现大部分功能的是它的内部类WindowManagerImpl。该对象可以通过getApplication().getSystemService(Context.WINDOW_SERVICE)得到。(注:如果是通过activity.getSystemService(Context.WINDOW_SERVICE)得到的只是属于Activity的LocalWindowManager)。

简单的介绍之后,我们直接来看代码实现,注释已经写在代码中。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
MainActivity.java
package com.example.floatviewdemo;
import com.example.floatviewdemo.service.FloatViewService;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
public class MainActivity extends Activity {
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
  }
  @Override
 protected void onStart() {
  Intent intent = new Intent(MainActivity.this, FloatViewService.class);
     //启动FloatViewService
     startService(intent);
 super.onStart();
 }
 
 @Override
 protected void onStop() {
 // 销毁悬浮窗
 Intent intent = new Intent(MainActivity.this, FloatViewService.class);
    //终止FloatViewService
    stopService(intent);
    super.onStop();
 }
}

activity_main.xml

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:background="#fff"
  android:paddingBottom="@dimen/activity_vertical_margin"
  android:paddingLeft="@dimen/activity_horizontal_margin"
  android:paddingRight="@dimen/activity_horizontal_margin"
  android:paddingTop="@dimen/activity_vertical_margin"
  tools:context="com.example.floatviewdemo.MainActivity" >
  <TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/hello_world" />
</RelativeLayout>

实现悬浮窗功能的service类

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package com.example.floatviewdemo.service;
import com.example.floatviewdemo.R;
import android.annotation.SuppressLint;
import android.app.Service;
import android.content.Intent;
import android.graphics.PixelFormat;
import android.os.IBinder;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.WindowManager;
import android.view.WindowManager.LayoutParams;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.Toast;
public class FloatViewService extends Service 
{
 private static final String TAG = "FloatViewService";
  //定义浮动窗口布局
  private LinearLayout mFloatLayout;
  private WindowManager.LayoutParams wmParams;
  //创建浮动窗口设置布局参数的对象
  private WindowManager mWindowManager;
  private ImageButton mFloatView;
  @Override
  public void onCreate() 
  {
    super.onCreate();
    Log.i(TAG, "onCreate");
    createFloatView();   
  }
  @SuppressWarnings("static-access")
 @SuppressLint("InflateParams") private void createFloatView()
  {
    wmParams = new WindowManager.LayoutParams();
    //通过getApplication获取的是WindowManagerImpl.CompatModeWrapper
    mWindowManager = (WindowManager)getApplication().getSystemService(getApplication().WINDOW_SERVICE);
    //设置window type
    wmParams.type = LayoutParams.TYPE_PHONE; 
    //设置图片格式,效果为背景透明
    wmParams.format = PixelFormat.RGBA_8888; 
    //设置浮动窗口不可聚焦(实现操作除浮动窗口外的其他可见窗口的操作)
    wmParams.flags = LayoutParams.FLAG_NOT_FOCUSABLE;   
    //调整悬浮窗显示的停靠位置为左侧置顶
    wmParams.gravity = Gravity.LEFT | Gravity.TOP;    
    // 以屏幕左上角为原点,设置x、y初始值,相对于gravity
    wmParams.x = 0;
    wmParams.y = 152;
    //设置悬浮窗口长宽数据 
    wmParams.width = WindowManager.LayoutParams.WRAP_CONTENT;
    wmParams.height = WindowManager.LayoutParams.WRAP_CONTENT;
    LayoutInflater inflater = LayoutInflater.from(getApplication());
    //获取浮动窗口视图所在布局
    mFloatLayout = (LinearLayout) inflater.inflate(R.layout.alert_window_menu, null);
    //添加mFloatLayout
    mWindowManager.addView(mFloatLayout, wmParams);
    //浮动窗口按钮
    mFloatView = (ImageButton) mFloatLayout.findViewById(R.id.alert_window_imagebtn);
    mFloatLayout.measure(View.MeasureSpec.makeMeasureSpec(0,
        View.MeasureSpec.UNSPECIFIED), View.MeasureSpec
        .makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
    //设置监听浮动窗口的触摸移动
    mFloatView.setOnTouchListener(new OnTouchListener() 
    {
     boolean isClick;
  @SuppressLint("ClickableViewAccessibility") @Override
  public boolean onTouch(View v, MotionEvent event) {
  switch (event.getAction()) {
  case MotionEvent.ACTION_DOWN:
   mFloatView.setBackgroundResource(R.drawable.circle_red);
   isClick = false;
   break;
  case MotionEvent.ACTION_MOVE:
   isClick = true;
   // getRawX是触摸位置相对于屏幕的坐标,getX是相对于按钮的坐标
   wmParams.x = (int) event.getRawX()
    - mFloatView.getMeasuredWidth() / 2;
   // 减25为状态栏的高度
   wmParams.y = (int) event.getRawY()
    - mFloatView.getMeasuredHeight() / 2 - 75;
   // 刷新
   mWindowManager.updateViewLayout(mFloatLayout, wmParams);
   return true;
  case MotionEvent.ACTION_UP:
   mFloatView.setBackgroundResource(R.drawable.circle_cyan);
   return isClick;// 此处返回false则属于移动事件,返回true则释放事件,可以出发点击否。
  default:
   break;
  }
  return false;
  }
    }); 
    mFloatView.setOnClickListener(new OnClickListener() 
    {
      @Override
      public void onClick(View v) 
      {
        Toast.makeText(FloatViewService.this, "一百块都不给我!", Toast.LENGTH_SHORT).show();
      }
    });
  }
  @Override
  public void onDestroy() 
  {
    super.onDestroy();
    if(mFloatLayout != null)
    {
      //移除悬浮窗口
      mWindowManager.removeView(mFloatLayout);
    }
  }
 @Override
 public IBinder onBind(Intent intent) {
 return null;
 }
}

悬浮窗的xml文件

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
alert_window_menu.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:orientation="vertical" >
  <ImageButton
    android:id="@+id/alert_window_imagebtn"
    android:layout_width="50dp"
    android:layout_height="50dp"
    android:background="@drawable/float_window_menu"
    android:contentDescription="@null"
    />
</LinearLayout>

以上内容是实现Android 全界面悬浮按钮的全部叙述,希望大家喜欢。

延伸 · 阅读

精彩推荐
  • AndroidAndroid实现Service获取当前位置(GPS+基站)的方法

    Android实现Service获取当前位置(GPS+基站)的方法

    这篇文章主要介绍了Android实现Service获取当前位置(GPS+基站)的方法,较为详细的分析了Service基于GPS位置的技巧,具有一定参考借鉴价值,需要的朋友可以参考下...

    Ruthless8342021-03-31
  • AndroidAndroid程序设计之AIDL实例详解

    Android程序设计之AIDL实例详解

    这篇文章主要介绍了Android程序设计的AIDL,以一个完整实例的形式较为详细的讲述了AIDL的原理及实现方法,需要的朋友可以参考下...

    Android开发网4642021-03-09
  • AndroidAndroid界面效果UI开发资料汇总(附资料包)

    Android界面效果UI开发资料汇总(附资料包)

    android ui界面设计,友好的界面会提高用户体验度;同时也增强了android ui界面设计的难度,本文提供了一些常用开发资料(有下载哦)感兴趣的朋友可以了解下...

    Android开发网4672021-01-03
  • AndroidAndroid实现固定屏幕显示的方法

    Android实现固定屏幕显示的方法

    这篇文章主要介绍了Android实现固定屏幕显示的方法,实例分析了Android屏幕固定显示所涉及的相关技巧,具有一定参考借鉴价值,需要的朋友可以参考下...

    鉴客6192021-03-27
  • AndroidAndroid编程解析XML方法详解(SAX,DOM与PULL)

    Android编程解析XML方法详解(SAX,DOM与PULL)

    这篇文章主要介绍了Android编程解析XML方法,结合实例形式详细分析了Android解析XML文件的常用方法与相关实现技巧,需要的朋友可以参考下...

    liuhe68810052021-05-03
  • AndroidAndroid中AsyncTask详细介绍

    Android中AsyncTask详细介绍

    这篇文章主要介绍了Android中AsyncTask详细介绍,AsyncTask是一个很常用的API,尤其异步处理数据并将数据应用到视图的操作场合,需要的朋友可以参考下...

    Android开发网7452021-03-11
  • AndroidAndroid CardView+ViewPager实现ViewPager翻页动画的方法

    Android CardView+ViewPager实现ViewPager翻页动画的方法

    本篇文章主要介绍了Android CardView+ViewPager实现ViewPager翻页动画的方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧...

    Abby代黎明9602022-03-02
  • Android汇总Android视频录制中常见问题

    汇总Android视频录制中常见问题

    这篇文章主要汇总了Android视频录制中常见问题,帮助大家更好地解决Android视频录制中常见的问题,需要的朋友可以参考下...

    yh_thu5192021-04-28