【问题标题】:How can I implement the flip transition between fragments in dialog fragment as well如何在对话框片段中实现片段之间的翻转过渡
【发布时间】:2018-04-09 18:51:31
【问题描述】:

我有两个对话框片段:

如何在对话框片段中实现相同的功能?

【问题讨论】:

标签: android android-dialogfragment android-transitions


【解决方案1】:

一个展示如何为对话框片段设置动画的示例

作为 DialogFragment 的 Dialog 类的包装器,您应该为基础 Dialog 设置一个主题以获得您想要的动画:

public class CustomDialogFragment extends DialogFragment implements OnEditorActionListener
{
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) 
    {
        return super.onCreateView(inflater, container, savedInstanceState);
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) 
    {
        // Set a theme on the dialog builder constructor!
        AlertDialog.Builder builder = 
            new AlertDialog.Builder( getActivity(), R.style.MyCustomTheme );

        builder  
        .setTitle( "Your title" )
        .setMessage( "Your message" )
        .setPositiveButton( "OK" , new DialogInterface.OnClickListener() 
            {      
              @Override
              public void onClick(DialogInterface dialog, int which) {
              dismiss();                  
            }
        });
        return builder.create();
    }
}

然后您只需要定义包含所需动画的主题即可。在 styles.xml 中添加您的自定义主题:

<style name="MyCustomTheme" parent="@android:style/Theme.Panel">
    <item name="android:windowAnimationStyle">@style/MyAnimation.Window</item>
</style>

<style name="MyAnimation.Window" parent="@android:style/Animation.Activity"> 
    <item name="android:windowEnterAnimation">@anim/anim_in</item>
    <item name="android:windowExitAnimation">@anim/anim_out</item>
</style> 

现在在 res/anim 文件夹中添加动画文件:

(android:pivotY 是关键)

anim_in.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <scale
        android:interpolator="@android:anim/linear_interpolator"
        android:fromXScale="0.0"
        android:toXScale="1.0"
        android:fromYScale="0.0"
        android:toYScale="1.0"
        android:fillAfter="false"
        android:startOffset="200"
        android:duration="200" 
        android:pivotX = "50%"
        android:pivotY = "-90%"
    />
    <translate
        android:fromYDelta="50%"
        android:toYDelta="0"
        android:startOffset="200"
        android:duration="200"
    />
</set>
anim_out.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <scale
        android:interpolator="@android:anim/linear_interpolator"
        android:fromXScale="1.0"
        android:toXScale="0.0"
        android:fromYScale="1.0"
        android:toYScale="0.0"
        android:fillAfter="false"
        android:duration="200" 
        android:pivotX = "50%"        
        android:pivotY = "-90%"        
    />
    <translate
        android:fromYDelta="0"
        android:toYDelta="50%"
        android:duration="200"
    />
</set>

最后,棘手的事情是让您的动画从每行的中心开始增长。我想该行水平填充屏幕,所以一方面 android:pivotX 值将是静态的。另一方面,您不能以编程方式修改 android:pivotY 值。

我的建议是,您定义几个动画,每个动画在 android:pivotY 属性上都有不同的百分比值(以及引用这些动画的几个主题)。然后,当用户点击该行时,以屏幕上该行的百分比计算 Y 位置。知道百分比位置后,为您的对话框分配一个具有适当 android:pivotY 值的主题。

这不是一个完美的解决方案,但可以为您解决问题。如果您不喜欢结果,那么我建议您忘记 DialogFragment 并为从行的确切中心增长的简单视图设置动画。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-06-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-31
    • 1970-01-01
    • 2013-07-31
    相关资源
    最近更新 更多