问题描述
我有一个DialogFragment
,并在onActivityCreated
方法中为进入/退出设置了动画,如下所示:
I have a DialogFragment
and I set animation for enter/exit in the onActivityCreated
method as below:
@Override
public void onActivityCreated(Bundle arg0) {
super.onActivityCreated(arg0);
getDialog().getWindow()
.getAttributes().windowAnimations = R.style.DialogAnimation;
}
我的DialogAnimation
样式文件如下:
<style name="DialogAnimation">
<item name="android:windowEnterAnimation">@android:anim/fade_in</item>
<item name="android:windowExitAnimation">@android:anim/fade_out</item>
</style>
这现在对我有用...
This works for me now...
现在我的问题是我希望有两种不同的退出动画,一种用于单击确定"按钮时,另一种用于取消按钮.所以我所做的是我在撤职前尝试更改过渡,但没有成功.
Now my problem is i want to have two different exit animation one for when the "OK" button is clicked and one for the cancel button. So what I did was I tried changing the transition just before dismissing but it didn't work.
关于如何实现它的任何解决方案?
Any solution on how it can be achieved?
这是我尝试过的:
@Override
public void onClick(View v) {
getDialog().getWindow()
.getAttributes().windowAnimations = R.style.DialogAnimation2;
this.dismiss();
}
推荐答案
您可以在DialogFragment中完成它,而无需更改
You can do it inside your DialogFragment, without changing
getDialog().getWindow()
.getAttributes().windowAnimations
您应为装饰视图"动画,在onStart和onClick中.
You should animate "decor view" in onStart and onClick.
这是代码片段:
首先创建对话框
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
return new AlertDialog.Builder(getActivity())
.setTitle("Hello from animated dialog :)")
.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
//we have to add button here and then override it's click in onStart
}
}
)
.setCancelable(false)
.create();
}
然后覆盖onStart方法
@Override
public void onStart() {
super.onStart();
AlertDialog dialog = (AlertDialog)getDialog();
final View decorView = getDialog()
.getWindow()
.getDecorView();
ObjectAnimator scaleDown = ObjectAnimator.ofPropertyValuesHolder(decorView,
PropertyValuesHolder.ofFloat("scaleX", 0.0f, 1.0f),
PropertyValuesHolder.ofFloat("scaleY", 0.0f, 1.0f),
PropertyValuesHolder.ofFloat("alpha", 0.0f, 1.0f));
scaleDown.setDuration(2000);
scaleDown.start();
Button positiveButton = dialog.getButton(Dialog.BUTTON_NEGATIVE);
positiveButton.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
final View decorView = getDialog()
.getWindow()
.getDecorView();
ObjectAnimator scaleDown = ObjectAnimator.ofPropertyValuesHolder(decorView,
PropertyValuesHolder.ofFloat("scaleX", 1.0f, 0.0f),
PropertyValuesHolder.ofFloat("scaleY", 1.0f, 0.0f),
PropertyValuesHolder.ofFloat("alpha", 1.0f, 0.0f));
scaleDown.addListener(new Animator.AnimatorListener() {
@Override
public void onAnimationEnd(Animator animation) {
dismiss();
}
@Override
public void onAnimationStart(Animator animation) {
}
@Override
public void onAnimationCancel(Animator animation) {
}
@Override
public void onAnimationRepeat(Animator animation) {
}
});
scaleDown.setDuration(2000);
scaleDown.start();
}
});
}
这是结果动画
如果您从我的代码中删除比例属性,则只会获得alpha动画.正是您想要的.
And if you remove scale properties from my code you will get only alpha animation. Exactly as you wanted.
删除此:
PropertyValuesHolder.ofFloat("scaleX", 1.0f, 0.0f),
PropertyValuesHolder.ofFloat("scaleY", 1.0f, 0.0f),
这篇关于在关闭前更改DialogFragment的进入/退出过渡的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!