从左往右无限循环的滑动动画

3
我在我的RelativeLayout中有一个ImageView,我的要求是:
我想从左到右滑动ImageView并淡出,然后再从左侧淡入向右滑动并淡出。
 ----                           ----
|    |   swipe left to right   |    |  Fade out here
|    |   --> ---> --->         |    |
|    |                         |    |
 ----                           ----

从左侧淡入并重复:
 ----                           ----
|    |   swipe left to right   |    |  Fade out here
|    |   --> ---> --->         |    |
|    |                         |    |
 ----                           ----

这个问题在Stack上没有匹配的其他问题,所以请不要试图标记它。

据我所知,我可以使用以下内容:

这是用于从左到右动画的代码:

<set xmlns:android="http://schemas.android.com/apk/res/android"
     android:shareInterpolator="false">
  <translate android:fromXDelta="-100%" android:toXDelta="0%"
             android:fromYDelta="0%" android:toYDelta="0%"
             android:duration="700"/>
</set>

对于淡入淡出效果:

fadein.xml

    <?xml version="1.0" encoding="UTF-8"?>
       <set xmlns:android="http://schemas.android.com/apk/res/android">
         <alpha android:fromAlpha="0.0" android:toAlpha="1.0" 
          android:interpolator="@android:anim/accelerate_interpolator" 
          android:duration="2000"/>
     </set>
fadeout.xml

    <?xml version="1.0" encoding="UTF-8"?>
       <set xmlns:android="http://schemas.android.com/apk/res/android">
         <alpha android:fromAlpha="1.0" android:toAlpha="0.0" 
          android:interpolator="@android:anim/accelerate_interpolator" 
          android:duration="2000"/>
     </set>

但问题在于如何在将ImageView从右侧淡出后将其定位回相同位置,再从左侧回到原处并重复此过程。


1
我认为你可以通过编程来实现这个,或者甚至可以在它向右淡出后立即应用一个新的-100%的翻译来完成。 - cYrixmorten
1个回答

1
你可以给两个动画都添加一个动画监听器
在淡出动画的onAnimationEnd()中,将ImageView的可见性设置为GONE。然后,开始淡入动画,在那个动画的onAnimationStart()中将ImageView的可见性设置为VISIBLE
你不需要设置图像的位置,只需让动画自己完成即可。
编辑:
除了淡入淡出之外,还需要向你的动画添加一个平移组件:
fadein.xml

<?xml version="1.0" encoding="UTF-8"?>
   <set xmlns:android="http://schemas.android.com/apk/res/android" 
      android:interpolator="@android:anim/accelerate_interpolator">
     <alpha android:fromAlpha="0.0" android:toAlpha="1.0" 
      android:duration="2000" />
     <translate android:fromXDelta="-100%" android:toXDelta="0%"
      android:duration="700" />
 </set>

fadeout.xml

<?xml version="1.0" encoding="UTF-8"?>
   <set xmlns:android="http://schemas.android.com/apk/res/android" 
      android:interpolator="@android:anim/accelerate_interpolator" >
     <alpha android:fromAlpha="1.0" android:toAlpha="0.0" 
      android:duration="2000"/>
     <translate android:fromXDelta="0%" android:toXDelta="100%" 
      android:duration="700"/>
 </set>

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