DialogFragment 出场动画

DialogFragment 其实就是继承fragment,用Fragment的事物进行管理
首先在onCreate中指定DialogFragment 所对应的样式

setStyle(DialogFragment.STYLE_NO_FRAME, R.style.DialogStyle);

    <style name="DialogStyle" parent="@android:style/Theme.Dialog">
        <item name="android:windowFrame">@null</item>
        <item name="android:windowIsFloating">true</item>
        <item name="android:windowIsTranslucent">true</item>
        <item name="android:windowNoTitle">true</item>
        <item name="android:windowBackground">@color/white</item>
    </style>

然后在onCreateView 中返回的是我们将要展示的布局

 rootView = View.inflate(getContext(), R.layout.dialog_fragment,null);

        final ViewTreeObserver viewTreeObserver = rootView.getViewTreeObserver();
        boolean alive = viewTreeObserver.isAlive();
 //        当一个视图树将要绘制时,所要调用的回调函数的接口类
 // 增加监听是为了,加入DialogFragment的出场动画
            viewTreeObserver.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
                @Override
                public boolean onPreDraw() {
//                    this 就表示的这个接口的匿名实现类
                    rootView.getViewTreeObserver().removeOnPreDrawListener(this);
                    System.out.println("MyDialogFragment onPreDraw 33333333333");
                    if (circleAnimal != null){
                        circleAnimal.show(rootView);
                    }
                    return true;
                }
            });

在onstart方法中设置DialogFragment的宽高,然后设置初始化动画对象

   @Override
    public void onStart() {
        super.onStart();
        System.out.println("MyDialogFragment onStart 44444444444444");
        ViewGroup.LayoutParams params = getDialog().getWindow().getAttributes();
  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
DialogFragment进出动画可以通过在DialogFragment的onCreateDialog方法中设置Dialog的Window动画来实现。具体步骤如下: 1. 在DialogFragment中重写onCreateDialog方法,并创建一个AlertDialog或其他类型的Dialog对象。 ```java @Override public Dialog onCreateDialog(Bundle savedInstanceState) { AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); // 设置对话框的内容、标题等 // 获取Dialog对象 Dialog dialog = builder.create(); // 设置进出动画 dialog.getWindow().getAttributes().windowAnimations = R.style.DialogAnimation; return dialog; } ``` 2. 在res目录下创建anim文件夹,并在其中定义进出动画的xml文件。例如,创建dialog_enter.xml和dialog_exit.xml。 ```xml <!-- dialog_enter.xml --> <set xmlns:android="http://schemas.android.com/apk/res/android"> <scale android:fromXScale="0.0" android:fromYScale="0.0" android:toXScale="1.0" android:toYScale="1.0" android:duration="300" /> </set> <!-- dialog_exit.xml --> <set xmlns:android="http://schemas.android.com/apk/res/android"> <scale android:fromXScale="1.0" android:fromYScale="1.0" android:toXScale="0.0" android:toYScale="0.0" android:duration="300" /> </set> ``` 3. 在styles.xml文件中定义Dialog的进出动画样式。 ```xml <style name="DialogAnimation"> <item name="android:windowEnterAnimation">@anim/dialog_enter</item> <item name="android:windowExitAnimation">@anim/dialog_exit</item> </style> ``` 4. 在DialogFragment所在的Activity中使用DialogFragment的show方法显示对话框。 ```java MyDialogFragment dialogFragment = new MyDialogFragment(); dialogFragment.show(getSupportFragmentManager(), "dialog"); ``` 这样就可以实现DialogFragment的进出动画了。你可以根据自己的需求在anim文件夹中定义不同的动画效果,并在DialogFragment的onCreateDialog方法中设置不同的动画样式。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值