显示DialogFragment动画从一个点不断增加动画、DialogFragment

2023-09-13 23:38:26 作者:我年轻丶心不定

我展示一个 DialogFragment 当用户在的ListView水龙头一排。我想,使其从该行的中心增长到动画对话框的表现。类似的效果可从启动打开一个文件夹时可以看到。

I'm showing a DialogFragment when the user taps on a row in a ListView. I'd like to animate the showing of the dialog so that it grows from the center of the row. A similar effect can be seen when opening a folder from the launcher.

这是我有一个想法是 TranslateAnimation 的组合和 ScaleAnimation 。有另一种方式?

One idea that I've had is a combination of TranslateAnimation and ScaleAnimation. Is there another way?

推荐答案

存在 DialogFragment 的包装器对话框类,你应该设定一个主题,你的基地对话框来得到你想要的动画:

Being DialogFragment a wrapper for the Dialog class, you should set a theme to your base Dialog to get the animation you want:

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 /阿尼姆添加动画文件文件夹:

(即安卓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>

最后,这里的棘手的事情是让每一行的中心动画增长。我想该行被占满屏幕水平等等,一方面安卓pivotX 值将是静态的。在另一方面,你不能修改安卓pivotY 值编程。

Finally, the tricky thing here is to get your animation grow from the center of each row. I suppose the row is filling the screen horizontally so, on one hand the android:pivotX value will be static. On the other hand, you can't modify the android:pivotY value programmatically.

我的建议是,你定义几个动画每一个有关于 Android的一个不同的百分比值:pivotY 属性(和几个主题引用这些动画)。然后,当用户敲击行,计算在屏幕上的行的百分比的Y位置。了解百分比的位置,指定一个主题,你的对话具有相应机器人:pivotY

What I suggest is, you define several animations each of which having a different percentage value on the android:pivotY attribute (and several themes referencing those animations). Then, when the user taps the row, calculate the Y position in percentage of the row on the screen. Knowing the position in percentage, assign a theme to your dialog that has the appropriate android:pivotY value.

这不是一个完美的解决方案,但可以做的伎俩为您服务。如果你不喜欢的结果,那么我建议忘记 DialogFragment 和动画的简单查看从生长该行的正中心。

It is not a perfect solution but could do the trick for you. If you don't like the result, then I would suggest forgetting the DialogFragment and animating a simple View growing from the exact center of the row.

祝你好运!