2

我想做一个像心跳一样的动画,一张图片快速将其 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 代码中使用它们。

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

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

4

2 回答 2

0

使用“set”的“repeatMode”属性。

<?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();
于 2016-11-03T14:52:24.673 回答
-2

尝试setRepeatMode(int)

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

}

于 2013-10-24T03:36:52.197 回答