封装使用PopupWindow弹出窗口(二),实现渐变阴影效果

引言

  上一期我们使用PopupWindow实现了简单的封装使用,本期就其封装类进一步添加渐变阴影效果。实现点击呼出弹出框,除弹出框外,整个activity的布局逐渐变暗,关闭弹出框,布局逐渐恢复原色。


介绍

  PopupWindow用来做弹出框并不是独一份,一般系统使用的是另一种,我们很多软件的消息通知其实都是用AlertDialog,但PopupWindow好就好在它足够灵活,可以适应很多自定义的弹出框环境。


传送门

  第一篇:简单封装使用PopupWindow
  第二篇:封装使用PopupWindow渐变阴影效果

本期效果预览

PopupWindow第二期.jpg

用法

第一步:布局文件

(1)主布局:activity_case22.xml(有改动)

说明:我简单添加了一个居中的按钮点击事件来处理弹出popupwindow。

改动:对控件背景色进行了调整。

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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:id="@+id/case22_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".show.Case22"
    android:background="@color/blue"
    tools:ignore="MissingConstraints">

        <androidx.appcompat.widget.Toolbar
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:titleTextColor="@color/white"
            android:background="@color/green"
            app:title="PopupWindow使用" />

        <Button
            android:id="@+id/show_popupWindow"
            android:layout_width="match_parent"
            android:textAllCaps="false"
            android:layout_height="wrap_content"
            android:text="弹出PopupWindow"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintBottom_toBottomOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

(2)子布局:popup_select_layout.xml(有改动)

说明:这里是比较常用的弹出框,拍照+相册+取消,View用作分割线。

改动:对控件背景和分割线颜色进行了调整。

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    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"
    tools:ignore="MissingConstraints">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/white"
        app:layout_constraintBottom_toBottomOf="parent">

        <Button
            android:id="@+id/take_photo"
            android:layout_width="match_parent"
            android:layout_height="40dp"
            android:background="@color/gray"
            android:gravity="center"
            android:text="拍照"
            android:textColor="@color/black"
            android:textSize="20sp" />

        <View
            android:id="@+id/divide1"
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:background="@color/blue"
            app:layout_constraintTop_toBottomOf="@id/take_photo" />

        <Button
            android:id="@+id/picture"
            android:layout_width="match_parent"
            android:layout_height="40dp"
            android:background="@color/gray"
            android:gravity="center"
            android:text="相册"
            android:textColor="@color/black"
            android:textSize="20sp"
            app:layout_constraintTop_toBottomOf="@id/divide1" />

        <View
            android:id="@+id/divide2"
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:background="@color/blue"
            app:layout_constraintTop_toBottomOf="@id/picture" />

        <Button
            android:id="@+id/cancel"
            android:layout_width="match_parent"
            android:layout_height="40dp"
            android:background="@color/gray"
            android:gravity="center"
            android:text="取消"
            android:textColor="@color/black"
            android:textSize="20sp"
            app:layout_constraintTop_toBottomOf="@id/divide2" />

    </androidx.constraintlayout.widget.ConstraintLayout>
</androidx.constraintlayout.widget.ConstraintLayout>

第二步:创建TestPopupWindow类(有改动)

继承自PopupWindow类,并且重写其方法。

改动:对比前期增加了弹出框后增加渐变阴影的效果。呼出渐变出现阴影效果,点击消失渐变消失阴影效果。

/**
 * @data on 2020/9/14 9:18 AM
 * @auther
 * @describe
 */
public class TestPopupWindow extends PopupWindow {
    TestDataListener mTestDataListener;
    Context mContext;
    int type = 0;


    public TestPopupWindow(Context context) {
        this.mContext = context;
        View contentView = LayoutInflater.from(context).inflate(R.layout.popup_select_layout, null);
        //封装成CommonPopupWindow类
        setContentView(contentView);
        setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        setFocusable(true);
        setBackgroundDrawable(new BitmapDrawable());
        setOutsideTouchable(true);
        // 收起时渐进恢复背景,从半透明-
        setOnDismissListener(new OnDismissListener() {
            @Override
            public void onDismiss() {
                ValueAnimator animator = ValueAnimator.ofFloat(0.5f, 1f).setDuration(500);
                animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                    @Override
                    public void onAnimationUpdate(ValueAnimator animation) {
                        setBackgroundAlpha((Float) animation.getAnimatedValue());
                    }
                });
                animator.start();
            }
        });
        show(contentView);

        //初始化控件
        TextView takePhoto = (TextView)contentView.findViewById(R.id.take_photo);
        TextView picture = (TextView)contentView.findViewById(R.id.picture);
        TextView cancel = (TextView)contentView.findViewById(R.id.cancel);

        takePhoto.setOnClickListener((View)->{
            Log.i("TestPopupWindow: ","拍照");
            type = 0;
            if(mTestDataListener != null){
                mTestDataListener.getTestData(type);
            }
            dismiss();
        });

        picture.setOnClickListener((View)->{
            Log.i("TestPopupWindow: ","相册");
            type = 1;
            if(mTestDataListener != null){
                mTestDataListener.getTestData(type);
            }
            dismiss();
        });
        cancel.setOnClickListener((View)->{
            Log.i("TestPopupWindow: ","取消");
            type = 2;
            if(mTestDataListener != null){
                mTestDataListener.getTestData(type);
            }
            dismiss();
        });
    }

    //回调事件接口
    public interface TestDataListener {
        void getTestData(int type);
    }

    public void setTestClickaListener(TestDataListener testDataListener) {
        mTestDataListener = testDataListener;
    }

    /**
     * 设置PopupWindow下面的内容的透明度
     */
    private void setBackgroundAlpha(float bgAlpha) {
        WindowManager.LayoutParams lp = ((Activity) mContext).getWindow().getAttributes();
        lp.alpha = bgAlpha;
        ((Activity) mContext).getWindow().setAttributes(lp);
    }
    /**
     * 显示弹窗,过程中伴随背景逐渐变暗。
     * 不要改变MaskLayout的背景色,否则会是灰色背景的框框整体向上升起的效果。
     */
    public void show(View parent) {
        ValueAnimator animator = ValueAnimator.ofFloat(1, 0.5f).setDuration(500);
        animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                setBackgroundAlpha((Float) animation.getAnimatedValue());
            }
        });
        animator.start();
    }

}

第三步:在Activity中使用

public class Case22 extends AppCompatActivity {
    TestPopupWindow popupWindow;
    Button btnShowPopupWindow;

    @SuppressLint("ResourceType")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_case22);
        ConstraintLayout case22Layout = findViewById(R.id.case22_layout); //主布局设置弹出窗口定位使用
        btnShowPopupWindow = findViewById(R.id.show_popupWindow);
        showPopupWindow(case22Layout);

    }
    //展示弹出窗口
    private void showPopupWindow(View parent){
        btnShowPopupWindow.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                popupWindow = new TestPopupWindow(getApplicationContext());
                popupWindow.showAtLocation(parent, Gravity.BOTTOM|Gravity.CENTER_HORIZONTAL, 0, 0);//在activity的底部展示。
//            popupWindow.showAsDropDown(binding.showPopupWindow); //在某个控件下方弹出Popupwindow
            popupWindow.setTestClickaListener(new TestPopupWindow.TestDataListener() {
                @Override
                public void getTestData(int type) {
                    Log.d("test: ", String.valueOf(type)); //测试
                    switch (type){
                        case 0:  //拍照
                            Toast.makeText(getBaseContext(),"拍照",Toast.LENGTH_SHORT).show();
                            break;
                        case 1:  //相册
                            Toast.makeText(getBaseContext(),"相册",Toast.LENGTH_SHORT).show();
                            break;
                        case 2:  //取消
                            Toast.makeText(getBaseContext(),"取消",Toast.LENGTH_SHORT).show();
                            break;
                    }
                }
            });
            }
        });
    }
}

大功告成!


千夜零一:“之前总是看各种博客学习东西,现在我想用博客记录下我的学习脚步,好东西也需要分享,索取和给予是相互的。以后会尽量日更的!目标完成1001篇博客哈哈。”
  如果觉得对你有所帮助,请不要吝啬你的点赞,有问题也可以在下方评论区留言哦,关注我一起学习吧~

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