如何制作一组ObjectAnimator循环?

时间:2013-10-24 02:54:02

标签: android

我想制作像心跳一样的动画,一张图片快速将其ALPHA从0更改为1,慢慢地从1更改为0.我在XML文件中定义它。

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:ordering="sequentially">
    <objectAnimator
        android:propertyName="alpha"
        android:valueFrom="0"
        android:valueTo="1"
        android:valueType="floatType"
        android:duration="700"/>
    <objectAnimator
        android:propertyName="alpha"
        android:valueFrom="1"
        android:valueTo="0"
        android:valueType="floatType"
        android:duration="1500"/>
</set>

我在Java Code中使用它们。

private void cursorHeartBeatAnimation() {
    Animator animator = AnimatorInflater.loadAnimator(this, R.animator.cursor_heartbeat);
    animator.setTarget(this.cursor);
    animator.setStartDelay(1500);
    animator.start();
}

我想让他们一直玩,怎么做?

2 个答案:

答案 0 :(得分:0)

使用&#34; repeatMode&#34; &#34; set&#34;。

的属性
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:repeatMode="restart"
    android:ordering="sequentially">
    <objectAnimator
        android:propertyName="alpha"
        android:valueFrom="0"
        android:valueTo="1"
        android:valueType="floatType"
        android:duration="700"/>
    <objectAnimator
        android:propertyName="alpha"
        android:valueFrom="1"
        android:valueTo="0"
        android:valueType="floatType"
        android:duration="1500"/>
</set>

或者你可以通过编程方式进行:

Animator animator = AnimatorInflater.loadAnimator(this, R.animator.your_animation);
        animator.setTarget(view);
        animator.addListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animation) {

            }

            @Override
            public void onAnimationEnd(Animator animation) {
                animation.start();
            }

            @Override
            public void onAnimationCancel(Animator animation) {

            }

            @Override
            public void onAnimationRepeat(Animator animation) {

            }
        });
        animator.start();

答案 1 :(得分:-3)

尝试setRepeatMode(int)

private void cursorHeartBeatAnimation() {
Animator animator = AnimatorInflater.loadAnimator(this, R.animator.cursor_heartbeat);
animator.setRepeatMode(Animation.INFINITE);
animator.setStartDelay(1500);
animator.start();

}