首先,得先說明,這個例子並不是我寫的,是從Android的一個帖子上面看到的,下載了他的源代碼,然後分析一下,供學習共享。(另外,對於其代碼有所修改,以便於更好的說明問題,另:同時把源代碼上傳,下載地址:
免費下載地址在 http://linux.linuxidc.com/
用戶名與密碼都是www.linuxidc.com
具體下載目錄在 /pub/Android源碼集錦/2011年/12月/Android懸浮窗--獲取內存/
一開始,我們先看一下運行效果:
其中,
這一塊就是懸浮窗,可以隨意拖動,動態顯示當前內存使用量。
下面看一下代碼是如何實現的:
懸浮窗的實現是用了一個service,為什麼要用service呢?了解service特點的大體就會明白。下面看一下:
- public class FloatService extends Service {
-
- WindowManager wm = null;
- WindowManager.LayoutParams wmParams = null;
- View view;
- private float mTouchStartX;
- private float mTouchStartY;
- private float x;
- private float y;
- int state;
- TextView tx1;
- TextView tx;
- ImageView iv;
- private float StartX;
- private float StartY;
- int delaytime=1000;
- @Override
- public void onCreate() {
- Log.d("FloatService", "onCreate");
- super.onCreate();
- view = LayoutInflater.from(this).inflate(R.layout.floating, null);
- tx = (TextView) view.findViewById(R.id.memunused);
- tx1 = (TextView) view.findViewById(R.id.memtotal);
- tx.setText("" + memInfo.getmem_UNUSED(this) + "KB");
- tx1.setText("" + memInfo.getmem_TOLAL() + "KB");
- iv = (ImageView) view.findViewById(R.id.img2);
- iv.setVisibility(View.GONE);
- createView();
- handler.postDelayed(task, delaytime);
- }
-
- private void createView() {
- // 獲取WindowManager
- wm = (WindowManager) getApplicationContext().getSystemService("window");
- // 設置LayoutParams(全局變量)相關參數
- wmParams = new WindowManager.LayoutParams();
- wmParams.type = 2002;
- wmParams.flags |= 8;
- wmParams.gravity = Gravity.LEFT | Gravity.TOP; // 調整懸浮窗口至左上角
- // 以屏幕左上角為原點,設置x、y初始值
- wmParams.x = 0;
- wmParams.y = 0;
- // 設置懸浮窗口長寬數據
- wmParams.width = WindowManager.LayoutParams.WRAP_CONTENT;
- wmParams.height = WindowManager.LayoutParams.WRAP_CONTENT;
- wmParams.format = 1;
-
- wm.addView(view, wmParams);
-
- view.setOnTouchListener(new OnTouchListener() {
- public boolean onTouch(View v, MotionEvent event) {
- // 獲取相對屏幕的坐標,即以屏幕左上角為原��
- x = event.getRawX();
- y = event.getRawY() - 25; // 25是系統狀態欄的高度
- Log.i("currP", "currX" + x + "====currY" + y);// 調試信息
- switch (event.getAction()) {
- case MotionEvent.ACTION_DOWN:
- state = MotionEvent.ACTION_DOWN;
- StartX = x;
- StartY = y;
- // 獲取相對View的坐標,即以此View左上角為原點
- mTouchStartX = event.getX();
- mTouchStartY = event.getY();
- Log.i("startP", "startX" + mTouchStartX + "====startY"
- + mTouchStartY);// 調試信息
- break;
- case MotionEvent.ACTION_MOVE:
- state = MotionEvent.ACTION_MOVE;
- updateViewPosition();
- break;
-
- case MotionEvent.ACTION_UP:
- state = MotionEvent.ACTION_UP;
-
- updateViewPosition();
- showImg();
- mTouchStartX = mTouchStartY = 0;
- break;
- }
- return true;
- }
- });
-
- iv.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View v) {
- // TODO Auto-generated method stub
- Intent serviceStop = new Intent();
- serviceStop.setClass(FloatService.this, FloatService.class);
- stopService(serviceStop);
- }
- });
-
- }
-
- public void showImg() {
- if (Math.abs(x - StartX) < 1.5 && Math.abs(y - StartY) < 1.5
- && !iv.isShown()) {
- iv.setVisibility(View.VISIBLE);
- } else if (iv.isShown()) {
- iv.setVisibility(View.GONE);
- }
- }
-
- private Handler handler = new Handler();
- private Runnable task = new Runnable() {
- public void run() {
- // TODO Auto-generated method stub
- dataRefresh();
- handler.postDelayed(this, delaytime);
- wm.updateViewLayout(view, wmParams);
- }
- };
-
- public void dataRefresh() {
- tx.setText("" + memInfo.getmem_UNUSED(this) + "KB");
- tx1.setText("" + memInfo.getmem_TOLAL() + "KB");
- }
-
- private void updateViewPosition() {
- // 更新浮動窗口位置參數
- wmParams.x = (int) (x - mTouchStartX);
- wmParams.y = (int) (y - mTouchStartY);
- wm.updateViewLayout(view, wmParams);
- }
-
- @Override
- public void onStart(Intent intent, int startId) {
- Log.d("FloatService", "onStart");
- setForeground(true);
- super.onStart(intent, startId);
- }
-
- @Override
- public void onDestroy() {
- handler.removeCallbacks(task);
- Log.d("FloatService", "onDestroy");
- wm.removeView(view);
- super.onDestroy();
- }
-
- @Override
- public IBinder onBind(Intent intent) {
- return null;
- }
- }