PopupWindow半透明背影

android应用需求要实现底部弹窗,而且需要半透明背影,使用PopupWindow实现的,PopupWindow背影网上有好多种实现,需要的自行搜索。下面给出我的解决方法,直接操作PopupWindow内的自定义view,做自定义view的动画,直接上代码:

public class TranslucentPopupWindow {


    private static final int ANIM_TIME = 300;
    private FrameLayout mRootView;
    private View mContentView;
    private PopupWindow mPopupWindow;


    public TranslucentPopupWindow(Context context) {
        mRootView = new FrameLayout(context);
        FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(
                FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT);
        mRootView.setLayoutParams(layoutParams);
        mRootView.setBackgroundColor(context.getResources().getColor(R.color.color_translucent));
        mRootView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
    }


    public void setCustomContentView(View view) {
        mContentView = view;
        addCustomContentView();
    }


    private void addCustomContentView() {
        if (null != mContentView) {
            FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(
                    FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.WRAP_CONTENT);
            layoutParams.gravity = Gravity.BOTTOM;
            layoutParams.leftMargin = mContentView.getResources().getDimensionPixelOffset(R.dimen.dialog_padding);//弹框距离屏幕左间距
            layoutParams.rightMargin = mContentView.getResources().getDimensionPixelOffset(R.dimen.dialog_padding);
            mRootView.addView(mContentView, layoutParams);
            mRootView.setVisibility(View.INVISIBLE);
        }
    }


    private void createWindow() {
        if (null == mPopupWindow) {
            mPopupWindow = new PopupWindow(mRootView);
            mPopupWindow.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
            mPopupWindow.setHeight(ViewGroup.LayoutParams.MATCH_PARENT);
            mPopupWindow.setOutsideTouchable(true);
            mPopupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
                @Override
                public void onDismiss() {
                    mEnterAnimatorSet = null;
                    mExitAnimatorSet = null;
                }
            });
        }
    }


    public void dismiss() {
        if (null != mPopupWindow && mPopupWindow.isShowing()) {
            startExitAnim();
        }
    }


    public boolean isShowing() {
        return null != mPopupWindow && mPopupWindow.isShowing();
    }


    public void showAtLocation(View parentView, int gravity, int x, int y) {
        if (null == mPopupWindow) {
            createWindow();
        }
        if (!mPopupWindow.isShowing()) {
            mRootView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
                @Override
                public void onGlobalLayout() {
                    mRootView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
                    mRootView.setVisibility(View.VISIBLE);
                    startEnterAnim();
                }
            });
            mPopupWindow.showAtLocation(parentView, gravity, x, y);
        }
    }


    private void startEnterAnim() {
        if (null != mContentView && (null == mEnterAnimatorSet || !mEnterAnimatorSet.isRunning())) {
            initEnterAnimatorSet();
            mEnterAnimatorSet.start();
        }
    }


    private AnimatorSet mEnterAnimatorSet;
    private void initEnterAnimatorSet() {
        if (null == mEnterAnimatorSet) {
            mContentView.setTranslationY(mContentView.getMeasuredHeight());
            mContentView.setAlpha(0);
            mEnterAnimatorSet = new AnimatorSet();
            ObjectAnimator animatorY = ObjectAnimator.ofFloat(mContentView, "translationY",
                    0);
            ObjectAnimator animatorAlpha = ObjectAnimator.ofFloat(mContentView, "alpha", 1);
            mRootView.setAlpha(0f);
            ObjectAnimator rootAnimatorAlpha = ObjectAnimator.ofFloat(mRootView, "alpha", 1);
            mEnterAnimatorSet.playTogether(animatorAlpha, animatorY, rootAnimatorAlpha);
            mEnterAnimatorSet.setDuration(ANIM_TIME);
        }
    }


    private void startExitAnim() {
        if (null != mContentView && (null == mExitAnimatorSet || !mExitAnimatorSet.isRunning())) {
            initExitAnimatorSet();
            mExitAnimatorSet.start();
        }
    }


    private AnimatorSet mExitAnimatorSet;
    private void initExitAnimatorSet() {
        if (null == mExitAnimatorSet) {
            mContentView.setTranslationY(0);
            mContentView.setAlpha(1);
            mExitAnimatorSet = new AnimatorSet();
            ObjectAnimator animatorY = ObjectAnimator.ofFloat(mContentView, "translationY",
                    mContentView.getMeasuredHeight());
            ObjectAnimator animatorAlpha = ObjectAnimator.ofFloat(mContentView, "alpha", 0);
            mRootView.setAlpha(1);
            ObjectAnimator rootAnimatorAlpha = ObjectAnimator.ofFloat(mRootView, "alpha", 0);
            mExitAnimatorSet.playTogether(animatorAlpha, animatorY, rootAnimatorAlpha);
            mExitAnimatorSet.setDuration(ANIM_TIME);
            mExitAnimatorSet.addListener(new Animator.AnimatorListener() {
                @Override
                public void onAnimationStart(Animator animation) {


                }


                @Override
                public void onAnimationEnd(Animator animation) {
                    if (null != mPopupWindow) {
                        mPopupWindow.dismiss();
                    }
                }


                @Override
                public void onAnimationCancel(Animator animation) {
                    if (null != mPopupWindow) {
                        mPopupWindow.dismiss();
                    }
                }


                @Override
                public void onAnimationRepeat(Animator animation) {


                }
            });
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值