android實(shí)現(xiàn)桌面移動(dòng)懸浮窗口
現(xiàn)在很多應(yīng)用都有這樣的功能,比如360等安全衛(wèi)士,手機(jī)管家之內(nèi)的應(yīng)用。
效果圖:

一、實(shí)現(xiàn)原理及移動(dòng)思路
調(diào)用WindowManager,并設(shè)置WindowManager.LayoutParams的相關(guān)屬性,通過(guò)WindowManager的addView方法創(chuàng)建View,這樣產(chǎn)生出來(lái)的View根據(jù)WindowManager.LayoutParams屬性不同,效果也就不同了。比如創(chuàng)建系統(tǒng)頂級(jí)窗口,實(shí)現(xiàn)懸浮窗口效果!然后通過(guò)覆寫懸浮View中onTouchEvent方法來(lái)改變windowMananager.LayoutParams中x和y的值來(lái)實(shí)現(xiàn)自由移動(dòng)懸浮窗口。
二、示例代碼
先看看懸浮View的代碼把
package com.example.suspend;
import android.content.Context;
import android.util.Log;
import android.view.MotionEvent;
import android.view.WindowManager;
import android.widget.ImageView;
public class MyFloatView extends ImageView {
private float mTouchStartX;
private float mTouchStartY;
private float x;
private float y;
private WindowManager wm = (WindowManager) getContext()
.getApplicationContext().getSystemService("window");
// 此wmParams變量為獲取的全局變量,用以保存懸浮窗口的屬性
private WindowManager.LayoutParams wmParams = ((MyApplication) getContext()
.getApplicationContext()).getMywmParams();
public MyFloatView(Context context) {
super(context);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
// TODO Auto-generated method stub
// 獲取相對(duì)屏幕的坐標(biāo),即以屏幕左上角為原點(diǎn)
x = event.getRawX();
y = event.getRawY() - 25; // 25是系統(tǒng)狀態(tài)欄的高度
Log.i("currP", "currX" + x + "====currY" + y);
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN: // 捕獲手指觸摸按下動(dòng)作
// 獲取相對(duì)View的坐標(biāo),即以此View左上角為原點(diǎn)
mTouchStartX = event.getX();
mTouchStartY = event.getY();
Log.i("startP", "startX" + mTouchStartX + "====startY"
+ mTouchStartY);
break;
case MotionEvent.ACTION_MOVE://捕獲手指觸摸移動(dòng)動(dòng)作
updateViewPosition();
break;
case MotionEvent.ACTION_UP://捕獲手指觸摸離開動(dòng)作
updateViewPosition();
mTouchStartX=mTouchStartY=0;
break;
default:
break;
}
return true;
}
private void updateViewPosition() {
//更新浮動(dòng)窗口位置參數(shù)
wmParams.x = (int)(x - mTouchStartX);
wmParams.y = (int)(y - mTouchStartY);
wm.updateViewLayout(this, wmParams);//刷新顯示
}
}
上面的wmParams變量(即WindowManager.LayoutParams)的存儲(chǔ)采用了extends Application的方式來(lái)創(chuàng)建全局變量,
示例代碼如下:
package com.example.suspend;
import android.app.Application;
import android.view.WindowManager;
public class MyApplication extends Application {
private WindowManager.LayoutParams wmParams = new WindowManager.LayoutParams();
@Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
}
public WindowManager.LayoutParams getMywmParams() {
return wmParams;
}
}
再來(lái)看一看Activity中的代碼:
package com.example.suspend;
import android.app.Activity;
import android.graphics.PixelFormat;
import android.os.Bundle;
import android.view.Gravity;
import android.view.WindowManager;
import android.view.WindowManager.LayoutParams;
public class MainActivity extends Activity {
private WindowManager wm;
private WindowManager.LayoutParams wmParams;
private MyFloatView myFV;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 創(chuàng)建懸浮窗口
createFloatView();
}
private void createFloatView() {
myFV = new MyFloatView(getApplicationContext());
myFV.setImageResource(R.drawable.ic_launcher);
// 獲取WindowManager
wm = (WindowManager) getApplicationContext().getSystemService("window");
// 設(shè)置LayoutParams(全局變量)相關(guān)參數(shù)
wmParams = ((MyApplication) getApplication()).getMywmParams();
wmParams.type = LayoutParams.TYPE_PHONE;// 設(shè)置window type
wmParams.format = PixelFormat.RGBA_8888;// 設(shè)置圖片格式,效果為背景透明
// 設(shè)置Window flag
wmParams.flags = LayoutParams.FLAG_NOT_TOUCH_MODAL
| LayoutParams.FLAG_NOT_FOCUSABLE;
/*
*
* 下面的flags屬性的效果形同“鎖定”。
*
* 懸浮窗不可觸摸,不接受任何事件,同時(shí)不影響后面的事件響應(yīng)。
*
* wmParams.flags=LayoutParams.FLAG_NOT_TOUCH_MODAL
*
* | LayoutParams.FLAG_NOT_FOCUSABLE
*
* | LayoutParams.FLAG_NOT_TOUCHABLE;
*/
wmParams.gravity = Gravity.LEFT | Gravity.TOP;// 調(diào)整懸浮窗口至左上角,便于調(diào)整坐標(biāo)
// 以屏幕左上角為原點(diǎn),設(shè)置x、y初始值
wmParams.x = 0;
wmParams.y = 0;
// 設(shè)置懸浮窗口長(zhǎng)寬數(shù)據(jù)
wmParams.width = 40;
wmParams.height = 40;
// 顯示myFloatView圖像
wm.addView(myFV, wmParams);
}
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
// 在程序退出(Activity銷毀)時(shí)銷毀懸浮窗口
wm.removeView(myFV);
}
}
最后,別忘了在AndroidManifest.xml中添加權(quán)限:
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" /> <uses-permission android:name="android.permission.SYSTEM_OVERLAY_WINDOW" />
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android菜單的定義及ActionBar的實(shí)現(xiàn)
本篇文章主要介紹了Android菜單的定義及ActionBar的實(shí)現(xiàn),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-08-08
Android 8.0實(shí)現(xiàn)發(fā)送通知
這篇文章主要為大家詳細(xì)介紹了Android 8.0實(shí)現(xiàn)發(fā)送通知,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-07-07
Android實(shí)現(xiàn)二級(jí)列表購(gòu)物車功能
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)二級(jí)列表購(gòu)物車功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-10-10
Android中Handler與Message的簡(jiǎn)單實(shí)例
這篇文章主要介紹了Android中Handler與Message的簡(jiǎn)單實(shí)例的相關(guān)資料,這里提供實(shí)例來(lái)說(shuō)明線程Handler與message 的結(jié)合使用,需要的朋友可以參考下2017-08-08
淺談Android中關(guān)于靜態(tài)變量(static)的使用問(wèn)題
本文主要介紹了Android中關(guān)于靜態(tài)變量(static)的使用問(wèn)題,具有一定的參考作用,下面跟著小編一起來(lái)看下吧2017-01-01
Android使用Intent實(shí)現(xiàn)頁(yè)面跳轉(zhuǎn)
這篇文章主要為大家詳細(xì)介紹了Android使用Intent實(shí)現(xiàn)頁(yè)面跳轉(zhuǎn),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-10-10
Android開發(fā)者需要知道的8個(gè)項(xiàng)目管理技巧
這篇文章主要為大家詳細(xì)介紹了Android開發(fā)者需要知道的8個(gè)項(xiàng)目管理技巧,感興趣的小伙伴們可以參考一下2016-02-02

