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<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
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 221,273评论 6 515
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 94,349评论 3 398
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 167,709评论 0 360
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 59,520评论 1 296
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 68,515评论 6 397
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 52,158评论 1 308
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,755评论 3 421
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,660评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 46,203评论 1 319
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 38,287评论 3 340
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 40,427评论 1 352
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 36,122评论 5 349
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,801评论 3 333
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 32,272评论 0 23
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 33,393评论 1 272
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,808评论 3 376
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 45,440评论 2 359