1.逐帧动画
添加图片资源
新建anim文件夹
2.补间动画 RotateAnimation
旋转
import android.os.Bundle
import android.os.Handler
import android.view.animation.Animation
import android.view.animation.RotateAnimation
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val hello = findViewById<TextView>(R.id.hello);
var ra = RotateAnimation(0f,-180f, // 开始结束的角度 逆时针
Animation.RELATIVE_TO_SELF,0.5f, // 相对坐标x的坐标点(旋转中心x的值)
Animation.RELATIVE_TO_SELF,1.0f);// 相对坐标y的坐标点(旋转中心y的值)
ra.duration = 500
ra.fillAfter = true
// ra.startOffset = 4000 // 延时
hello.startAnimation(ra)
Handler().postDelayed(Runnable { // 延时后的操作
var ra1 = RotateAnimation(-180f,0f, // 开始结束的角度 逆时针
Animation.RELATIVE_TO_SELF,0.5f, // 相对坐标x的坐标点(旋转中心x的值)
Animation.RELATIVE_TO_SELF,1.0f);// 相对坐标y的坐标点(旋转中心y的值)
ra1.duration = 500
ra1.fillAfter = true
hello.startAnimation(ra1)
}, 2000)
}
}
xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="@+id/hello"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
属性动画 Property animation
属性动画就是在一定的时间内,按照一定的规律来改变对象的属性(该属性对于该对象应该是从形态(大小,位置等)上可以感受到的),从而是对象展现出动画的效果。