圣誕節(jié),寫個程序練練手————Android 全界面懸浮按鈕實現(xiàn)
開始我以為懸浮窗口,可以用Android中得PopupWindow 來實現(xiàn),雖然也實現(xiàn)了,但局限性非常大。比如PopupWindow必須要有載體View,也就是說,必須要指定在那個View的上面來實現(xiàn)。以該View為相對位置,來顯示PopupWindow。這就局限了其智能在用戶交互的窗口上,相對的顯示。而無法自由的拖動位置和在桌面顯示。
于是查閱了一些資料,有兩種實現(xiàn)方法。一種是自定義Toast,Toast是運行于所有界面之上的,也就是說沒有界面可以覆蓋它。另一種是Android中得CompatModeWrapper類,ConmpatModeWrapper是基類,實現(xiàn)大部分功能的是它的內(nèi)部類WindowManagerImpl。該對象可以通過getApplication().getSystemService(Context.WINDOW_SERVICE)得到。(注:如果是通過activity.getSystemService(Context.WINDOW_SERVICE)得到的只是屬于Activity的LocalWindowManager)。
簡單的介紹之后,我們直接來看代碼實現(xiàn),注釋已經(jīng)寫在代碼中。
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
<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>
實現(xiàn)懸浮窗功能的service類
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;
//創(chuàng)建浮動窗口設(shè)置布局參數(shù)的對象
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);
//設(shè)置window type
wmParams.type = LayoutParams.TYPE_PHONE;
//設(shè)置圖片格式,效果為背景透明
wmParams.format = PixelFormat.RGBA_8888;
//設(shè)置浮動窗口不可聚焦(實現(xiàn)操作除浮動窗口外的其他可見窗口的操作)
wmParams.flags = LayoutParams.FLAG_NOT_FOCUSABLE;
//調(diào)整懸浮窗顯示的??课恢脼樽髠?cè)置頂
wmParams.gravity = Gravity.LEFT | Gravity.TOP;
// 以屏幕左上角為原點,設(shè)置x、y初始值,相對于gravity
wmParams.x = 0;
wmParams.y = 152;
//設(shè)置懸浮窗口長寬數(shù)據(jù)
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));
//設(shè)置監(jiān)聽浮動窗口的觸摸移動
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為狀態(tài)欄的高度
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則釋放事件,可以出發(fā)點擊否。
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文件
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>
以上內(nèi)容是實現(xiàn)Android 全界面懸浮按鈕的全部敘述,希望大家喜歡。
- Android自定義可拖拽的懸浮按鈕DragFloatingActionButton
- Android中FloatingActionButton實現(xiàn)懸浮按鈕實例
- Android實現(xiàn)系統(tǒng)級懸浮按鈕
- Android懸浮按鈕點擊返回頂部FloatingActionButton
- Android開發(fā)模仿qq視頻通話懸浮按鈕(實例代碼)
- Android利用WindowManager生成懸浮按鈕及懸浮菜單
- Android開發(fā)中在TableView上添加懸浮按鈕的方法
- Android開發(fā)懸浮按鈕 Floating ActionButton的實現(xiàn)方法
- Android利用懸浮按鈕實現(xiàn)翻頁效果
- Android懸浮按鈕的使用方法
相關(guān)文章
android開發(fā)通過Scroller實現(xiàn)過渡滑動效果操作示例
這篇文章主要介紹了android開發(fā)通過Scroller實現(xiàn)過渡滑動效果,結(jié)合實例形式分析了Android Scroller類實現(xiàn)過渡滑動效果的基本原理與實現(xiàn)技巧,需要的朋友可以參考下2020-01-01
Android RecyclerView網(wǎng)格布局示例解析
這篇文章主要介紹了Android RecyclerView網(wǎng)格布局示例解析,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2020-12-12
SafeList?in?Flutter?and?Dart小技巧
這篇文章主要為大家介紹了SafeList?in?Flutter?and?Dart小技巧,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-12-12
Android實現(xiàn) EditText輸入手機號空格功能
這篇文章主要介紹了Android實現(xiàn) EditText輸入手機號空格功能,實現(xiàn)思路是要重寫TextWatcher,每次EditText內(nèi)容變化,都判斷內(nèi)容是否符合要求,具體實例代碼大家參考下本文2018-02-02
Android實現(xiàn)自動變換大小的ViewPager
ViewPager使用適配器類將數(shù)據(jù)和view的處理分離,ViewPager的適配器叫PagerAdapter,這是一個抽象類,不能實例化,所以它有兩個子類:FragmentPagerAdapter 和 FragmentStatePagerAdapter,這兩個都是處理頁面為Fragment的情況2022-11-11

