问题描述
当用户点击 ListView
中的一行时,我会显示一个 DialogFragment
.我想为对话框的显示设置动画,使其从行的中心增长.从启动器打开文件夹时可以看到类似的效果.
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
的 Dialog
类的包装器,你应该为你的基础 设置一个主题>Dialog
获取你想要的动画:
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 中添加您的自定义主题:
Then you just need to define the theme that will include your desired animation. In styles.xml add your custom theme:
<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 文件夹中添加动画文件:
Now add the animation files in the res/anim folder:
(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
值.
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 位置.了解百分比位置,为您的对话框分配一个具有适当 android: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
并动画一个简单的 View
从行的确切中心增长.
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.
祝你好运!
这篇关于显示 DialogFragment 动画从一个点开始的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!