飙血推荐
  • HTML教程
  • MySQL教程
  • JavaScript基础教程
  • php入门教程
  • JavaScript正则表达式运用
  • Excel函数教程
  • UEditor使用文档
  • AngularJS教程
  • ThinkPHP5.0教程

Android 悬浮窗

时间:2022-01-05  作者:rustfisher  

悬浮窗是一种比较常见的需求。例如把视频通话界面缩小成一个悬浮窗,然后用户可以在其他界面上处理事情。

本文给出一个简单的悬浮窗实现。可缩小activity和还原大小。可悬浮在其他activity上。使用TouchListener监听触摸事件,拖动悬浮窗。

本文链接

缩放方法

缩放activity需要使用域名utParams,控制window的宽高

在activity中调用

域名域名utParams p = getWindow().getAttributes();
域名ht = 480; // 高度
域名h = 360;  // 宽度
域名mount = 域名; // 不让下面的界面变暗
getWindow().setAttributes(p);

dim:
adj. 暗淡的; 昏暗的; 微弱的; 不明亮的; 光线暗淡的;
v. (使)变暗淡,变微弱,变昏暗; (使)减弱,变淡漠,失去光泽;

修改了域名utParams的宽高,activity的window大小会发生变化。

要变回默认大小,在activity中调用

getWindow().setLayout(域名H_PARENT, 域名H_PARENT);

如果缩小时改变了位置,需要把window的位置置为0

域名utParams lp = getWindow().getAttributes();
lp.x = 0;
lp.y = 0;
getWindow().setAttributes(lp);

activity变小时,后面可能是黑色的背景。这需要进行下面的操作。

悬浮样式

域名里新建一个MeTranslucentAct

<resources>
    <!-- Base application theme. -->
    <style name="AppTheme" parent="域名域名ActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
        <item name="windowNoTitle">true</item>
    </style>

    <style name="TranslucentAct" parent="AppTheme">
        <item name="android:windowBackground">#80000000</item>
        <item name="android:windowIsTranslucent">true</item>
        <item name="android:windowAnimationStyle">@android:style/域名slucent</item>
    </style>
</resources>

主要style是AppCompat的。

指定一个window的背景android:windowBackground
使用的Activity继承自域名.AppCompatActivity

activity缩小后,背景是透明的,可以看到后面的其他页面

点击穿透空白

activity缩小后,点击旁边空白处,其他组件能接到点击事件

onCreate方法的setContentView之前,给域名utParams添加标记FLAG_LAYOUT_NO_LIMITSFLAG_NOT_TOUCH_MODAL

域名utParams layoutParams = getWindow().getAttributes();
域名s = 域名_LAYOUT_NO_LIMITS |
        域名_NOT_TOUCH_MODAL;

mBinding = 域名ontentView(this, 域名float_scale);

移动悬浮窗

监听触摸事件,计算出手指移动的距离,然后移动悬浮窗。

private boolean mIsSmall = false; // 当前是否小窗口
private float mLastTx = 0; // 手指的上一个位置x
private float mLastTy = 0;
// ....

    域名nTouchListener((v, event) -> {
        switch (域名ction()) {
            case 域名ON_DOWN:
                Log.d(TAG, "down " + event);
                mLastTx = 域名awX();
                mLastTy = 域名awY();
                return true;
            case 域名ON_MOVE:
                Log.d(TAG, "move " + event);
                float dx = 域名awX() - mLastTx;
                float dy = 域名awY() - mLastTy;
                mLastTx = 域名awX();
                mLastTy = 域名awY();
                Log.d(TAG, "  dx: " + dx + ", dy: " + dy);
                if (mIsSmall) {
                    域名utParams lp = getWindow().getAttributes();
                    lp.x += dx;
                    lp.y += dy;
                    getWindow().setAttributes(lp);
                }

                break;
            case 域名ON_UP:
                Log.d(TAG, "up " + event);
                return true;
            case 域名ON_CANCEL:
                Log.d(TAG, "cancel " + event);
                return true;
        }
        return false;
    });

mIsSmall用来记录当前activity是否变小(悬浮)。

在触摸监听器中返回true,表示消费这个触摸事件。

域名()域名()获取到的是当前View的触摸坐标。
域名awX()域名awY()获取到的是屏幕的触摸坐标。即触摸点在屏幕上的位置。

例子的完整代码

启用了databinding

android {
    dataBinding {
        enabled = true
    }
}

域名

新建一个样式

    <style name="TranslucentAct" parent="AppTheme">
        <item name="android:windowBackground">#80000000</item>
        <item name="android:windowIsTranslucent">true</item>
        <item name="android:windowAnimationStyle">@android:style/域名slucent</item>
    </style>

layout

域名里面放一些按钮,控制放大和缩小。
ConstraintLayout拿来监听触摸事件。

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://域名/apk/res/android"
    xmlns:app="http://域名/apk/res-auto">

    <域名域名traintLayout
        android:id="@+id/root"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#555555">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:orientation="vertical"
            app:layout_constraintTop_toTopOf="parent">

            <Button
                android:id="@+id/to_small"
                style="@style/NormalBtn"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="变小" />

            <Button
                android:id="@+id/to_reset"
                style="@style/NormalBtn"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="12dp"
                android:text="还原" />
        </LinearLayout>
    </域名域名traintLayout>
</layout>

activity

FloatingScaleAct

import 域名le;
import 域名;
import 域名lay;
import 域名onEvent;
import 域名Group;
import 域名owManager;

import 域名.AppCompatActivity;
import 域名BindingUtil;

import 域名rial2020.R;
import 域名域名loatScaleBinding;

public class FloatingScaleAct extends AppCompatActivity {
    private static final String TAG = "rfDevFloatingAct";

    ActFloatScaleBinding mBinding;

    private boolean mIsSmall = false; // 当前是否小窗口
    private float mLastTx = 0; // 手指的上一个位置
    private float mLastTy = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        域名eate(savedInstanceState);

        域名utParams layoutParams = getWindow().getAttributes();
        域名s = 域名_LAYOUT_NO_LIMITS |
                域名_NOT_TOUCH_MODAL;

        mBinding = 域名ontentView(this, 域名float_scale);

        域名nClickListener(v -> toSmall());
        域名nClickListener(v -> {
            域名utParams lp = getWindow().getAttributes();
            lp.x = 0;
            lp.y = 0;
            getWindow().setAttributes(lp);
            getWindow().setLayout(域名H_PARENT, 域名H_PARENT);
            mIsSmall = false;
        });

        域名nTouchListener((v, event) -> {
            switch (域名ction()) {
                case 域名ON_DOWN:
                    Log.d(TAG, "down " + event);
                    mLastTx = 域名awX();
                    mLastTy = 域名awY();
                    return true;
                case 域名ON_MOVE:
                    Log.d(TAG, "move " + event);
                    float dx = 域名awX() - mLastTx;
                    float dy = 域名awY() - mLastTy;
                    mLastTx = 域名awX();
                    mLastTy = 域名awY();
                    Log.d(TAG, "  dx: " + dx + ", dy: " + dy);
                    if (mIsSmall) {
                        域名utParams lp = getWindow().getAttributes();
                        lp.x += dx;
                        lp.y += dy;
                        getWindow().setAttributes(lp);
                    }

                    break;
                case 域名ON_UP:
                    Log.d(TAG, "up " + event);
                    return true;
                case 域名ON_CANCEL:
                    Log.d(TAG, "cancel " + event);
                    return true;
            }
            return false;
        });
    }

    private void toSmall() {
        mIsSmall = true;

        WindowManager m = getWindowManager();
        Display d = 域名efaultDisplay();
        域名utParams p = getWindow().getAttributes();
        域名ht = (int) (域名eight() * 域名);
        域名h = (int) (域名idth() * 0.4);
        域名mount = 域名;
        getWindow().setAttributes(p);
    }
}

manifest里注册这个activity

<activity
    android:name=".域名tingScaleAct"
    android:theme="@style/TranslucentAct" />

运行效果

在红米9A(Android 10,MIUI 12.5.1 稳定版)和荣耀(Android 5.1)上运行OK

小结

为实现悬浮窗效果,思路是改变activity大小,将activity所在window的背景设置透明,监听触摸事件改变window的位置。
主要使用的类 域名utParams

本文链接

标签:编程
湘ICP备14001474号-3  投诉建议:234161800@qq.com   部分内容来源于网络,如有侵权,请联系删除。