我想制作一个像心跳的动画,一张图片迅速将其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();
}


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

最佳答案

使用“设置”的“ 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();

10-08 14:37