演示
tweenAnim.gif布局资源
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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"
android:fitsSystemWindows="true"
android:background="@drawable/ic_launcher_background"
tools:context=".MainActivity">
<ImageView
android:id="@+id/gifView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:contentDescription="@null"
android:src="@drawable/bean_one"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/btnTranslate"
style="@android:style/Widget.Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/offset"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent" />
<Button
android:id="@+id/btnRotate"
style="@android:style/Widget.Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/rotate"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.5" />
<Button
android:id="@+id/btnScale"
style="@android:style/Widget.Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/scale"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent" />
<Button
android:id="@+id/btnAlpha"
style="@android:style/Widget.Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/transition"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
动画资源
属性:
duration 持续时间
fromXXX 起始透明度/角度/比例/位置
toXXX 结束透明度/角度/比例/位置
interpolator 补间动画
repeatCount 重复次数
repeatMode 重复模式
pivotXY 中心点
渐变动画
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<alpha
android:duration="1000"
android:fromAlpha="1.0"
android:interpolator="@android:anim/accelerate_interpolator"
android:repeatCount="infinite"
android:repeatMode="reverse"
android:toAlpha="0.0" />
</set>
旋转动画
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<rotate
android:duration="1000"
android:fromDegrees="0.0"
android:pivotX="50%"
android:pivotY="50%"
android:repeatCount="infinite"
android:repeatMode="reverse"
android:toDegrees="360.0" />
</set>
缩放动画
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<scale
android:duration="1000"
android:fromXScale="1.0"
android:fromYScale="1.0"
android:pivotX="50%"
android:pivotY="50%"
android:repeatCount="infinite"
android:repeatMode="reverse"
android:toXScale="0.5"
android:toYScale="0.5" />
</set>
平移动画
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:duration="1000"
android:fromXDelta="-200.0"
android:fromYDelta="0.0"
android:repeatCount="infinite"
android:repeatMode="reverse"
android:toXDelta="200.0"
android:toYDelta="0.0" />
</set>
主界面代码
package cn.dev.tweenanim
import android.graphics.Color
import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.view.View
import android.view.animation.AnimationUtils
import kotlinx.android.synthetic.main.activity_main.*
class MainActivity : AppCompatActivity(), View.OnClickListener {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
window.decorView.systemUiVisibility = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
window.statusBarColor = Color.TRANSPARENT
btnTranslate.setOnClickListener(this)
btnRotate.setOnClickListener(this)
btnScale.setOnClickListener(this)
btnAlpha.setOnClickListener(this)
}
override fun onClick(view: View) {
when (view.id) {
R.id.btnTranslate -> {
val animOffset = AnimationUtils.loadAnimation(this, R.anim.translate)//加载平移动画
gifView.startAnimation(animOffset)//启动平移动画
}
R.id.btnRotate -> {
val animRotate = AnimationUtils.loadAnimation(this, R.anim.rotate)//加载旋转动画
gifView.startAnimation(animRotate)//启动旋转动画
}
R.id.btnScale -> {
val animScale = AnimationUtils.loadAnimation(this, R.anim.scale)//加载缩放动画
gifView.startAnimation(animScale)//启动缩放动画
}
R.id.btnAlpha -> {
val animTransition = AnimationUtils.loadAnimation(this, R.anim.alpha)//加载渐变动画
gifView.startAnimation(animTransition)//启动渐变动画
}
}
}
}
网友评论