Android xml动画加载

问题描述 投票:0回答:1
android xml rotation imageview animationdrawable
1个回答
0
投票

这个动画可以由两个图像组成,第一个是旋转动画,第二个是在完成请求时显示,所有这些都只在一个 ImageView 中运行。

<ImageView
        android:id="@+id/imgAnimate"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:srcCompat="@drawable/image_rotate" />

然后就可以在res中新建一个anim文件夹,添加一个动画文件进行rotate

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<rotate
    android:repeatCount="-1"
    android:pivotX = "50%"
    android:pivotY = "50%"
    android:fromDegrees = "0"
    android:toDegrees = "360"
    android:duration = "2000"/>
</set>

动画现在可以开始了

    private lateinit var img : ImageView
    private lateinit var anim : Animation

    //inside the onCreate
    img = findViewById<ImageView>(R.id.imgAnimate)
    anim = AnimationUtils.loadAnimation(
                this,
                R.anim.rotate)

    img.startAnimation(anim)

最后当您的请求完成时

fun requestCompleted(){
    img.clearAnimation()
    img.setImageDrawable(context.getDrawable(R.drawable.image_completed))
    anim.cancel()
    anim.reset()
}
© www.soinside.com 2019 - 2024. All rights reserved.