android 實現懸浮控制項

來源:互聯網
上載者:User

標籤:des   android   style   color   io   os   ar   java   for   


實現案頭View         如案頭歌詞

 1)將要顯示在案頭的view,通過WindowManager.addView,掛在到WindowManager下; 要注意,WindowManager對象來源,源碼有解釋。          並且配置好view的WindowManager.LayoutParams
         1-1)WindowManager.LayoutParams.type 設定為 WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY;時,案頭UI不可以直接setOnClickListener(),該View的onTouchEvent()函數也無效了         設定為WindowManager.LayoutParams.TYPE_SYSTEM_ALERT(2003)或者WindowManager.LayoutParams.TYPE_PHONE(2002)才有效。
1-2)WindowManager.LayoutParams.flag 設定為LayoutParams.FLAG_NOT_TOUCH_MODAL | LayoutParams.FLAG_NOT_FOCUSABLE
 如果設定了WindowManager.LayoutParams.type為2002,2003;那麼flag不設定為這兩個flag的話,那麼其他組件就無法獲得touch和focus事件了。
 設定了該兩個屬性之後就大家都可以獲得Touch和Focus事件

 2)androidManifest裡申明許可權
         <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />

package com.example.desktoptxt;import android.app.Activity;import android.content.Context;import android.content.Intent;import android.graphics.PixelFormat;import android.os.Bundle;import android.view.MotionEvent;import android.view.View;import android.view.View.OnClickListener;import android.view.WindowManager.LayoutParams;import android.view.WindowManager;import android.widget.Button;import android.widget.LinearLayout;import android.widget.TextView;import android.widget.Toast;/** * 實現案頭View *  * 1)將要顯示在案頭的view,通過WindowManager.addView,掛在到WindowManager下;要注意,WindowManager對象來源,源碼裡解釋。 * 並且配置好view的WindowManager.LayoutParams * 1-1)WindowManager.LayoutParams.type 設定為 WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY;時, * 案頭UI不可以直接setOnClickListener(),該View的onTouchEvent()函數也無效了 * 設定為WindowManager.LayoutParams.TYPE_SYSTEM_ALERT(2003)或者WindowManager.LayoutParams.TYPE_PHONE2002才有效。 * 1-2)WindowManager.LayoutParams.flag 設定為LayoutParams.FLAG_NOT_TOUCH_MODAL | LayoutParams.FLAG_NOT_FOCUSABLE * 如果設定了WindowManager.LayoutParams.type為2002,2003;那麼flag不設定為這兩個flag的話,那麼其他組件就無法獲得touch和focus事件了。 * 設定了該兩個屬性之後就大家都可以獲得Touch和Focus事件 *  * 2)androidManifest裡申明許可權 * <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" /> *  *  * @author jiese1990 * */public class MainActivity extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);TextView tv = new TextView(this);  tv.setText("this is a desktop text");tv.setTextColor(0xFFFF0000);tv.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {Toast.makeText(MainActivity.this, "click", Toast.LENGTH_SHORT).show();}});tv.setBackgroundColor(0xFF00000);initDestopText(tv);}private void initDestopText(View childView){//直接通過Activity擷取的 WindowManager,在act退出時,案頭組件也將退出。//WindowManager wm = (WindowManager)getSystemService(WINDOW_SERVICE); //一定要通過getApplicationContext()擷取WindowManager,這種情況下,當Application終止後,懸浮控制項才會被退出WindowManager wm = (WindowManager)getApplicationContext().getSystemService(WINDOW_SERVICE); WindowManager.LayoutParams params = new WindowManager.LayoutParams();  //params.type = WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY;  //如果設定為params.type = WindowManager.LayoutParams.TYPE_PHONE; // params.flags = LayoutParams.FLAG_NOT_TOUCH_MODAL | LayoutParams.FLAG_NOT_FOCUSABLE;params.width = WindowManager.LayoutParams.WRAP_CONTENT;  params.height = WindowManager.LayoutParams.WRAP_CONTENT;  wm.addView(childView, params);  }}


android 實現懸浮控制項

聯繫我們

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