標籤:
public class MainActivity extends Activity { private WindowManager mWindowManager; private WindowManager.LayoutParams mLayout; private DesktopLayout mDesktopLayout; private long startTime; // 聲明螢幕的寬高 float x, y; int top; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); createWindowManager(); createDesktopLayout(); Button btn = (Button) findViewById(R.id.btn); btn.setOnClickListener(new OnClickListener() { public void onClick(View v) { showDesk(); } }); } /** * 建立懸浮表單 */ private void createDesktopLayout() { mDesktopLayout = new DesktopLayout(this); mDesktopLayout.setOnTouchListener(new OnTouchListener() { float mTouchStartX; float mTouchStartY; @Override public boolean onTouch(View v, MotionEvent event) { // 擷取相對螢幕的座標,即以螢幕左上方為原點 x = event.getRawX(); y = event.getRawY() - top; // 25是系統狀態列的高度 Log.i("startP", "startX" + mTouchStartX + "====startY" + mTouchStartY); switch (event.getAction()) { case MotionEvent.ACTION_DOWN: // 擷取相對View的座標,即以此View左上方為原點 mTouchStartX = event.getX(); mTouchStartY = event.getY(); Log.i("startP", "startX" + mTouchStartX + "====startY" + mTouchStartY); long end = System.currentTimeMillis() - startTime; // 雙擊的間隔在 300ms以下 if (end < 300) { closeDesk(); } startTime = System.currentTimeMillis(); break; case MotionEvent.ACTION_MOVE: // 更新浮動視窗位置參數 mLayout.x = (int) (x - mTouchStartX); mLayout.y = (int) (y - mTouchStartY); mWindowManager.updateViewLayout(v, mLayout); break; case MotionEvent.ACTION_UP: // 更新浮動視窗位置參數 mLayout.x = (int) (x - mTouchStartX); mLayout.y = (int) (y - mTouchStartY); mWindowManager.updateViewLayout(v, mLayout); // 可以在此記錄最後一次的位置 mTouchStartX = mTouchStartY = 0; break; } return true; } }); } @Override public void onWindowFocusChanged(boolean hasFocus) { super.onWindowFocusChanged(hasFocus); Rect rect = new Rect(); // /取得整個視圖部分,注意,如果你要設定標題樣式,這個必須出現在標題樣式之後,否則會出錯 getWindow().getDecorView().getWindowVisibleDisplayFrame(rect); top = rect.top;//狀態列的高度,所以rect.height,rect.width分別是系統的高度的寬度 Log.i("top",""+top); } /** * 顯示DesktopLayout */ private void showDesk() { mWindowManager.addView(mDesktopLayout, mLayout); finish(); } /** * 關閉DesktopLayout */ private void closeDesk() { mWindowManager.removeView(mDesktopLayout); finish(); } /** * 設定WindowManager */ private void createWindowManager() { // 取得系統表單 mWindowManager = (WindowManager) getApplicationContext() .getSystemService("window"); // 表單的配置樣式 mLayout = new WindowManager.LayoutParams(); // 設定表單顯示類型――TYPE_SYSTEM_ALERT(系統提示) mLayout.type = WindowManager.LayoutParams.TYPE_SYSTEM_ALERT; // 設定表單焦點及觸摸: // FLAG_NOT_FOCUSABLE(不能獲得按鍵輸入焦點) mLayout.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE; // 設定顯示的模式 mLayout.format = PixelFormat.RGBA_8888; // 設定對齊的方法 mLayout.gravity = Gravity.TOP | Gravity.LEFT; // 設定表單寬度和高度 mLayout.width = WindowManager.LayoutParams.WRAP_CONTENT; mLayout.height = WindowManager.LayoutParams.WRAP_CONTENT; }}
public class DesktopLayout extends LinearLayout { public DesktopLayout(Context context) { super(context); setOrientation(LinearLayout.VERTICAL);// 水平排列 //設定寬高 this.setLayoutParams( new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); View view = LayoutInflater.from(context).inflate( R.layout.desklayout, null); this.addView(view); }}
別忘了在AndroidManifest.xml中加入如下的許可權:
public class DesktopLayout extends LinearLayout { public DesktopLayout(Context context) { super(context); setOrientation(LinearLayout.VERTICAL);// 水平排列 //設定寬高 this.setLayoutParams( new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); View view = LayoutInflater.from(context).inflate( R.layout.desklayout, null); this.addView(view); }}
Android 懸浮視窗