Android 全局悬浮按钮,悬浮按钮点击事件

实现效果:

Android 全局悬浮按钮,悬浮按钮点击事件_第1张图片Android 全局悬浮按钮,悬浮按钮点击事件_第2张图片

 

 

实现方法:

 在自定义baseActivity里面添加viwe即可。在子activity里刷新悬浮View即可

 

public abstract class BaseActivity extends BaseCommonActivity {

    LinearLayout saoli,ewmli;
    ImageView imageView;

    private QrCodeDialog mMQrCodeDialog;

    public static final int SECURITY_ACTIVITY_REQUEST_DD = 30008;

    //需要处理全屏逻辑之类的操作,在子类重写beforeSetContentView方法做处理
    @Override
    protected void beforeSetContentView() {
        super.beforeSetContentView();
        setTranslucentStatus();
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    }

    //需要在每个Activity处理逻辑,直接在onCreate方法处理
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        createFloatActionButtons();
        ObServernotice.getInstance().addObserver(observer);

    }

    MyObserver observer = new MyObserver() {
        @Override
        public void update(int id, int intparameter, final String string, Bitmap bitmap) {
            if (id==999){

                if (intparameter==0){
                    saoli.setVisibility(View.GONE);
                    ewmli.setVisibility(View.GONE);
                }else if (intparameter==1){
                    saoli.setVisibility(View.VISIBLE);
                    ewmli.setVisibility(View.VISIBLE);

                    mMQrCodeDialog = new QrCodeDialog(BaseActivity.this) {
                        @Override
                        public String setIvQrCode() {
                            return QrActivity.QR_TAG_ORDER + string;
                        }
                    };
                    imageView.setImageBitmap(bitmap);

                }
            }
        }
    };

    private void createFloatActionButtons() {
        View v = getWindow().getLayoutInflater().inflate(R.layout.layout_float_action_buttons, null);
        FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT, FrameLayout.LayoutParams.WRAP_CONTENT);
        layoutParams.gravity = Gravity.BOTTOM | Gravity.RIGHT;
        addContentView(v, layoutParams);
        saoli = findViewById(R.id.saoli);
        ewmli = findViewById(R.id.ewmli);
        imageView = findViewById(R.id.ddmaimg);
        initShowfloat();
        saoli.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                scan();
            }
        });
        ewmli.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mMQrCodeDialog != null) {
                    mMQrCodeDialog.show();
                }
            }
        });
    }

    public void initShowfloat(){
        int ss = ShareUtil.getSharedInt("floatint");
        if (ss==1){
            saoli.setVisibility(View.VISIBLE);
            ewmli.setVisibility(View.VISIBLE);
        }else {
            saoli.setVisibility(View.GONE);
            ewmli.setVisibility(View.GONE);
        }
        final String ewmstring =ShareUtil.getSharedString("ewmss");
        if (!TextUtils.isEmpty(ewmstring)){
            Bitmap bitmap = QRCodeEncoder.syncEncodeQRCode(QrActivity.QR_TAG_ORDER+ewmstring,
                    BGAQRCodeUtil.dp2px(BaseActivity.this, ResUtil.getPx(R.dimen.normal_340dp)),
                    ContextCompat.getColor(BaseActivity.this, R.color.black), null);
            mMQrCodeDialog = new QrCodeDialog(BaseActivity.this) {
                @Override
                public String setIvQrCode() {
                    return QrActivity.QR_TAG_ORDER + ewmstring;
                }
            };
            imageView.setImageBitmap(bitmap);

        }

    }

    private void scan() {
        startActivity(QrActivity.newIntent(this, 0));
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
        @NonNull int[] grantResults) {
        PermissionUtil.getInstance(getApplicationContext())
            .onRequestPermissionsResult(requestCode, permissions, grantResults);
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ObServernotice.getInstance().removeObserver(observer);
        observer = null;
    }
}

实现demo:https://download.csdn.net/download/meixi_android/12568936

 

在线bug交流:扣1085220040

 

你可能感兴趣的:(Android开发,悬浮View,悬浮按钮)