android編程之懸浮表單

來源:互聯網
上載者:User

標籤:android   des   blog   http   java   使用   os   strong   

用過手機360和QQ手機管家等一些軟體的朋友,會發現,在這些應用中,會出現一個懸浮表單,例如QQ手機管家中打電話的情境:


這種表單除了會顯示外,還可以移動它的位置,並且一直顯示。除了關閉當前程式外,視窗不會主動消失。其實,它的使用原理也很簡單,就是借用了WindowManager這個管理類來實現的。
注意:要在AndroidManifest.xml中添加使用許可權:

[html] view plaincopy 
  1. <uses-permission  
  2.         android:name="android.permission.SYSTEM_ALERT_WINDOW" />  


這裡,我採用代碼布局的方式,模仿了一下QQ這個介面效果:

[java] view plaincopy 
  1. import android.content.Context;  
  2. import android.widget.ImageView;  
  3. import android.widget.LinearLayout;  
  4. import android.widget.TextView;  
  5.   
  6. public class DesktopLayout extends LinearLayout {  
  7.   
  8.     public DesktopLayout(Context context) {  
  9.         super(context);  
  10.         setOrientation(LinearLayout.HORIZONTAL);  
  11.         LayoutParams mLayoutParams = new LayoutParams(  
  12.                 LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);  
  13.         setLayoutParams(mLayoutParams);  
  14.   
  15.         // 顯示的ICON  
  16.         ImageView mImageView = new ImageView(context);  
  17.         mImageView.setImageResource(R.drawable.icon);  
  18.         addView(mImageView, mLayoutParams);  
  19.   
  20.         // 顯示的文字  
  21.         TextView mTextView = new TextView(context);  
  22.         mTextView.setText("Hello");  
  23.         mTextView.setTextSize(30);  
  24.         addView(mTextView, mLayoutParams);  
  25.     }  
  26. }  


接下來,在activity中讓它顯示出來。首先要設定一下WindowManager.LayoutParams:

[java] view plaincopy 
  1. // 取得系統表單  
  2. mWindowManager = (WindowManager) getApplicationContext()  
  3.         .getSystemService("window");  
  4.   
  5. // 表單的配置樣式  
  6. mLayoutParams = new WindowManager.LayoutParams();  
  7.   
  8. // 設定表單顯示類型——TYPE_SYSTEM_ALERT(系統提示)  
  9. mLayoutParams.type = WindowManager.LayoutParams.TYPE_SYSTEM_ALERT;  
  10.   
  11. // 設定表單焦點及觸摸:  
  12. // FLAG_NOT_FOCUSABLE(不能獲得按鍵輸入焦點)  
  13. mLayoutParams.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;  
  14.   
  15. // 設定顯示的模式  
  16. mLayoutParams.format = PixelFormat.RGBA_8888;  
  17.   
  18. // 設定對齊的方法  
  19. mLayoutParams.gravity = Gravity.TOP | Gravity.LEFT;  
  20.   
  21. // 設定表單寬度和高度  
  22. mLayoutParams.width = WindowManager.LayoutParams.WRAP_CONTENT;  
  23. mLayoutParams.height = WindowManager.LayoutParams.WRAP_CONTENT;  
  24.   
  25. // 設定表單顯示的位置,否則在螢幕中心顯示  
  26. mLayoutParams.x = 50;  
  27. mLayoutParams.y = 50;  


顯示表單與關閉表單的方法:

[java] view plaincopy 
  1. mWindowManager.addView(mDesktopLayout, mLayoutParams);  
  2.   
  3. mWindowManager.removeView(mDesktopLayout);  


以下是activity的原代碼,這裡設計了一個雙擊關閉表單的效果:

[java] view plaincopy 
  1. import android.app.Activity;  
  2. import android.graphics.PixelFormat;  
  3. import android.os.Bundle;  
  4. import android.view.Gravity;  
  5. import android.view.MotionEvent;  
  6. import android.view.View;  
  7. import android.view.View.OnClickListener;  
  8. import android.view.View.OnTouchListener;  
  9. import android.view.WindowManager;  
  10. import android.widget.Button;  
  11.   
  12. public class DeskTip extends Activity {  
  13.   
  14.     private WindowManager mWindowManager;  
  15.   
  16.     private WindowManager.LayoutParams mLayoutParams;  
  17.   
  18.     private DesktopLayout mDesktopLayout;  
  19.   
  20.     private long starttime;  
  21.   
  22.     /** 
  23.      * 建立懸浮表單 
  24.      */  
  25.     private void createDesktopLayout() {  
  26.         mDesktopLayout = new DesktopLayout(this);  
  27.         mDesktopLayout.setOnTouchListener(new OnTouchListener() {  
  28.             public boolean onTouch(View v, MotionEvent event) {  
  29.                 onActionMove(event);  
  30.                 return true;  
  31.             }  
  32.         });  
  33.     }  
  34.   
  35.     /** 
  36.      * 設定WindowManager 
  37.      */  
  38.     private void createWindowManager() {  
  39.         // 取得系統表單  
  40.         mWindowManager = (WindowManager) getApplicationContext()  
  41.                 .getSystemService("window");  
  42.   
  43.         // 表單的配置樣式  
  44.         mLayoutParams = new WindowManager.LayoutParams();  
  45.   
  46.         // 設定表單顯示類型——TYPE_SYSTEM_ALERT(系統提示)  
  47.         mLayoutParams.type = WindowManager.LayoutParams.TYPE_SYSTEM_ALERT;  
  48.   
  49.         // 設定表單焦點及觸摸:  
  50.         // FLAG_NOT_FOCUSABLE(不能獲得按鍵輸入焦點)  
  51.         mLayoutParams.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;  
  52.   
  53.         // 設定顯示的模式  
  54.         mLayoutParams.format = PixelFormat.RGBA_8888;  
  55.   
  56.         // 設定對齊的方法  
  57.         mLayoutParams.gravity = Gravity.TOP | Gravity.LEFT;  
  58.   
  59.         // 設定表單寬度和高度  
  60.         mLayoutParams.width = WindowManager.LayoutParams.WRAP_CONTENT;  
  61.         mLayoutParams.height = WindowManager.LayoutParams.WRAP_CONTENT;  
  62.   
  63.         // 設定表單顯示的位置,否則在螢幕中心顯示  
  64.         mLayoutParams.x = 50;  
  65.         mLayoutParams.y = 50;  
  66.     }  
  67.   
  68.     private void onActionMove(MotionEvent event) {  
  69.   
  70.         if (event.getAction() == MotionEvent.ACTION_DOWN) {  
  71.             long end = System.currentTimeMillis() - starttime;  
  72.             // 雙擊的間隔在 200ms 到 500ms 之間  
  73.             if (end > 200 && end < 500) {  
  74.                 closeDesk();  
  75.                 return;  
  76.             }  
  77.             starttime = System.currentTimeMillis();  
  78.         }  
  79.   
  80.         mLayoutParams.x = (int) (event.getRawX() - (mDesktopLayout.getWidth()));  
  81.         mLayoutParams.y = (int) (event.getRawY() - (mDesktopLayout.getHeight()));  
  82.   
  83.         mWindowManager.updateViewLayout(mDesktopLayout, mLayoutParams);  
  84.     }  
  85.   
  86.     /** 
  87.      * 顯示DesktopLayout 
  88.      */  
  89.     private void showDesk() {  
  90.         mWindowManager.addView(mDesktopLayout, mLayoutParams);  
  91.         finish();  
  92.     }  
  93.   
  94.     /** 
  95.      * 關閉DesktopLayout 
  96.      */  
  97.     private void closeDesk() {  
  98.         mWindowManager.removeView(mDesktopLayout);  
  99.         finish();  
  100.     }  
  101.   
  102.     public void onCreate(Bundle savedInstanceState) {  
  103.         super.onCreate(savedInstanceState);  
  104.         setContentView(R.layout.main);  
  105.         createWindowManager();  
  106.         createDesktopLayout();  
  107.   
  108.         Button btn = (Button) findViewById(R.id.btn);  
  109.         btn.setOnClickListener(new OnClickListener() {  
  110.             public void onClick(View v) {  
  111.                 showDesk();  
  112.             }  
  113.         });  
  114.     }  
  115. }  


顯示的效果:



懸浮表單例子

 

http://blog.csdn.net/xyz_fly/article/details/7546096

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.