如何向DialogFragment添加动画?

时间:2013-02-25 09:34:07

标签: android animation

如何向DialogFragment添加动画。我的动画是:

out anim:

<scale
    android:duration="200"
    android:fillAfter="false"
    android:fromXScale="1.0"
    android:fromYScale="1.0"
    android:interpolator="@android:anim/linear_interpolator"
    android:pivotX="50%"
    android:pivotY="-90%"
    android:startOffset="200"
    android:toXScale="0.5"
    android:toYScale="0.5" />

<translate
    android:duration="300"
    android:fromXDelta="0"
    android:fromYDelta="0"
    android:toXDelta="-200"
    android:toYDelta="-200" />

in anim:

<scale
    android:duration="200"
    android:fillAfter="false"
    android:fromXScale="0.5"
    android:fromYScale="0.5"
    android:interpolator="@android:anim/linear_interpolator"
    android:pivotX="50%"
    android:pivotY="-90%"
    android:toXScale="1.0"
    android:toYScale="1.0" />

<translate
    android:duration="300"
    android:fromXDelta="-200"
    android:fromYDelta="-200"
    android:toXDelta="0"
    android:toYDelta="0" />

和我的代码:

FragmentTransaction ft = getFragmentManager().beginTransaction();
                ft.setCustomAnimations(R.anim.jump_in, R.anim.jump_out, R.anim.jump_in, R.anim.jump_out);
                ft.add(layer_frag, "layer frag");
                ft.show(layer_frag).commit();//layer_frag is a class whitch extends DialogFragment

我必须错过一些东西,因为它看起来像以前一样。

1 个答案:

答案 0 :(得分:19)

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) 
{
    final Dialog dialog = super.onCreateDialog(savedInstanceState);
    dialog.getWindow().getAttributes().windowAnimations = R.style.detailDialogAnimation;
    return dialog;
}

答案来自stackoverflow.com/a/13537234/969325,但您必须将样式设置为onCreateDialog函数。