【问题标题】:Repeat android animation重复安卓动画
【发布时间】:2013-02-07 21:43:25
【问题描述】:
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.vitesse);

    gpsManager = new GPSManager();

    gpsManager.startListening(getApplicationContext());
    gpsManager.setGPSCallback(this);
    Typeface tf = Typeface.createFromAsset(getAssets(),
            "font/DS-DIGI.TTF");
     TextView  loding =(TextView)findViewById(R.id.info_message);
            loding.setTypeface(tf);
            AlphaAnimation fadeIn = new AlphaAnimation(0.0f , 1.0f ) ; 
            AlphaAnimation fadeOut = new AlphaAnimation( 1.0f , 0.0f ) ; 
            loding.startAnimation(fadeIn);
            loding.startAnimation(fadeOut);
            fadeIn.setDuration(500);
            fadeOut.setDuration(1200);
            fadeOut.setStartOffset(1200+fadeIn.getStartOffset()+1200);     
            measurement_index = AppSettings.getMeasureUnit(this);
}

我想重复这个 textview loding 动画,直到从 GPS 获得信息

【问题讨论】:

    标签: android android-animation


    【解决方案1】:

    点赞this

    animation.setRepeatCount(Animation.INFINITE);
    

    【讨论】:

    • 也可以在XML中定义,比如:android:repeatCount = "infinite"
    【解决方案2】:

    Android 为您提供了优雅的机制来表示加载过程。您可以使用不确定的ProgressBar,或带有缩放/alpha 动画的ImageView,而不是为TextView 本身设置动画。

    也许你会发现这个动画很有用,它可以同时为 alpha 和缩放设置动画。根据您的喜好改变参数:

    file res/anim/alpha_scale_animation.xml

    <?xml version="1.0" encoding="utf-8"?>
    <set xmlns:android="http://schemas.android.com/apk/res/android" >
    <scale
        android:fromXScale="0.7"
        android:toXScale="1.0"
        android:fromYScale="0.7"
        android:toYScale="1.0"
        android:pivotX="50%p"
        android:pivotY="50%p"
        android:duration="4000"
        android:repeatCount="infinite"
        />
    
    <alpha
        android:fromAlpha="0.0"
        android:toAlpha="1.0"
        android:duration="2000"
        android:repeatMode="reverse"
        android:repeatCount="infinite"
        />
    </set>
    

    然后在您的代码中启动它:

    Animation connectingAnimation = AnimationUtils.loadAnimation(getContext(), R.anim.alpha_scale_animation);
    myView.startAnimation(connectingAnimation);
    

    然后停止它:

    myView.clearAnimation();
    connectingAnimation.cancel();
    connectingAnimation.reset();
    

    您还可以使用 ProgressButtonView 之类的库在同一个小部件中同时支持用户交互和加载过程。

    希望这些解决方案对某人有用:)

    【讨论】:

    • cancel() 的文档(我正在使用 ValueAnimator,一个子类)声明它需要与调用者在同一个线程上。这相当不方便,但它似乎仍然有效! (对于使用 ValueAnimator 类的其他人,没有reset();似乎不需要它。)
    • 你是对的。 reset() 不是必需的,cancel() 应该足够了
    【解决方案3】:

    您可以使用这些方法来控制重复行为:

    fadeIn.setRepeatCount(int count) // or Animation.INFINITE
    
    fadeIn.setRepeatMode(Animation.REPEAT) // or any other repeat mode, such as Animation.REVERSE
    

    如果需要,实现这些监听器:

    // anim = your animation    
    anim.setAnimationListener(new AnimationListener() {
                public void onAnimationStart(Animation arg0) 
                {
                    // TODO Auto-generated method stub
                }
                
                public void onAnimationRepeat(Animation arg0)
                {
                    // TODO Auto-generated method stub
                }
                
                public void onAnimationEnd(Animation arg0)
                {
                    // TODO Auto-generated method stub
                }
            });
    

    如果您想突然停止动画,请使用yourView.clearAnimation(),希望对您有所帮助。

    【讨论】:

    • setRepeatMode 只需要 RESTART 或 REVERSE。 INFINITE 应该只给 setRepeatCount。
    【解决方案4】:

    要重复动画,只需将代码添加到位于我们创建动画对象的 anim 文件夹文件中的 xml 文件中。

    1. android:repeatCount="infinite"
    2. android:repeatMode="restart"

    【讨论】:

      【解决方案5】:

      将重复计数设置为-1,然后它将动画无限

      【讨论】:

      • 这是一个有效的答案,但包含代码示例可能会很有用。它也不一定通过使用静态值而不是引用官方变量(例如Animation.REPEAT)来遵循最佳编码实践。但这并不意味着它是一个糟糕的答案——欢迎加入社区! :)
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-11-15
      • 2011-12-04
      • 1970-01-01
      • 2010-10-27
      • 2016-11-13
      • 2013-03-05
      • 2011-12-20
      相关资源
      最近更新 更多