使用FragmentDialog自定义底部弹出Dialog,动画无效的问题

最近在自定义Dialog时,发现网上提供的很多方案,最多的是FragmentDialog。

比如我们想要实现一个底部弹出的功能,虽然Google已推出官方版的BottomSheet,但是使用起来会发现点击outsize区域功能没有做,用起来很不方便,我们还是使用FragmentDialog自己实现吧

直接上代码
首先是dialog布局文件pip_dialog_select

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ffffff"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:orientation="horizontal">
        <Button
            android:id="@+id/btn_from_camera"
            android:layout_width="0dp"
            android:layout_weight="0.5"
            android:layout_height="match_parent"
            android:text="相机"/>
        <Button
            android:id="@+id/btn_from_gallery"
            android:layout_width="0dp"
            android:layout_weight="0.5"
            android:layout_height="match_parent"
            android:text="相册"/>
    </LinearLayout>


</LinearLayout>

FragmentDialog文件

public class BottomSelectDialog extends DialogFragment implements View.OnClickListener {
    private  View masker;
    private  LinearLayout layoutBottom;
    private SelectFromListener selectFromListener;


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        //去掉dialog的标题,需要在setContentView()之前
        this.getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);
        Window window = this.getDialog().getWindow();
        //去掉dialog默认的padding
        window.getDecorView().setPadding(0, 0, 0, 0);
        WindowManager.LayoutParams lp = window.getAttributes();
        lp.width = WindowManager.LayoutParams.MATCH_PARENT;
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
        //设置dialog的位置在底部
        lp.gravity = Gravity.BOTTOM;
        //设置dialog的动画
        lp.windowAnimations = R.style.BottomDialog;
        window.setAttributes(lp);
        window.setBackgroundDrawable(new ColorDrawable());

        final View view = inflater.inflate(R.layout.pip_dialog_select, null);
        view.findViewById(R.id.btn_from_camera).setOnClickListener(this);
        view.findViewById(R.id.btn_from_gallery).setOnClickListener(this);

        return view;
    }
}

定义Dialog的主题,values/styls.xml文件

 <!-- 带动画的底部弹出dialog-->
    <style name="BottomDialog" parent="android:Animation">
        <item name="android:windowAnimationStyle">@style/BottomDialogAnimation</item>
    </style>
    <style name="BottomDialogAnimation">
        <item name="android:windowEnterAnimation">@anim/dialog_slide_up</item>
        <item name="android:windowExitAnimation">@anim/dialog_slide_down</item>
    </style>

消失动画文件res/anim/dialog_slid_down.xnl

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
    android:duration="@android:integer/config_mediumAnimTime"
    android:interpolator="@android:anim/accelerate_interpolator"
    android:fromYDelta="0"
    android:toYDelta="100%p"/>
</set>

出现动画文件res/anim/dialog_slid_up.xnl

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate
        android:duration="@android:integer/config_mediumAnimTime"
        android:interpolator="@android:anim/accelerate_interpolator"
        android:fromYDelta="100%p"
        android:toYDelta="0"/>
</set>

参照网上的写法,基本上是这样,但是会出现一个问题,如下:

1111.gif

发现弹出动画根本没起作用

查询资料后发现Animation not working the in custom dialog

尝试修改lp.windowAnimations引用动画为不引用Theme,直接引用Animation

修改前
lp.windowAnimations = R.style.BottomDialog
修改后
 lp.windowAnimations = R.style.BottomDialogAnimation;
2222.gif

完美!
原因呢?待后续思考。。。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容