android 顶部弹窗的2种实现方式

1.popWindow

public class PopWindowUtil {
    private View view;
    private  PopupWindow   mPopupWindow;
    private int layou_id;
    private Activity mContext;
    private boolean isShowAsView;//是否在某个View下面显示
    private boolean isMatchParent;//宽高是否是Match_Parent
    public PopWindowUtil(int layou_id,Activity mContext,boolean isShowAsView,boolean isMatchParent){
        view=LayoutInflater.from(mContext).inflate(layou_id, null);
        setView(view);
        this.mContext=mContext;
        this.isShowAsView=isShowAsView;
        this.isMatchParent=isMatchParent;
        mPopupWindow = new PopupWindow(mContext);
        setmPopupWindow(mPopupWindow);
    }
    public void showPop(View dropDownView) {
        // 设置布局文件
        mPopupWindow.setContentView(view);
        // 为了避免部分机型不显示,我们需要重新设置一下宽高
        if(!isMatchParent){
            mPopupWindow.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
            mPopupWindow.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        }else{
            mPopupWindow.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
            mPopupWindow.setHeight(ViewGroup.LayoutParams.MATCH_PARENT);
        }
        // 设置pop透明效果
        mPopupWindow.setBackgroundDrawable(new ColorDrawable(0x0000));
        // 设置pop出入动画
        if(isShowAsView){
            mPopupWindow.setAnimationStyle(R.style.pop_add);
        }
        // 设置pop获取焦点,如果为false点击返回按钮会退出当前Activity,如果pop中有Editor的话,focusable必须要为true
        mPopupWindow.setFocusable(true);

        // 设置点击pop外侧消失,默认为false;在focusable为true时点击外侧始终消失
        // 设置pop可点击,为false点击事件无效,默认为true
        mPopupWindow.setTouchable(true);
        mPopupWindow.setOutsideTouchable(true);
        // 相对于 + 号正下面,同时可以设置偏移量
        if(isShowAsView){
            mPopupWindow.showAsDropDown(dropDownView, -100, -12);
        }else{
            mPopupWindow.showAtLocation(dropDownView, Gravity.TOP, 0, 0);
        }
    }


    public View getView() {
        return view;
    }

    public void setView(View view) {
        this.view = view;
    }

    public PopupWindow getmPopupWindow() {
        return mPopupWindow;
    }

    public void setmPopupWindow(PopupWindow mPopupWindow) {
        this.mPopupWindow = mPopupWindow;
    }
}

然后设置好位置就好了,emm..动画就想法子自己实现吧

2.直接弄个布局实现,然后通过属性动画的方式展示出来,给人一种弹窗的感觉,实际就是一个控件

 /**
     * View展开动画
     * @param v
     * @param mHiddenViewMeasuredHeight
     */
    public static void animateOpen(View v, int mHiddenViewMeasuredHeight) {
        v.setVisibility(View.VISIBLE);
        ValueAnimator animator = createDropAnimator(v, 0,
                mHiddenViewMeasuredHeight);
        animator.start();
    }

    /**
     * View折叠动画
     * @param view
     */
    public static void animateClose(final View view, final AnimaionLoadEndListener listener) {
        int origHeight = view.getHeight();
        ValueAnimator animator = createDropAnimator(view, origHeight, 0);
        animator.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                view.setVisibility(View.GONE);
                if(ValidateUtils.isValidate(listener)){
                    listener.onLoadEnd();
                }

            }
        });
        animator.start();
    }

    private static ValueAnimator createDropAnimator(final View v, int start, int end) {
        ValueAnimator animator = ValueAnimator.ofInt(start, end);
        animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {

            @Override
            public void onAnimationUpdate(ValueAnimator arg0) {
                int value = (int) arg0.getAnimatedValue();
                ViewGroup.LayoutParams layoutParams = v.getLayoutParams();
                layoutParams.height = value;
                v.setLayoutParams(layoutParams);

            }
        });
        return animator;
    }
    public interface AnimaionLoadEndListener{
        void onLoadEnd();
    }

直接拿过去用就好,你懂得哦

猜你喜欢

转载自blog.csdn.net/z936689039/article/details/108329440