我需要创建一个动画 - 翻转一个 View 并显示另一个。
当前显示的 View 的宽度慢慢减小到零,之后要显示的 View 的宽度必须从零开始增加。
在此期间,高度从当前显示的高度变为略微降低的高度并再次返回。
我怎样才能做到这一点...使用 ViewFlipper。
最佳答案
您可以通过在 ViewFlipper
上设置 ScaleAnimations
来做到这一点。我在没有第二个比例的情况下做类似的事情。我有两个动画,一个用于输出 View ,一个用于输入 View 。我将在此处发布它们作为您的起点。
shrink_to_middle.xml
<set xmlns:android="http://schemas.android.com/apk/res/android">
<scale
android:interpolator="@android:anim/linear_interpolator"
android:fromXScale="1.0"
android:toXScale="1.0"
android:fromYScale="1.0"
android:toYScale="0.0"
android:fillAfter="false"
android:duration="200" />
<translate
android:fromYDelta="0"
android:toYDelta="50%"
android:duration="200"/>
</set>
grow_from_middle.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="1.0"
android:fromYScale="0.0"
android:toYScale="1.0"
android:fillAfter="false"
android:startOffset="200"
android:duration="200" />
<translate
android:fromYDelta="50%"
android:toYDelta="0"
android:startOffset="200"
android:duration="200"/>
</set>
然后在应用程序中,我将它们设置为 ViewFlipper
,如下所示:
mViewFlipper.setInAnimation(context, R.anim.grow_from_middle);
mViewFlipper.setOutAnimation(context, R.anim.shrink_to_middle);
就像我说的,这与您所描述的不完全一样,但它非常接近并且可以帮助您入门。
--编辑--
这是使用 pivotX 和 pivotY 的代码(好吧,在我的例子中只是 pivotY):
shrink_to_middle.xml
<?xml version="1.0" encoding="utf-8"?>
<scale
xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/linear_interpolator"
android:fromXScale="1.0"
android:toXScale="1.0"
android:fromYScale="1.0"
android:toYScale="0.0"
android:pivotY="50%"
android:fillAfter="false"
android:duration="200" />
grow_from_middle.xml
<?xml version="1.0" encoding="utf-8"?>
<scale
xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/linear_interpolator"
android:fromXScale="1.0"
android:toXScale="1.0"
android:fromYScale="0.0"
android:toYScale="1.0"
android:pivotY="50%"
android:fillAfter="false"
android:startOffset="200"
android:duration="200" />
关于Android 动画 - 翻转,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3364010/