浮動窗體的控件:類iphone球

import android.content.Context;
import android.content.Intent;
import android.graphics.PixelFormat;
import android.provider.Settings;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

//textView爲懸浮的view,現在設置爲static
private static TextView textView = null;
//要有處理window的對象
private WindowManager windowManager = null;

//用於創建view的上下文
private Context context = null;

//懸浮窗的樣式控制(包裹內容、設置位置)
private WindowManager.LayoutParams lp = null;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    //實例上下文
    context = getApplicationContext();
    //實例windowmanager,它是系統服務,直接get
    windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);

    //懸浮窗樣式對象
    lp = new WindowManager.LayoutParams();
    //設置成在屏幕懸浮的效果
    //如下寫法爲api 19以前的寫法,新版api要求這個lp.type只能是一個值
    //2003|2006=?     200     011    110     111=7

// lp.type=WindowManager.LayoutParams.TYPE_SYSTEM_ALERT
// |WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY;
lp.type = WindowManager.LayoutParams.TYPE_PRIORITY_PHONE;

    //不獲取焦點,不全屏
    lp.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
            | WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN;

    //設置位置
    lp.x = 10;
    lp.y = 50;

    //設置一個位置的相對標準
    lp.gravity = Gravity.LEFT | Gravity.TOP;


    //寬高包裹內容
    lp.width = WindowManager.LayoutParams.WRAP_CONTENT;
    lp.height = WindowManager.LayoutParams.WRAP_CONTENT;

    //懸浮窗一般都是透明效果的,因爲怕影響觀看桌面的應用
    lp.format = PixelFormat.TRANSPARENT;


    if (textView == null) {
        //下面創建要懸浮的view
        textView = new TextView(context);
        //把之前設置好的樣式交給這個懸浮的view

        //layoutparams不匹配,報錯爲classcastexception
        //我們應該把這個關於view的樣式交給windowmanager來進行管理
        // textView.setLayoutParams(lp);
        textView.setText("123456789");

        windowManager.addView(textView, lp);
    } else {
        //如果這個textView已經存在,如何刪除它
        windowManager.removeView(textView);
        textView = null;
    }


    //點擊?移動?都怎麼辦?
    if (textView != null) {
        //點擊的監聽,直接跳轉進來
        textView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, MainActivity.class);
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK
                        | Intent.FLAG_ACTIVITY_CLEAR_TOP
                );
                startActivity(intent);
            }
        });

        //移動?
        //用touchlistener來監聽,然後實現移動效果
        textView.setOnTouchListener(new View.OnTouchListener() {

            private long lastDownTime;
            private int lastX;
            private int lastY;
            //把懸浮窗的屬性複製一份
            private WindowManager.LayoutParams mlp = lp;

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                boolean ret = false;
                switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                        lastDownTime = System.currentTimeMillis();
                        lastX = (int) event.getRawX();
                        lastY = (int) event.getRawY();
                        ret = true;
                        break;
                    case MotionEvent.ACTION_MOVE:
                        //移動
                        //獲取這個event相對於手機屏幕的寬高
                        float x = event.getRawX();
                        float y = event.getRawY();
                        //計算這一次移動的x和y的增量
                        int ix = (int) (x - lastX);
                        int iy = (int) (y - lastY);
                        //把增量設置給lp
                        mlp.x += ix;
                        mlp.y += iy;
                        //最後還要把這一次移動的x,y作爲lastX設lastY
                        lastX = (int) x;
                        lastY = (int) y;

                        //更新懸浮窗的位置
                        windowManager.updateViewLayout(textView, mlp);
                        break;
                    case MotionEvent.ACTION_UP:
                        long time = System.currentTimeMillis();
                        if (time - lastDownTime < 300) {
                            //觸發點擊監聽
                            textView.performClick();
                        }
                        break;

                }

                return ret;
            }
        });


    }


}

}

發佈了29 篇原創文章 · 獲贊 7 · 訪問量 1萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章