Android之浮動小視窗

來源:互聯網
上載者:User

//建立建立全域變數類

 1 public class MyApplication extends Application {
 2     
 3     /**
 4      * 建立全域變數
 5      * 全域變數一般都比較傾向於建立一個單獨的資料類檔案,並使用static靜態變數
 6      * 
 7      * 這裡使用了在Application中添加資料的方法實現全域變數
 8      * 注意在AndroidManifest.xml中的Application節點添加android:name=".MyApplication"屬性
 9      * 
10      */
11     private WindowManager.LayoutParams wmParams=new WindowManager.LayoutParams();
12 
13     public WindowManager.LayoutParams getMywmParams(){
14         return wmParams;
15     }

 

//Activity類 

 1 public class MyFloatViewActivity extends Activity {
 2    
 3 
 4     /** Called when the activity is first created. */
 5     
 6     private WindowManager wm=null;
 7     private WindowManager.LayoutParams wmParams=null;
 8     
 9     private MyFloatView myFV=null;
10 
11     
12     @Override
13     public void onCreate(Bundle savedInstanceState) {
14         super.onCreate(savedInstanceState);
15         setContentView(R.layout.main);
16     
18     }
19     
22     private void createView(){
23         myFV=new MyFloatView(getApplicationContext());
24         myFV.setImageResource(R.drawable.icon);
25         //擷取WindowManager
26         wm=(WindowManager)getApplicationContext().getSystemService("window");
27         //設定LayoutParams(全域變數)相關參數
28         wmParams = ((MyApplication)getApplication()).getMywmParams();
29 
30          /**
31          *以下都是WindowManager.LayoutParams的相關屬性
32          * 具體用途可參考SDK文檔
33          */
34         wmParams.type=LayoutParams.TYPE_PHONE;   //設定window type
35         wmParams.format=PixelFormat.RGBA_8888;   //設定圖片格式,效果為背景透明
36 
37         //設定Window flag
38         wmParams.flags=LayoutParams.FLAG_NOT_TOUCH_MODAL
39                               | LayoutParams.FLAG_NOT_FOCUSABLE;
40         /*
41          * 下面的flags屬性的效果形同“鎖定”。
42          * 懸浮窗不可觸摸,不接受任何事件,同時不影響後面的事件響應。
43          wmParams.flags=LayoutParams.FLAG_NOT_TOUCH_MODAL 
44                                | LayoutParams.FLAG_NOT_FOCUSABLE
45                                | LayoutParams.FLAG_NOT_TOUCHABLE;
46         */
47         
48         
49         wmParams.gravity=Gravity.LEFT|Gravity.TOP;   //調整懸浮視窗至左上方
50         //以螢幕左上方為原點,設定x、y初始值
51         wmParams.x=0;
52         wmParams.y=0;
53         
54         //設定懸浮視窗長寬資料
55         wmParams.width=40;
56         wmParams.height=40;
57     
58         //顯示myFloatView映像
59         wm.addView(myFV, wmParams);
60         
61     }
62     
63     @Override
64     public void onDestroy(){
65         super.onDestroy();
66         //在程式退出(Activity銷毀)時銷毀懸浮視窗
67         wm.removeView(myFV);
68     }
69     
70     @Override
71     protected void onRestart() {
72         // TODO Auto-generated method stub
73         wm.removeView(myFV);
74         super.onRestart();
75     }
76 
79     @Override
80     protected void onStop() {
81         // TODO Auto-generated method stub
82         //建立懸浮視窗
83         createView();
84         super.onStop();
85     }  

 

//浮動視窗內容類別

 

 1 public class MyFloatView extends ImageView {
 2     private float mTouchStartX;
 3     private float mTouchStartY;
 4     private float x;
 5     private float y;
 6     
 8     private WindowManager wm=(WindowManager)getContext().getApplicationContext().getSystemService("window");
 9     
10     //此wmParams為擷取的全域變數,用以儲存懸浮視窗的屬性
11     private WindowManager.LayoutParams wmParams = ((MyApplication)getContext().getApplicationContext()).getMywmParams();
12 
13     public MyFloatView(Context context) {
14         super(context);        
15         // TODO Auto-generated constructor stub
16     }
17     
18      @Override
19      public boolean onTouchEvent(MotionEvent event) {
20          
21          //getRawX()擷取相對螢幕的座標,即以螢幕左上方為原點         
22          x = event.getRawX();   
23          y = event.getRawY()-25;   //25是系統狀態列的高度
24          Log.i("currP", "currX"+x+"====currY"+y);
25          switch (event.getAction()) {
26             case MotionEvent.ACTION_DOWN:
27                 //getX()擷取相對View的座標,即以此View左上方為原點
28                 mTouchStartX =  event.getX(); 
29                 mTouchStartY =  event.getY();
30                 
31                 Log.i("startP", "startX"+mTouchStartX+"====startY"+mTouchStartY);
32                 
33                 break;
34             case MotionEvent.ACTION_MOVE:                
35                 updateViewPosition();
36                 break;
37 
38             case MotionEvent.ACTION_UP:
39                 updateViewPosition();
40                 mTouchStartX=mTouchStartY=0;
41                 break;
42             }
43             return true;
44         }
45      
46      private void updateViewPosition(){
47         //更新浮動視窗位置參數,x是滑鼠在螢幕的位置,mTouchStartX是滑鼠在圖片的位置
48         wmParams.x=(int)( x-mTouchStartX);
49         System.out.println(mTouchStartX);
50         wmParams.y=(int) (y-mTouchStartY);
51         wm.updateViewLayout(this, wmParams);
52         
53      }
54 
55 }

 

在androidManifest中設定:

<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" /> 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.