首页 > 解决方案 > Android动画旋转属性不起作用

问题描述

我正在尝试仅使用 xml 制作自定义动画进度条。

我用圆圈制作了图层列表,但是当我对具有不同属性的所有圆圈使用标签动画旋转时,它们仍然在做同样的事情。

我怎样才能为这个列表中的每个项目制作不同的旋转动画?

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">

 <item android:id="@+id/first">
        <animated-rotate
            android:duration="2000"
            android:fromDegrees="0"
            android:interpolator="@android:anim/linear_interpolator"
            android:pivotX="50%"
            android:pivotY="50%"
            android:repeatCount="1"
            android:toDegrees="-360">
            <shape
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:innerRadius="100dp"
                android:shape="oval">
                <padding
                    android:bottom="3dp"
                    android:left="3dp"
                    android:right="3dp"
                    android:top="3dp" />
                <stroke
                    android:width="2dp"
                    android:color="@color/colorPrimary"
                    android:dashWidth="60dp"
                    android:dashGap="60dp" />
                <size
                    android:width="150dp"
                    android:height="150dp" />
            </shape>
        </animated-rotate>
    </item>
    <item>
        <animated-rotate
            android:duration="300"
            android:fromDegrees="0"
            android:interpolator="@android:anim/cycle_interpolator"
            android:pivotX="50%"
            android:pivotY="50%"
            android:repeatCount="infinite"
            android:toDegrees="360">
            <shape
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:innerRadius="100dp"
                android:shape="oval">
                <padding
                    android:bottom="3dp"
                    android:left="3dp"
                    android:right="3dp"
                    android:top="3dp" />

                <stroke
                    android:width="2dp"
                    android:color="@color/colorSecondary"
                    android:dashWidth="50dp"
                    android:dashGap="50dp" />
            </shape>
        </animated-rotate>
    </item>
</layer-list>

标签: androidxmlandroid-studiokotlin

解决方案


一种解决方案是使用动画创建一个文件。在您的代码内部,使用线程设置动画开始之间的延迟。

例如,

Thread t = new Thread(){  
    public void run(){  
        t.sleep(500);
        Animation rotation = AnimationUtils.loadAnimation(StartActivity.this, R.anim.rotate);
        view.startAnimation(rotation);

        t.sleep(500);
        Animation rotation = AnimationUtils.loadAnimation(StartActivity.this, R.anim.rotate);
        view2.startAnimation(rotation);

        t.sleep(500):
        Animation rotation = AnimationUtils.loadAnimation(StartActivity.this, R.anim.rotate);
        view3.startAnimation(rotation);
        ..... (This would go on until you started the animation for all the circles)
    }  
}
t.start();

推荐阅读