Android 任何位置的可移動懸浮窗

恰好要作這塊的東西,因此網上翻了下資料,百度出來的基本上都是:默認起始位置左上角,還不能改動,一改動起始位置,第二次拖動就不正常了~~html

下面直接附上任意位置可拖動的源碼(因爲是demo寫的比較亂):java

package com.example.dengshaomin.repluginhost;

import android.content.Context;
import android.graphics.PixelFormat;
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.ImageView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
    private WindowManager wm = null;
    private WindowManager.LayoutParams wmParams = null;
    private ImageView leftbtn = null;
    private int statusBarHeight1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
        if (resourceId > 0) {
            //根據資源ID獲取響應的尺寸值
            statusBarHeight1 = getResources().getDimensionPixelSize(resourceId);
        }
        setContentView(R.layout.activity_main);
        initFloatView();
    }

    public void textClick(View view) {
        Toast.makeText(MainActivity.this, view.getId() + "", Toast.LENGTH_SHORT).show();
    }

    private void initFloatView() {
        //應用內懸浮窗,ondestory時記得銷燬懸浮窗
        /*
        wm = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
//        //設置LayoutParams(全局變量)相關參數
        wmParams = new WindowManager.LayoutParams(
                WindowManager.LayoutParams.WRAP_CONTENT,
                WindowManager.LayoutParams.WRAP_CONTENT,
                WindowManager.LayoutParams.TYPE_APPLICATION,
                WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL,
                PixelFormat.TRANSLUCENT);
*/
        ///全局懸浮按鈕,會顯示在其餘應用和桌面上
        //獲取WindowManager
        wm = (WindowManager) getApplicationContext().getSystemService(Context.WINDOW_SERVICE);
        //設置LayoutParams(全局變量)相關參數
        wmParams = new WindowManager.LayoutParams(
                WindowManager.LayoutParams.WRAP_CONTENT,
                WindowManager.LayoutParams.WRAP_CONTENT,
                WindowManager.LayoutParams.TYPE_TOAST,
                WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL,
                PixelFormat.TRANSLUCENT);
        wmParams.gravity = Gravity.LEFT | Gravity.CENTER_VERTICAL;
        //以屏幕左上角爲原點,設置x、y初始值
        wmParams.x = 0;
        wmParams.y = 0;
        //設置懸浮窗口長寬數據
        wmParams.width = 200;
        wmParams.height = 200;
        createLeftFloatView();
        leftbtn.invalidate();
    }

    private void createLeftFloatView() {
        leftbtn = new ImageView(this);
        leftbtn.setImageResource(R.mipmap.ic_launcher);
        leftbtn.setAlpha(100);
        leftbtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //Toast.makeText(MainActivity.this,"click",Toast.LENGTH_SHORT).show();
            }
        });
        //調整懸浮窗口
        //顯示myFloatView圖像
        wm.addView(leftbtn, wmParams);
        leftbtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(MainActivity.this, "float", Toast.LENGTH_SHORT).show();
            }
        });
        leftbtn.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                if (wm != null) {
                    wm.removeViewImmediate(leftbtn);
                }
//                Toast.makeText(MainActivity.this, "finish", Toast.LENGTH_SHORT).show();
                return true;
            }
        });
        leftbtn.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {

                // 當前值以屏幕左上角爲原點
                switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                        mStartX = event.getRawX();
                        mStartY = event.getRawY();
                        break;

                    case MotionEvent.ACTION_MOVE:
                        wmParams.x += event.getRawX() - mStartX;
                        wmParams.y += event.getRawY() - mStartY;
                        wm.updateViewLayout(leftbtn, wmParams);
                        mStartX = event.getRawX();
                        mStartY = event.getRawY();
                        break;
                    case MotionEvent.ACTION_UP:
                        break;
                }

                // 消耗觸摸事件
                return true;

            }
        });
    }


    private float mStartX, mStartY;
}

  

項目地址: http://ol58b2pn9.bkt.clouddn.com/+%E5%AF%B9%E8%AF%9D%E6%A1%86%E6%82%AC%E6%B5%AE%E7%AA%97.zipandroid

以上代碼,只有顯示及拖動懸浮框的處理,更高階的懸浮框能夠參考一下連接:app

  http://www.cnblogs.com/tianzhijiexian/p/3994546.htmlide

相關文章
相關標籤/搜索