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);
    }
}



你可能感兴趣的:(个人总结,PopupWindow,背景变暗,半透明)