Android 懸浮視窗

來源:互聯網
上載者:User

標籤:

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 懸浮視窗

聯繫我們

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