PopupWindow屏幕自动变暗半透明

PopupWindow弹出时一般会希望其余背景会变暗,看了网上的例子感受都比较繁琐,于是自己封装了个PopupWindow,只要像原来使用PopupWindow一样就能达到想要的效果

思路是这样:

在目标pop弹出前先弹出一个全屏灰色半透明的pop之后再显示目标pop,并且在目标pop消失时也让背景pop消失。


package com.gy.widget;

import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.PopupWindow;

/**
 * Created by gy on 2016/1/10.
 */
public class PopWrapper extends PopupWindow {

    private PopupWindow popBg;

    public PopWrapper(Context context) {
        super(context);
        createBgPop(context);
    }

    public PopWrapper(Context context, AttributeSet attrs) {
        super(context, attrs);
        createBgPop(context);
    }

    public PopWrapper(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        createBgPop(context);
    }

    public PopWrapper(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        createBgPop(context);
    }

    public PopWrapper(View contentView) {
        super(contentView);
        createBgPop(contentView.getContext());
    }

    public PopWrapper(Context context, int width, int height) {
        super(width, height);
        createBgPop(context);
    }

    public PopWrapper(View contentView, int width, int height) {
        super(contentView, width, height);
        createBgPop(contentView.getContext());
    }

    public PopWrapper(View contentView, int width, int height, boolean focusable) {
        super(contentView, width, height, focusable);
        createBgPop(contentView.getContext());
    }

    private View contentView;
    public void setContentView(View contentView) {
        super.setContentView(contentView);
        this.contentView=contentView;
        setWidth(ViewGroup.LayoutParams.WRAP_CONTENT);
        setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        setBackgroundDrawable(new ColorDrawable());
        setOutsideTouchable(true);
    }

    private void createBgPop(Context context) {
        setFocusable(true);
        View view = new View(context);
        view.setBackgroundColor(0x33000000);
        popBg = new PopupWindow(view, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        setOnDismissListener(new OnDismissListener() {
            @Override
            public void onDismiss() {
                popBg.dismiss();
            }
        });
    }

    public void setWrapperBackgroundColor(int color) {
        popBg.getContentView().setBackgroundColor(color);
    }

    @Override
    public void showAsDropDown(View anchor, int xoff, int yoff, int gravity) {
        popBg.showAtLocation(anchor, Gravity.TOP, 0, 0);
        super.showAsDropDown(anchor, xoff, yoff, gravity);
    }

    @Override
    public void showAsDropDown(View anchor, int xoff, int yoff) {
        popBg.showAtLocation(anchor, Gravity.TOP, 0, 0);
        super.showAsDropDown(anchor, xoff, yoff);
    }

    @Override
    public void showAsDropDown(View anchor) {
        popBg.showAtLocation(anchor, Gravity.TOP, 0, 0);
        super.showAsDropDown(anchor);
    }

    @Override
    public void showAtLocation(View parent, int gravity, int x, int y) {
        popBg.showAtLocation(parent, Gravity.TOP, 0, 0);
        super.showAtLocation(parent, gravity, x, y);
    }

    @Override
    public void update() {
        popBg.update();
        super.update();
    }

    public void update(int x, int y, int width, int height, boolean force) {
        popBg.update(x, y, width, height, force);
        super.update(x, y, width, height, force);
    }
}



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值