【问题标题】:How to give animation to the ViewSwitcher如何为 ViewSwitcher 提供动画
【发布时间】:2022-05-01 03:35:08
【问题描述】:

我创建了一个由 ViewSwitcher 组成的广告控件....

在该控件中,我有 ImageView 和 TextView,因为广告是文本或图像..

现在我必须为广告添加动画效果..

我试过了

Animation 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.setInAnimation(inAnimation);

switcher.setOutAnimation(outAnimation);

但它不会工作..

请给我任何其他选择..或者如果使用上面的代码是错误的,那么如何使用它??

【问题讨论】:

    标签: android android-animation


    【解决方案1】:

    尝试将 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" >
    

    【讨论】:

    • 有没有什么办法可以像这样从上侧进出垂直动画??
    • @Rashmi:您可以使用"@android:anim/slide_in_up""@android:anim/slide_out_down"。如果您收到错误消息“资源未公开”,请检查stackoverflow.com/a/8019405/1097104
    • 如果您想旋转它 - 只需使用 android:rotation 将您的内容再嵌套一个布局并在相反方向使用 android:rotation
    【解决方案2】:

    除此之外:

    照顾 switcher.showNext();或 switcher.showPrevious();

    如果您为切换器设置动画,则两个动作都会产生相同的动画。

    【讨论】:

      【解决方案3】:

      什么都没有发生或者你有什么错误?你的意思是它不起作用?

      你是用switcher.showNext();还是switcher.showPrevious();开始动画

      希望它会有所帮助.. 干杯 ;)

      【讨论】:

      • 我使用了 switcher.showNext() 方法.. 但它不会像上面提到的那样滑入和滑出...
      【解决方案4】:

      我正在提供问题中提到的替代方案。使用它,您将实现与 ViewPager 相同的动画。

      您可以将 displayChild 设置为 1,而不是 showNext。

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

      您可以将 displayPrevious 设置为 0,而不是 showPrevious。

      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>
      

      每次设置displayChild,都需要设置动画。如果您不想要动画,您可以简单地忽略它。仅此而已。快乐编码!

      PS:Kotlin 代码

      【讨论】:

        【解决方案5】:

        一个“switcher.showNext();”从最后一个布局和一个“switcher.showPrevious();”从第一个布局给出错误。它必须类似于堆栈中的stackoverflow和stackunderflow情况。所以在调用“showNext()”之前检查它不是您当前所在的最后一个布局,并且在调用“showPrevious()”时您是否不在第一个布局中”。我遇到了这个简单的错误。 抱歉发了这个帖子,我(菜鸟)还没有被授权评论帖子

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2021-12-22
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多