導航:首頁 > 操作系統 > android懸浮窗口動畫

android懸浮窗口動畫

發布時間:2022-10-05 23:16:08

『壹』 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如何只在應用內顯示懸浮窗

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上顯示窗口。


『叄』 Android桌面懸浮窗效果怎麼實現

可以模仿360手機衛士懸浮窗的那份代碼的基礎上繼續開發。
打開手機衛士主界面,然後上拉,然後點擊快捷設置,然後點擊桌面懸浮窗,就可以將360手機衛士安卓版桌面浮窗調出來了,具體步驟如下:
1、安裝最新的360手機衛士。
2、點開隱私保護,打開右上角的三個點。
3、點開衛士設置,點開懸浮窗。
4、開啟內存清理懸浮窗, 選擇顯示樣式,安仔樣式或是加速球。
5、可以選擇僅在桌面顯示,若開啟則懸浮窗只出現在桌面,若關閉則懸浮窗會跟隨打開頁面一直出現。
6、可以同時開啟拖動清理內存,這樣直接拖動懸浮窗圖標,就可以輕松清理內存了。

『肆』 Android桌面懸浮窗效果怎麼實現

可以根據項目需要改變其相應布局。
package com.zk.me;
import java.util.List;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.content.pm.ResolveInfo;
import android.os.Bundle;
import android.view.Gravity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.Toast;
public class MainActivity extends Activity implements OnClickListener {
private WindowManager windowManager = null;
private WindowManager.LayoutParams windowManagerParams = null;
private MyFloatView floatView = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);// 取消標題欄
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);// 全屏
setContentView(R.layout.activity_main);
createView();
}
public void onDestroy() {
super.onDestroy();
// 在程序退出(Activity銷毀)時銷毀懸浮窗口
}
private void createView() {
floatView = new MyFloatView(getApplicationContext());
floatView.setOnClickListener(this);
ImageView view = new ImageView(this);
view.setImageResource(R.drawable.ic_launcher);
floatView.addView(view); // 這里簡單的用自帶的icon來做演示
windowManager = (WindowManager) getApplicationContext()
.getSystemService("window");
// 設置LayoutParams(全局變數)相關參數
windowManagerParams = ((FloatApplication) getApplication())
.getWindowParams();
windowManagerParams.type = 2003; // 設置window type
// windowManagerParams.format = PixelFormat.RGBA_8888; // 設置圖片格式,效果為背景透明
// 設置Window flag
windowManagerParams.flags = 40;
windowManagerParams.format = 1;
// 調整懸浮窗口至左上角,便於調整坐標
windowManagerParams.gravity = Gravity.LEFT | Gravity.TOP;
// 以屏幕左上角為原點,設置x、y初始值
windowManagerParams.x = 0;
windowManagerParams.y = 0;
// 設置懸浮窗口長寬數據
windowManagerParams.width = 40;
windowManagerParams.height = 40;
// 顯示myFloatView圖像
windowManager.addView(floatView, windowManagerParams);
}
public void onClick(View v) {
Toast.makeText(this, "Clicked", Toast.LENGTH_SHORT).show();
openCLD("com.jovian.android.pqgl", getApplicationContext());
}
public static void openCLD(String packageName, Context context) {// 打開移動警務應用
PackageManager packageManager = context.getPackageManager();
PackageInfo pi = null;
try {
pi = packageManager.getPackageInfo(packageName, 0);
} catch (NameNotFoundException e) {
}
Intent resolveIntent = new Intent(Intent.ACTION_MAIN, null);
resolveIntent.addCategory(Intent.CATEGORY_LAUNCHER);
resolveIntent.setPackage(packageName);
List<ResolveInfo> apps = packageManager.queryIntentActivities(
resolveIntent, 0);
ResolveInfo resolveinfo = apps.iterator().next();
if (resolveinfo != null) {
String className = resolveinfo.activityInfo.name;
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
ComponentName cn = new ComponentName(packageName, className);
intent.setComponent(cn);
context.startActivity(intent);
}
}
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onRestoreInstanceState(savedInstanceState);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
package com.zk.me;
import android.content.Context;
import android.graphics.Rect;
import android.util.Log;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.widget.LinearLayout;
public class MyFloatView extends LinearLayout {
private float mTouchX;
private float mTouchY;
private float x;
private float y;
private float mStartX;
private float mStartY;
private OnClickListener mClickListener;
private WindowManager windowManager = (WindowManager) getContext()
.getApplicationContext().getSystemService("window");
// 此windowManagerParams變數為獲取的全局變數,用以保存懸浮窗口的屬性
private WindowManager.LayoutParams windowManagerParams = ((FloatApplication) getContext()
.getApplicationContext()).getWindowParams();
public MyFloatView(Context context) {
super(context);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
// 獲取到狀態欄的高度
Rect frame = new Rect();
getWindowVisibleDisplayFrame(frame);
int statusBarHeight = frame.top;
System.out.println("statusBarHeight:" + statusBarHeight);
// 獲取相對屏幕的坐標,即以屏幕左上角為原點
x = event.getRawX();
y = event.getRawY() - statusBarHeight; // statusBarHeight是系統狀態欄的高度
Log.i("tag", "currX" + x + "====currY" + y);
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN: // 捕獲手指觸摸按下動作
// 獲取相對View的坐標,即以此View左上角為原點
mTouchX = event.getX();
mTouchY = event.getY();
mStartX = x;
mStartY = y;
Log.i("tag", "startX" + mTouchX + "====startY" + mTouchY);
break;
case MotionEvent.ACTION_MOVE: // 捕獲手指觸摸移動動作
updateViewPosition();
break;
case MotionEvent.ACTION_UP: // 捕獲手指觸摸離開動作
updateViewPosition();
mTouchX = mTouchY = 0;
if ((x - mStartX) < 5 && (y - mStartY) < 5) {
if (mClickListener != null) {
mClickListener.onClick(this);
}
}
break;
}
return true;
}
@Override
public void setOnClickListener(OnClickListener l) {
this.mClickListener = l;
}
private void updateViewPosition() {
// 更新浮動窗口位置參數
windowManagerParams.x = (int) (x - mTouchX);
windowManagerParams.y = (int) (y - mTouchY);
windowManager.updateViewLayout(this, windowManagerParams); // 刷新顯示
}
}

『伍』 android裡面的WindowManager如何給view添加自定義動畫

懸浮窗口就是popuwindow. activity也可以通過theme設置成懸浮形式,就像dialog

『陸』 android 怎麼讓浮動窗口顯示

android 手機讓浮動窗口顯示的設置步驟:

  1. 點擊設置圖標

  2. 點擊「設置」列表中「管理應用程序」

  3. 找到要設置浮動窗口的軟體

  4. 進入「應用程序信息」

  5. 點擊「應用程序信息」最下面的「許可權」

  6. 在「許可權」頁面中勾選「顯示懸浮窗」。這樣就開啟了浮動窗

  7. android手機版本繁多,各個廠家的rom不一樣,設置也不一樣。

『柒』 懸浮窗能實現自定Animation動畫效果嗎

閱讀全文

與android懸浮窗口動畫相關的資料

熱點內容
java自動機 瀏覽:363
相機連拍解壓 瀏覽:31
linuxssh服務重啟命令 瀏覽:330
茂名氫氣隔膜壓縮機 瀏覽:47
程序員地鐵寫程序 瀏覽:330
java的switchenum 瀏覽:329
pdf瓷器 瀏覽:905
怎樣用adb命令刷機 瀏覽:962
蘋果手機怎麼買app 瀏覽:303
如何找到伺服器連接地址 瀏覽:776
重慶百望伺服器地址 瀏覽:227
python中range後的結果 瀏覽:101
編譯器管理的存儲有哪些 瀏覽:956
顯控觸摸屏與單片機通信 瀏覽:426
宅之便利店app怎麼使用輕應用 瀏覽:320
去外國怎麼下載外國app 瀏覽:269
linux開機啟動配置 瀏覽:367
androidstudio類注釋 瀏覽:137
如何在pdf中插入圖片 瀏覽:907
京山pdf 瀏覽:28