出處:http://www.jb51.net/article/32321.htm
用過新版本android 360手機助手都人都對 360中只在桌面顯示一個小小懸浮窗口羨慕不已吧?
其實實現這種功能,主要有兩步:
1.判斷當前顯示的是爲桌面。這個內容我在前面的帖子裏面已經有過介紹,如果還沒看過的趕快穩步看一下哦。
2.使用windowManager往最頂層添加一個View
.這個知識點就是爲本文主要講解的內容哦。在本文的講解中,我們還會講到下面的知識點:
a.如果獲取到狀態欄的高度
b.懸浮窗口的拖動
c.懸浮窗口的點擊事件
FloatView 代碼:
import android.content.Context;
import android.graphics.Rect;
import android.util.Log;
import android.view.MotionEvent;
import android.view.WindowManager;
import android.widget.ImageView;
public class FloatView extends ImageView {
private final static String TAG = "FloatView";
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(Context.WINDOW_SERVICE);
// this windowManagerParams is system variable, to save window params.
private WindowManager.LayoutParams windowManagerParams = ((FloatApplication) getContext().getApplicationContext()).getWindowParams();
public FloatView(Context context) {
super(context);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
// Get the status bar height.
Rect frame = new Rect();
getWindowVisibleDisplayFrame(frame);
int statusBarHeight = frame.top;
Log.e(TAG,"statusBarHeight:" + statusBarHeight);
// get the x and y, base on left-top point.
x = event.getRawX();
y = event.getRawY() - statusBarHeight; // statusBarHeight is the height of system status bar.
Log.e(TAG, "currX" + x + "====currY" + y);
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN: // get the motion of finger
// get the view x&y, base on left-top point
mTouchX = event.getX();
mTouchY = event.getY();
mStartX = x;
mStartY = y;
Log.e(TAG, "startX" + mTouchX + "====startY" + mTouchY);
break;
case MotionEvent.ACTION_MOVE: // get the motion of finger.
updateViewPosition();
break;
case MotionEvent.ACTION_UP: // get the motion of finger leave.
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() {
// update the window position
windowManagerParams.x = (int) (x - mTouchX);
windowManagerParams.y = (int) (y - mTouchY);
windowManager.updateViewLayout(this, windowManagerParams); // refresh to show.
}
}
int statusBarHeight = frame.top;
爲獲取狀態欄的高度,爲什麼在event.getRawY()的時候減去狀態欄的高度呢?
因爲我們的懸浮窗口不可能顯示到狀態欄中去,而後getRawY爲獲取到屏幕原點的距離。當我們屏幕處於全屏模式時,獲取到的狀態欄高度會變成0
(x - mStartX) < 5 && (y - mStartY) < 5
如果我們在觸摸過程中,移動距離少於5 ,則視爲點擊,觸發點擊的回調。
另外我們需要自定義一個application:
import android.app.Application;
import android.view.WindowManager;
public class FloatApplication extends Application {
private WindowManager.LayoutParams windowParams = new WindowManager.LayoutParams();
public WindowManager.LayoutParams getWindowParams() {
return windowParams;
}
}
代碼解釋:
自定義application的目的是爲了保存windowParams的值 ,因爲我們在拖動懸浮窗口的時候,如果每次都重新new一個layoutParams的話,在update
的時候會在異常發現。
windowParams的值也不一定非得在自定義application裏面來保存,只要是全局的都行。
最後我們再來看看Activity中的實現:
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.graphics.PixelFormat;
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.view.WindowManager.LayoutParams;
import android.widget.Toast;
/**
* @author torv
*
*/
public class MainActivity extends Activity implements OnClickListener {
private WindowManager windowManager = null;
private WindowManager.LayoutParams windowManagerParams = null;
private FloatView floatView = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);//cancel title bar
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);//full screen
setContentView(R.layout.activity_main);
createView();
}
// @Override
// public boolean onCreateOptionsMenu(Menu menu) {
// getMenuInflater().inflate(R.menu.activity_main, menu);
// return true;
// }
public void onDestroy() {
super.onDestroy();
// destroy float window
windowManager.removeView(floatView);
}
private void createView() {
floatView = new FloatView((FloatApplication)getApplicationContext());
floatView.setOnClickListener(this);
floatView.setImageResource(R.drawable.ic_launcher); // use default icon.
//get WindowManager
windowManager = (WindowManager) getApplicationContext().getSystemService(Context.WINDOW_SERVICE);
// set LayoutParams(global) params
windowManagerParams = ((FloatApplication) getApplication()).getWindowParams();
windowManagerParams.type = LayoutParams.TYPE_PHONE; // set window type
windowManagerParams.format = PixelFormat.RGBA_8888; // set pic format, background transparent
// set Window flag
windowManagerParams.flags = LayoutParams.FLAG_NOT_TOUCH_MODAL|LayoutParams.FLAG_NOT_FOCUSABLE;
/*
* PS: flag value should be: LayoutParams.FLAG_NOT_TOUCH_MODAL not effect event
* LayoutParams.FLAG_NOT_FOCUSABLE unable focus. LayoutParams.FLAG_NOT_TOUCHABLE
* unable touch focus.
*/
// adjust the window to left-top
windowManagerParams.gravity = Gravity.LEFT | Gravity.TOP;
// set x.y base on left-top
windowManagerParams.x = 0;
windowManagerParams.y = 0;
// set float window height, weight.
windowManagerParams.width = LayoutParams.WRAP_CONTENT;
windowManagerParams.height = LayoutParams.WRAP_CONTENT;
// show my float view.
windowManager.addView(floatView, windowManagerParams);
}
public void onClick(View v) {
Toast.makeText(this, "Clicked", Toast.LENGTH_SHORT).show();
}
}
代碼解釋:
在activity中我們主要是添加懸浮窗,並且設置他的位置。另外需要注意flags的應用:
LayoutParams.FLAG_NOT_TOUCH_MODAL 不影響後面的事件
LayoutParams.FLAG_NOT_FOCUSABLE 不可聚焦
LayoutParams.FLAG_NOT_TOUCHABLE 不可觸摸
最後我們在onDestroy()中移除到懸浮窗口。所以,我們測試的時候,記得按Home鍵來切換到桌面。
最後千萬記得,在androidManifest.xml中來申明我們需要用到的android.permission.SYSTEM_ALERT_WINDOW權限
並且記得申明我們自定義的application哦。
AndroidManifest.xml代碼如下:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.torv.pro"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme"
android:name="FloatApplication">
<activity
android:name="com.torv.pro.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>