Android應(yīng)用內(nèi)懸浮窗Activity的簡單實現(xiàn)
前言
懸浮窗是一種比較常見的需求。例如把視頻通話界面縮小成一個懸浮窗,然后用戶可以在其他界面上處理事情。
本文給出一個簡單的應(yīng)用內(nèi)懸浮窗實現(xiàn)??煽s小activity和還原大小。可懸浮在同一個app的其他activity上。使用TouchListener監(jiān)聽觸摸事件,拖動懸浮窗。
縮放方法
縮放activity需要使用WindowManager.LayoutParams,控制window的寬高
在activity中調(diào)用
android.view.WindowManager.LayoutParams p = getWindow().getAttributes(); p.height = 480; // 高度 p.width = 360; // 寬度 p.dimAmount = 0.0f; // 不讓下面的界面變暗 getWindow().setAttributes(p);
dim: adj. 暗淡的; 昏暗的; 微弱的; 不明亮的; 光線暗淡的; v. (使)變暗淡,變微弱,變昏暗; (使)減弱,變淡漠,失去光澤;
修改了WindowManager.LayoutParams的寬高,activity的window大小會發(fā)生變化。
要變回默認大小,在activity中調(diào)用
getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
如果縮小時改變了位置,需要把window的位置置為0
WindowManager.LayoutParams lp = getWindow().getAttributes(); lp.x = 0; lp.y = 0; getWindow().setAttributes(lp);
activity變小時,后面可能是黑色的背景。這需要進行下面的操作。
懸浮樣式
在styles.xml里新建一個MeTranslucentAct。
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- 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/Animation.Translucent</item>
</style>
</resources>
主要style是AppCompat的。
指定一個window的背景android:windowBackground 使用的Activity繼承自androidx.appcompat.app.AppCompatActivity
activity縮小后,背景是透明的,可以看到后面的其他頁面
點擊穿透空白
activity縮小后,點擊旁邊空白處,其他組件能接到點擊事件
在onCreate方法的setContentView之前,給WindowManager.LayoutParams添加標(biāo)記FLAG_LAYOUT_NO_LIMITS和FLAG_NOT_TOUCH_MODAL
WindowManager.LayoutParams layoutParams = getWindow().getAttributes();
layoutParams.flags = WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS |
WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL;
mBinding = DataBindingUtil.setContentView(this, R.layout.act_float_scale);
移動懸浮窗
監(jiān)聽觸摸事件,計算出手指移動的距離,然后移動懸浮窗。
private boolean mIsSmall = false; // 當(dāng)前是否小窗口
private float mLastTx = 0; // 手指的上一個位置x
private float mLastTy = 0;
// ....
mBinding.root.setOnTouchListener((v, event) -> {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
Log.d(TAG, "down " + event);
mLastTx = event.getRawX();
mLastTy = event.getRawY();
return true;
case MotionEvent.ACTION_MOVE:
Log.d(TAG, "move " + event);
float dx = event.getRawX() - mLastTx;
float dy = event.getRawY() - mLastTy;
mLastTx = event.getRawX();
mLastTy = event.getRawY();
Log.d(TAG, " dx: " + dx + ", dy: " + dy);
if (mIsSmall) {
WindowManager.LayoutParams lp = getWindow().getAttributes();
lp.x += dx;
lp.y += dy;
getWindow().setAttributes(lp);
}
break;
case MotionEvent.ACTION_UP:
Log.d(TAG, "up " + event);
return true;
case MotionEvent.ACTION_CANCEL:
Log.d(TAG, "cancel " + event);
return true;
}
return false;
});
mIsSmall用來記錄當(dāng)前activity是否變小(懸?。?/p>
在觸摸監(jiān)聽器中返回true,表示消費這個觸摸事件。
event.getX()和event.getY()獲取到的是當(dāng)前View的觸摸坐標(biāo)。 event.getRawX()和event.getRawY()獲取到的是屏幕的觸摸坐標(biāo)。即觸摸點在屏幕上的位置。
例子的完整代碼
啟用了databinding
android {
dataBinding {
enabled = true
}
}
styles.xml
新建一個樣式
<style name="TranslucentAct" parent="AppTheme">
<item name="android:windowBackground">#80000000</item>
<item name="android:windowIsTranslucent">true</item>
<item name="android:windowAnimationStyle">@android:style/Animation.Translucent</item>
</style>
layout
act_float_scale.xml里面放一些按鈕,控制放大和縮小。 ConstraintLayout拿來監(jiān)聽觸摸事件。
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<androidx.constraintlayout.widget.ConstraintLayout
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>
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>
activity
FloatingScaleAct
import android.os.Bundle;
import android.util.Log;
import android.view.Display;
import android.view.MotionEvent;
import android.view.ViewGroup;
import android.view.WindowManager;
import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;
import com.rustfisher.tutorial2020.R;
import com.rustfisher.tutorial2020.databinding.ActFloatScaleBinding;
public class FloatingScaleAct extends AppCompatActivity {
private static final String TAG = "rfDevFloatingAct";
ActFloatScaleBinding mBinding;
private boolean mIsSmall = false; // 當(dāng)前是否小窗口
private float mLastTx = 0; // 手指的上一個位置
private float mLastTy = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
WindowManager.LayoutParams layoutParams = getWindow().getAttributes();
layoutParams.flags = WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS |
WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL;
mBinding = DataBindingUtil.setContentView(this, R.layout.act_float_scale);
mBinding.toSmall.setOnClickListener(v -> toSmall());
mBinding.toReset.setOnClickListener(v -> {
WindowManager.LayoutParams lp = getWindow().getAttributes();
lp.x = 0;
lp.y = 0;
getWindow().setAttributes(lp);
getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
mIsSmall = false;
});
mBinding.root.setOnTouchListener((v, event) -> {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
Log.d(TAG, "down " + event);
mLastTx = event.getRawX();
mLastTy = event.getRawY();
return true;
case MotionEvent.ACTION_MOVE:
Log.d(TAG, "move " + event);
float dx = event.getRawX() - mLastTx;
float dy = event.getRawY() - mLastTy;
mLastTx = event.getRawX();
mLastTy = event.getRawY();
Log.d(TAG, " dx: " + dx + ", dy: " + dy);
if (mIsSmall) {
WindowManager.LayoutParams lp = getWindow().getAttributes();
lp.x += dx;
lp.y += dy;
getWindow().setAttributes(lp);
}
break;
case MotionEvent.ACTION_UP:
Log.d(TAG, "up " + event);
return true;
case MotionEvent.ACTION_CANCEL:
Log.d(TAG, "cancel " + event);
return true;
}
return false;
});
}
private void toSmall() {
mIsSmall = true;
WindowManager m = getWindowManager();
Display d = m.getDefaultDisplay();
WindowManager.LayoutParams p = getWindow().getAttributes();
p.height = (int) (d.getHeight() * 0.35);
p.width = (int) (d.getWidth() * 0.4);
p.dimAmount = 0.0f;
getWindow().setAttributes(p);
}
}
manifest里注冊這個activity
<activity
android:name=".act.FloatingScaleAct"
android:theme="@style/TranslucentAct" />
運行效果
在紅米9A(Android 10,MIUI 12.5.1 穩(wěn)定版)和榮耀(Android 5.1)上運行OK

小結(jié)
為實現(xiàn)懸浮窗效果,思路是改變activity大小,將activity所在window的背景設(shè)置透明,監(jiān)聽觸摸事件改變window的位置。 主要使用的類 WindowManager.LayoutParams
到此這篇關(guān)于Android應(yīng)用內(nèi)懸浮窗Activity實現(xiàn)的文章就介紹到這了,更多相關(guān)Android懸浮窗Activity內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Android webview加載https鏈接錯誤或無響應(yīng)的解決
這篇文章主要介紹了Android webview加載https鏈接錯誤或無響應(yīng)的解決,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-03-03
AndroidHttpClient詳解及調(diào)用示例
本文給大家介紹AndroidHttpClient結(jié)構(gòu)、使用方式及調(diào)用示例詳解,需要的朋友可以參考下2015-10-10
android studio3.3.1代碼提示忽略大小寫的設(shè)置
這篇文章主要介紹了android studio3.3.1代碼提示忽略大小寫的設(shè)置,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-03-03
Android網(wǎng)絡(luò)開發(fā)中GET與POST請求詳解
這篇文章主要介紹了android實現(xiàn)網(wǎng)絡(luò)請求的get和post請求的簡單封裝與使用,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧<BR>2022-12-12
RxJava+Retrofit+OkHttp實現(xiàn)文件上傳
本篇文章主要介紹了RxJava+Retrofit+OkHttp實現(xiàn)文件上傳,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-11-11
Compose狀態(tài)保存rememberSaveable原理解析
這篇文章主要為大家介紹了Compose狀態(tài)保存rememberSaveable原理解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-11-11
Win8下Android SDK安裝與環(huán)境變量配置教程
這篇文章主要為大家詳細介紹了Win8下Android SDK安裝與環(huán)境變量配置教程,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-07-07

