android纯Java代码写的addView弹窗(亲测可用,欢迎留言)

本文参考了https://blog.csdn.net/u010648159/article/details/50493847,并改进了一个原文中的bug。

 

 

package com.view;

import android.animation.AnimatorSet;
import android.content.Context;
import android.graphics.PixelFormat;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.LinearLayout;

/**
 * Created by Administrator on 2018/6/26 0026.
 */

public abstract class BaseActivity extends AppCompatActivity implements View.OnTouchListener {

    protected Button mFloatingButton;
    private WindowManager.LayoutParams mLayoutParams;
    protected WindowManager mWindowManager;

    private AnimatorSet mShowAnimatorSet, mHideAnimatorSet;

    private int mStartX;
    private int mStartY;
    private int mRawX;
    private int mRawY;
    private static final String TAG = "BaseActivity";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mWindowManager = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
        initButton();
        openView();
    }

    protected abstract void initView();

    protected void initButton() {
        mFloatingButton = new Button(this);
        mFloatingButton.setText("button1");
        mFloatingButton.setVisibility(View.GONE);
        mLayoutParams = new WindowManager.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT, 0, 0, PixelFormat.TRANSPARENT);
        mLayoutParams.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
        //mLayoutParams.type = WindowManager.LayoutParams.TYPE_SYSTEM_ALERT;
        mLayoutParams.type = WindowManager.LayoutParams.TYPE_APPLICATION;
        mLayoutParams.gravity = Gravity.LEFT | Gravity.TOP;
        mLayoutParams.x = 10;
        mLayoutParams.y = 300;

        mLayoutParams.windowAnimations = android.R.style.Animation_Toast;

        mWindowManager.addView(mFloatingButton, mLayoutParams);

        /*
         * 写不写根据自己的需求而定
         */
        mFloatingButton.setOnTouchListener(new View.OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {

                mRawX = (int) event.getRawX();
                mRawY = (int) event.getRawY();

                switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                        mStartX = (int) event.getX();
                        mStartY = (int) event.getY();
                        mLayoutParams.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;

                        break;
                    case MotionEvent.ACTION_MOVE:

                        mLayoutParams.x = mRawX - mStartX;
                        mLayoutParams.y = mRawY - mStartY;

                        mWindowManager.updateViewLayout(mFloatingButton, mLayoutParams);

                        break;

                    default:
                        break;
                }
                return false;
            }
        });

        mFloatingButton.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                System.out.println("按钮的点击事件");
            }
        });
    }

    /**
     * 展示弹窗
     */
    public void openView() {
        mFloatingButton.setVisibility(View.VISIBLE);
    }

    /**
     * 关闭弹窗
     */
    private void closeView() {
        mFloatingButton.setVisibility(View.GONE);
    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        return false;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mWindowManager != null) {
            //1.此方法谨慎使用,容易造成内存泄漏
//            mWindowManager.removeView(mFloatingButton);
            //2.这个方法不会造成内存泄露(建议使用)
            mWindowManager.removeViewImmediate(mFloatingButton);
        }
    }
}

源码已贴出,如果有看不清的地方,可联系qq1164688204,我直接给源码。

 

 

 

 

 

你可能感兴趣的:(android)