Android實(shí)現(xiàn)類似360,QQ管家那樣的懸浮窗
我手機(jī)從來(lái)不裝這些東西,不過(guò),有次看到同事的android手機(jī)上,有個(gè)QQ管家在桌面上浮著,同事拖動(dòng)管家時(shí),管家就變成一只鳥,桌面下方還有個(gè)彈弓,桌面頂部有只烏鴉,把管家也就是鳥拖動(dòng)到彈弓那,然后,松手,鳥就飛出去。這個(gè)過(guò)程是動(dòng)畫過(guò)程,做的事,實(shí)際上是清楚內(nèi)存。
二:原理:
其實(shí),沒(méi)什么原理,用到的就是WindowManager以及WindowManager.LayoutParams,對(duì)這個(gè)LayoutParams做文章,當(dāng)設(shè)置為屬性后,然后,創(chuàng)建一個(gè)View,將這個(gè)View添加到WindowManager中就行。
package com.chris.floats.window;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.WindowManager;
import android.app.Activity;
import android.content.Context;
public class MainActivity extends Activity {
private static WindowManager mWindowMgr = null;
private WindowManager.LayoutParams mWindowMgrParams = null;
private static FloatsWindowView mFloatsWindowView = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
/*
* 顯示應(yīng)用主界面時(shí),去除懸浮層
*/
@Override
public void onWindowFocusChanged(boolean hasFocus) {
if(hasFocus){
if(mFloatsWindowView != null){
mWindowMgr.removeView(mFloatsWindowView);
mFloatsWindowView = null;
}
}else{
getWindowLayout();
}
}
private void initParams(){
DisplayMetrics dm = getResources().getDisplayMetrics();
mWindowMgrParams.x = dm.widthPixels - 136;
mWindowMgrParams.y = 300;
mWindowMgrParams.width = 136;
mWindowMgrParams.height = 136;
}
private void getWindowLayout(){
if(mFloatsWindowView == null){
mWindowMgr = (WindowManager)getBaseContext().getSystemService(Context.WINDOW_SERVICE);
mWindowMgrParams = new WindowManager.LayoutParams();
/*
* 2003 在指懸浮在所有界面之上
* (4.0+系統(tǒng)中,在下拉菜單下面,而在2.3中,在上拉菜單之上)
*/
mWindowMgrParams.type = 2003;
mWindowMgrParams.format = 1;
/*
* 代碼實(shí)際是wmParams.flags |= FLAG_NOT_FOCUSABLE;
* 40的由來(lái)是wmParams的默認(rèn)屬性(32)+ FLAG_NOT_FOCUSABLE(8)
*/
mWindowMgrParams.flags = 40;
mWindowMgrParams.gravity = Gravity.LEFT | Gravity.TOP;
initParams();
mFloatsWindowView = new FloatsWindowView(this);
mWindowMgr.addView(mFloatsWindowView, mWindowMgrParams);
}
}
}
上面代碼,主要在getWindowLayout函數(shù)中,最后兩行就是創(chuàng)建一個(gè)View,并加入到WindowManager中。
繼承View的懸浮View:
package com.chris.floats.window;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.AnimationDrawable;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewTreeObserver.OnPreDrawListener;
import android.view.WindowManager;
public class FloatsWindowView extends View {
private Context mContext = null;
private WindowManager mWindowMgr = null;
private WindowManager.LayoutParams mWindowMgrParams = null;
private AnimationDrawable mAnimationDrawable = null;
private int iPosX = 0;
private int iPosY = 0;
private int iLastPosX = 0;
private int iLastPosY = 0;
private boolean bMoved = false;
public FloatsWindowView(Context context) {
this(context, null, 0);
}
public FloatsWindowView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public FloatsWindowView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
mContext = context;
mWindowMgr = (WindowManager)getContext().getApplicationContext().getSystemService("window");
mWindowMgrParams = new WindowManager.LayoutParams();
initParams();
mAnimationDrawable = new AnimationDrawable();
for(int i = 0; i < 4; i++){
int id = getResources().getIdentifier("a"+ i, "drawable", mContext.getPackageName());
mAnimationDrawable.addFrame(getResources().getDrawable(id), 100);
}
mAnimationDrawable.setOneShot(false);
this.setBackgroundDrawable(mAnimationDrawable);
OnPreDrawListener listener = new OnPreDrawListener(){
@Override
public boolean onPreDraw() {
mAnimationDrawable.start();
return true;
}
};
this.getViewTreeObserver().addOnPreDrawListener(listener);
}
private void initParams(){
DisplayMetrics dm = getResources().getDisplayMetrics();
mWindowMgrParams.x = dm.widthPixels - 136;
mWindowMgrParams.y = 300;
mWindowMgrParams.width = 136;
mWindowMgrParams.height = 136;
}
@Override
public boolean onTouchEvent(MotionEvent event) {
switch(event.getAction()){
case MotionEvent.ACTION_DOWN:
iPosX = (int)event.getX();
iPosY = (int)event.getY();
bMoved = false;
break;
case MotionEvent.ACTION_MOVE:
bMoved = true;
iLastPosX = (int)event.getX();
iLastPosY = (int)event.getY();
updatePostion(iLastPosX - iPosX, iLastPosY - iPosY);
break;
case MotionEvent.ACTION_UP:
if(!bMoved){
Intent it=new Intent(mContext, MainActivity.class);
mContext.startActivity(it);
}
break;
default:
break;
}
return true;
}
private void updatePostion(int x, int y){
mWindowMgrParams.type = 2003;
mWindowMgrParams.format = 1;
mWindowMgrParams.flags = 40;
mWindowMgrParams.gravity = Gravity.LEFT | Gravity.TOP;
mWindowMgrParams.x += x;
mWindowMgrParams.y += y;
mWindowMgr.updateViewLayout(this, mWindowMgrParams);
}
}
之所以將updatePosition中的參數(shù)與Activity中設(shè)置一樣,是為了確保在MOVE時(shí),造成相對(duì)位置的不一樣,而導(dǎo)致閃礫,大家要是不理解,可以實(shí)驗(yàn)下。
三、小結(jié):
這篇文章實(shí)現(xiàn)了簡(jiǎn)單的懸浮窗口動(dòng)畫效果,如果要想做成像360,QQ管家那樣,還需要一些其它的操作:
1. 比如啟動(dòng)一個(gè)后臺(tái)服務(wù)來(lái)監(jiān)控系統(tǒng)信息;
2. ACTION_DOWN時(shí),修改懸浮窗口上的圖片;
3. ACTION_MOVE時(shí)窗口跟隨;
4. ACTION_UP時(shí),創(chuàng)建一個(gè)線程,來(lái)完成釋放后,向上運(yùn)動(dòng)的動(dòng)畫過(guò)程等;
- android 添加隨意拖動(dòng)的桌面懸浮窗口
- Android實(shí)現(xiàn)桌面懸浮窗、蒙板效果實(shí)例代碼
- 不依賴于Activity的Android全局懸浮窗的實(shí)現(xiàn)
- Android 懸浮窗權(quán)限各機(jī)型各系統(tǒng)適配大全(總結(jié))
- Android應(yīng)用內(nèi)懸浮窗的實(shí)現(xiàn)方案示例
- Android實(shí)現(xiàn)類似qq微信消息懸浮窗通知功能
- Android 8.0如何完美適配全局dialog懸浮窗彈出
- Android懸浮窗屏蔽懸浮窗外部所有的點(diǎn)擊事件的實(shí)例代碼
- Android 獲取判斷是否有懸浮窗權(quán)限的方法
- android仿華為手機(jī)懸浮窗設(shè)計(jì)
相關(guān)文章
Android系統(tǒng)實(shí)現(xiàn)DroidPlugin插件機(jī)制
這篇文章主要為大家詳細(xì)介紹了Android系統(tǒng)上實(shí)現(xiàn)DroidPlugin插件機(jī)制,可以在無(wú)需安裝、修改的情況下運(yùn)行APK文件,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-01-01
android自定義滾動(dòng)上下回彈scollView
這篇文章主要為大家詳細(xì)介紹了android自定義滾動(dòng)上下回彈scollView,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-04-04
Android自定義Chronometer實(shí)現(xiàn)短信驗(yàn)證碼秒表倒計(jì)時(shí)功能
這篇文章主要介紹了Android自定義ChronometerView實(shí)現(xiàn)類似秒表倒計(jì)時(shí),短信驗(yàn)證碼倒計(jì)時(shí)功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-11-11
Android開發(fā)實(shí)現(xiàn)Files文件讀取解析功能示例
這篇文章主要介紹了Android開發(fā)實(shí)現(xiàn)Files文件讀取解析功能,結(jié)合實(shí)例形式分析了Android針對(duì)txt文本文件的讀取、保存功能實(shí)現(xiàn)方法與布局操作技巧,需要的朋友可以參考下2017-09-09
android編程實(shí)現(xiàn)圖片庫(kù)的封裝方法
這篇文章主要介紹了android編程實(shí)現(xiàn)圖片庫(kù)的封裝方法,涉及Android針對(duì)圖片的下載、保存、獲取及操作緩存圖片等相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-11-11
Android自定義View實(shí)現(xiàn)支付寶支付成功-極速get花式Path炫酷動(dòng)畫
這篇文章主要介紹了Android自定義View實(shí)現(xiàn)支付寶支付成功-極速get花式Path炫酷動(dòng)畫的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2017-01-01
Android應(yīng)用開發(fā)中Fragment的靜態(tài)加載與動(dòng)態(tài)加載實(shí)例
這篇文章主要介紹了Android應(yīng)用開發(fā)中Fragment的靜態(tài)加載與動(dòng)態(tài)加載實(shí)例,例子中包括動(dòng)態(tài)的添加更新以及刪除Fragment等操作,很有借鑒意義,需要的朋友可以參考下2016-02-02
Android開發(fā)中RecyclerView模仿探探左右滑動(dòng)布局功能
本文給大家分享android開發(fā)中RecyclerView模仿探探左右滑動(dòng)布局功能,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友參考下2017-01-01

