如何为ViewSwitcher添加动画效果

17
我创建了一个广告控件,其中包括ViewSwitcher...
在该控件中,我有ImageView和TextView,因为广告要么是文本,要么是图像。
现在我必须给广告添加动画效果...
我尝试过以下方法
inAnimation = AnimationUtils.loadAnimation(this,android.R.anim.slide_in_left); inAnimation.setDuration(1500);
outAnimation = AnimationUtils.loadAnimation(this,android.R.anim.slide_out_right); outAnimation.setDuration(1500);
然后将其设置为开关器
ViewSwitcher switcher; switcher.setInAnimation(inAnimation); switcher.setOutAnimation(outAnimation);
但它不起作用。
请给我任何其他替代方案。或者如果使用上述代码错误,则如何使用它?
5个回答

34

尝试在xml中设置动画,例如

<ViewSwitcher
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:inAnimation="@android:anim/slide_in_left"
    android:outAnimation="@android:anim/slide_out_right" >

有没有办法实现垂直动画,就像从上面进入并从下面退出一样? - NullPointerException
5
你可以使用"@android:anim/slide_in_up""@android:anim/slide_out_down"。如果你收到错误信息“Resource is not public”,请参考https://dev59.com/Gmsz5IYBdhLWcg3wQFi2#8019405。 - Juuso Ohtonen
如果您想要旋转它 - 只需使用 android:rotation,再将您的内容嵌套在更低一层的布局中,并使用相反方向的 android:rotation - Aiden Strydom

9
此外:
请注意 switcher.showNext(); 或 switcher.showPrevious();。
如果您对切换器设置了动画,这两个操作将会得到相同的动画效果。

3

根据问题,我提供了一种替代方案。使用此方法,您可以实现与ViewPager相同的动画效果。

不要使用showNext,而是将displayedChild设置为1。

switcherView?.inAnimation = AnimationUtils.loadAnimation(baseActivity, R.anim.slide_in_right)
switcherView?.outAnimation = AnimationUtils.loadAnimation(baseActivity, R.anim.slide_out_left)
switcherView?.displayedChild = 1

代替showPrevious,您可以将displayedChild设置为0。

switcherView?.inAnimation = AnimationUtils.loadAnimation(baseActivity, R.anim.slide_in_left)
switcherView?.outAnimation = AnimationUtils.loadAnimation(baseActivity, R.anim.slide_out_right)
switcherView?.displayedChild = 0

动画文件: R.anim.slide_in_right:

<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate
        android:duration="@android:integer/config_shortAnimTime"
        android:fromXDelta="100%p"
        android:toXDelta="0" />
</set>

R.anim.slide_out_left

<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate
        android:duration="@android:integer/config_shortAnimTime"
        android:fromXDelta="0"
        android:toXDelta="-100%p" />
</set>

R.anim.slide_in_left

<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate
        android:duration="@android:integer/config_shortAnimTime"
        android:fromXDelta="-100%p"
        android:toXDelta="0" />
</set>

R.anim.slide_out_right

<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate
        android:duration="@android:integer/config_shortAnimTime"
        android:fromXDelta="0"
        android:toXDelta="100%p" />
</set>

每当你设置displayedChild时,都需要设置动画。如果不想要动画,可以简单地忽略它。 就这些。编码愉快! PS:Kotlin代码

2

没有任何反应或者出现了一些错误?你说的“它不起作用”是什么意思?

你是使用 switcher.showNext(); 还是 switcher.showPrevious(); 来启动动画的?

希望这能帮到你,加油! ;)


我已经使用了 switcher.showNext() 方法,但它不会像所述的那样滑入和滑出。 - NullPointerException

0
从最后一个布局切换到下一个布局使用了"switcher.showNext();",而从第一个布局切换到上一个布局使用了"switcher.showPrevious();"出现了错误。这就像栈中的stackoverflow和stackunderflow情况一样,在调用"showNext()"之前,必须检查当前是否处于最后一个布局,并且在调用"showPrevious()"时不要处于第一个布局。我犯了这个简单的错误。对不起,我还没有权限在帖子上发表评论。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接