美文网首页动画
Android-仿微博自定义底部动态弹出的半透明PopupWin

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

作者: 三千世界_ | 来源:发表于2019-02-22 10:09 被阅读0次

    首先定义一个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<Long>() {
                            @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<Long>() {
                            @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<Long>() {
                                @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布局

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/contentView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:alpha="9"
        android:background="@color/ffffff"
        android:gravity="center_horizontal">
    
        <RelativeLayout
            android:id="@+id/ll_close"
            android:layout_width="match_parent"
            android:layout_height="@dimen/dp_60"
            android:layout_alignParentBottom="true"
            android:background="@android:color/white"
            android:orientation="vertical">
    
            <View
                android:layout_width="match_parent"
                android:layout_height="@dimen/dp_0_5"
                android:background="#d7d7d7" />
    
            <ImageView
                android:layout_width="@dimen/dp_35"
                android:layout_height="@dimen/dp_35"
                android:layout_centerInParent="true"
                android:layout_gravity="center"
                android:padding="@dimen/dp_10"
                android:scaleType="fitXY"
                android:src="@mipmap/ic_launcher" />
        </RelativeLayout>
    
    
        <LinearLayout
            android:id="@+id/weblink_window"
            android:layout_width="@dimen/dp_20"
            android:layout_height="@dimen/dp_10"
            android:layout_above="@+id/ll_close"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="@dimen/dp_20"
            android:gravity="center"
            android:orientation="vertical"></LinearLayout>
    
    
    
    
        <!--        -->
        <LinearLayout
            android:id="@+id/wimdow_tuwen_lay"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_above="@+id/weblink_window"
            android:layout_weight="1"
            android:gravity="center"
            android:orientation="vertical">
    
            <ImageView
                android:layout_width="@dimen/dp_54"
                android:layout_height="@dimen/dp_54"
                android:src="@mipmap/ic_launcher" />
    
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="@dimen/dp_10"
                android:text="标题1"
                android:textColor="@color/c_333"
                android:textSize="@dimen/sp_13" />
        </LinearLayout>
    
    
    
        <LinearLayout
            android:layout_toRightOf="@id/wimdow_tuwen_lay"
            android:id="@+id/wimdow_zhibo_lay"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_above="@+id/weblink_window"
            android:layout_weight="1"
            android:gravity="center"
            android:orientation="vertical">
    
            <ImageView
                android:layout_width="@dimen/dp_54"
                android:layout_height="@dimen/dp_54"
                android:src="@mipmap/ic_launcher" />
    
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="@dimen/dp_10"
                android:text="标题1"
                android:textColor="@color/c_333"
                android:textSize="@dimen/sp_13" />
        </LinearLayout>
    
    
        <LinearLayout
            android:id="@+id/wimdow_youji_lay"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_above="@+id/weblink_window"
            android:layout_toRightOf="@id/wimdow_zhibo_lay"
            android:layout_weight="1"
            android:gravity="center"
            android:orientation="vertical">
    
            <ImageView
                android:layout_width="@dimen/dp_54"
                android:layout_height="@dimen/dp_54"
                android:src="@mipmap/ic_launcher" />
    
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="@dimen/dp_10"
                android:text="标题1"
                android:textColor="@color/c_333"
                android:textSize="@dimen/sp_13" />
        </LinearLayout>
    
    
        <LinearLayout
            android:id="@+id/wimdow_bobao_lay"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_above="@+id/weblink_window"
            android:layout_toRightOf="@+id/wimdow_youji_lay"
            android:layout_weight="1"
            android:gravity="center"
            android:orientation="vertical">
    
            <ImageView
                android:layout_width="@dimen/dp_54"
                android:layout_height="@dimen/dp_54"
                android:src="@mipmap/ic_launcher" />
    
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="@dimen/dp_10"
                android:text="标题1"
                android:textColor="@color/c_333"
                android:textSize="@dimen/sp_13" />
        </LinearLayout>
    
    
    
        <!--
            </RelativeLayout>
        -->
    
    
    </RelativeLayout>
    

    定义一个动画工具类MyAnimator

    public class MyAnimator implements TypeEvaluator<Float> {
        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);
                }
            });
    
    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        xmlns:suspend="http://schemas.android.com/apk/res-auto">
    
        <TextView
            android:id="@+id/ssss"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:padding="@dimen/dp_20"
            android:layout_centerHorizontal="true"
            android:text="sssssss"/>
    </RelativeLayout>
    
    Screenshot_20190222-110553.jpg Screenshot_20190222-110549.jpg mmexport1550760836263.jpg

    相关文章

      网友评论

        本文标题:Android-仿微博自定义底部动态弹出的半透明PopupWin

        本文链接:https://www.haomeiwen.com/subject/szesyqtx.html