Android-仿微博自定义底部动态弹出的半透明PopupWindow

首先定义一个PublishPopWindow

导入注解jar包(部分jar包可不用到)

   compile 'io.reactivex.rxjava2:rxjava:2.1.0'
    // 必要rxjava2依赖
    compile 'io.reactivex.rxjava2:rxandroid:2.0.1'
    // 必要rxandrroid依赖,切线程时需要用到
    compile 'com.squareup.retrofit2:retrofit:2.3.0'
    // 必要retrofit依赖
    compile 'com.squareup.retrofit2:adapter-rxjava2:2.3.0'
public class PublishPopWindow extends  PopupWindow  implements View.OnClickListener {
    private View rootView;
    private RelativeLayout contentView;
    private Activity mContext;
    public PublishPopWindow(Activity context) {
        this.mContext = context;
    }

    public void showMoreWindow(View anchor) {
        LayoutInflater inflater = (LayoutInflater) mContext
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        rootView = inflater.inflate(R.layout.dialog_publish, null);
        int h = mContext.getWindowManager().getDefaultDisplay().getHeight();
        int w = mContext.getWindowManager().getDefaultDisplay().getWidth();
        setContentView(rootView);
        this.setWidth(w);
       // this.setHeight(h - ScreenUtils.getStatusHeight(mContext));
        this.setHeight(h - UIUtils.getBarHeight(mContext));
        contentView = (RelativeLayout) rootView.findViewById(R.id.contentView);
        contentView.setOnClickListener(this);

       //设置您想要的透明度
        contentView.getBackground().setAlpha(250);

        rootView.findViewById(R.id.wimdow_zhibo_lay).setOnClickListener(this);
        rootView.findViewById(R.id.wimdow_youji_lay).setOnClickListener(this);
        rootView.findViewById(R.id.wimdow_tuwen_lay).setOnClickListener(this);
        rootView.findViewById(R.id.wimdow_bobao_lay).setOnClickListener(this);


        LinearLayout linearLayout1=rootView.findViewById(R.id.wimdow_zhibo_lay);
        LinearLayout linearLayout2=rootView.findViewById(R.id.wimdow_youji_lay);
        LinearLayout linearLayout3=rootView.findViewById(R.id.wimdow_tuwen_lay);
        LinearLayout linearLayout4=rootView.findViewById(R.id.wimdow_bobao_lay);
        //设置四个view使其占屏幕四分之一的宽度
        DisplayMetrics dm = mContext.getResources().getDisplayMetrics();
        linearLayout1.setMinimumWidth(dm.widthPixels/4);
        linearLayout2.setMinimumWidth(dm.widthPixels/4);
        linearLayout3.setMinimumWidth(dm.widthPixels/4);
        linearLayout4.setMinimumWidth(dm.widthPixels/4);


        RelativeLayout close = (RelativeLayout) rootView.findViewById(R.id.ll_close);
       // close.setBackgroundColor(0xFFFFFFFF);
        close.setBackgroundColor(mContext.getResources().getColor(R.color.ffffff));
        close.setOnClickListener(this);
        showAnimation(contentView);
       // setBackgroundDrawable(mContext.getResources().getDrawable(R.drawable.translucence_with_white));
        setOutsideTouchable(true);
        setFocusable(true);
        try {
            if(!isShowing()) {
                showAtLocation(anchor, Gravity.BOTTOM, 0, 0);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }
 /**
     * 显示进入动画效果
     * @param layout
     */
    private void showAnimation(ViewGroup layout) {
        //遍历根试图下的一级子试图
        for (int i = 0; i < layout.getChildCount(); i++) {
            final View child = layout.getChildAt(i);
            //忽略关闭组件
            if (child.getId() == R.id.ll_close) {
                continue;
            }
            //设置所有一级子试图的点击事件
            child.setOnClickListener(this);
            child.setVisibility(View.INVISIBLE);
            //延迟显示每个子试图(主要动画就体现在这里)
            Observable.timer(i * 50, TimeUnit.MILLISECONDS)
                    .subscribeOn(Schedulers.newThread())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new Consumer() {
                        @Override
                        public void accept(Long aLong) throws Exception {
                            child.setVisibility(View.VISIBLE);
                            ValueAnimator fadeAnim = ObjectAnimator.ofFloat(child, "translationY", 600, 0);
                            fadeAnim.setDuration(300);
                            MyAnimator kickAnimator = new MyAnimator();
                            kickAnimator.setDuration(150);
                            fadeAnim.setEvaluator(kickAnimator);
                            fadeAnim.start();
                        }
/*
                        @Override
                        public void call(Long aLong) {

                        }*/
                    });
        }

    }

    /**
     * 关闭动画效果
     * @param layout
     */
    private void closeAnimation(ViewGroup layout) {
        for (int i = 0; i < layout.getChildCount(); i++) {
            final View child = layout.getChildAt(i);
            if (child.getId() == R.id.ll_close) {
                continue;
            }
            Observable.timer((layout.getChildCount() - i - 1) * 30, TimeUnit.MILLISECONDS)
                    .subscribeOn(Schedulers.newThread())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new Consumer() {
                        @Override
                        public void accept(Long aLong) throws Exception {
                            child.setVisibility(View.VISIBLE);
                            ValueAnimator fadeAnim = ObjectAnimator.ofFloat(child, "translationY", 0, 600);
                            fadeAnim.setDuration(200);
                            MyAnimator kickAnimator = new MyAnimator();
                            kickAnimator.setDuration(100);
                            fadeAnim.setEvaluator(kickAnimator);
                            fadeAnim.start();
                            fadeAnim.addListener(new Animator.AnimatorListener() {

                                @Override
                                public void onAnimationStart(Animator animation) {
                                }

                                @Override
                                public void onAnimationRepeat(Animator animation) {
                                }

                                @Override
                                public void onAnimationEnd(Animator animation) {
                                    child.setVisibility(View.INVISIBLE);
                                }

                                @Override
                                public void onAnimationCancel(Animator animation) {
                                }
                            });
                        }

                      /*  @Override
                        public void call(Long aLong) {
                        }*/
                    });

           Observable.timer((layout.getChildCount() - i) * 30 + 80, TimeUnit.MILLISECONDS)
                        .subscribeOn(Schedulers.newThread())
                        .observeOn(AndroidSchedulers.mainThread())
                        .subscribe(new Consumer() {
                            @Override
                            public void accept(Long aLong) throws Exception {
                                dismiss();
                            }

                           /* @Override
                            public void call(Long aLong) {

                            }*/
                        });
            }

    }
  @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.wimdow_zhibo_lay:
                startA(4);
                if (isShowing()) {
                    closeAnimation(contentView);
                }
                break;
            case R.id.wimdow_youji_lay:
                startA(2);
                if (isShowing()) {
                    closeAnimation(contentView);
                }
                break;
            case R.id.wimdow_bobao_lay:
                startA(1);
                if (isShowing()) {
                    closeAnimation(contentView);
                }
                break;
            case R.id.wimdow_tuwen_lay:
                startA(3);
                if (isShowing()) {
                    closeAnimation(contentView);
                }
                break;
            case R.id.contentView:
                if (isShowing()) {
                    closeAnimation(contentView);
                }
                break;
            case R.id.ll_close:
                if (isShowing()) {
                    closeAnimation(contentView);
                }
                break;
            default:
                break;
        }
    }
   private void startA(int i) {
        //mContext.startActivity(intent);
    }

 public static int  getBarHeight(Activity context){
        int result = 0;

        int resourceId = context.getResources().getIdentifier("notch_height", "dimen", "android");
        if (resourceId > 0) {
            result = context.getResources().getDimensionPixelSize(resourceId);
            if(result==0){
                Rect rect = new Rect();
                context.getWindow().getDecorView().getWindowVisibleDisplayFrame(rect);
                result = (int) Math.ceil(25 * context.getResources().getDisplayMetrics().density);
            }
        }else {
            Rect rect = new Rect();
            context.getWindow().getDecorView().getWindowVisibleDisplayFrame(rect);
            result = (int) Math.ceil(25 * context.getResources().getDisplayMetrics().density);
        }
        return result;
        //layout.setPadding(0, result, 0, 0);
    }
}

xml布局



    

        

        
    


    




    
    

        

        
    



    

        

        
    


    

        

        
    


    

        

        
    



    



定义一个动画工具类MyAnimator

public class MyAnimator implements TypeEvaluator {
    private final float s = 1.70158f;
    float mDuration = 0f;

    public void setDuration(float duration) {
        mDuration = duration;
    }

    public Float evaluate(float fraction, Float startValue, Float endValue) {
        float t = mDuration * fraction;
        float b = startValue.floatValue();
        float c = endValue.floatValue() - startValue.floatValue();
        float d = mDuration;
        float result = calculate(t, b, c, d);
        return result;
    }

    public Float calculate(float t, float b, float c, float d) {
        return c * ((t = t / d - 1) * t * ((s + 1) * t + s) + 1) + b;
    }
}

最后简单的使用一下

 final TextView textView = (TextView) findViewById(R.id.ssss);


        textView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                PublishPopWindow popWindow = new PublishPopWindow(SHHomeActivity.this);
                popWindow.showMoreWindow(textView);
            }
        });




    

Screenshot_20190222-110553.jpg
Screenshot_20190222-110549.jpg
mmexport1550760836263.jpg

你可能感兴趣的:(Android-仿微博自定义底部动态弹出的半透明PopupWindow)