Simple summary of Android animation
1. Android animation includes attribute animation and traditional animation (frame animation and tween animation)
2. There are two ways to implement Android animation: XML settings and Java (Kotlin) code implementation
3. Common animation implementation examples
<1> Zoom
<scale
android:duration="2000"
android:fillAfter="false"
android:fromXScale="0.0"
android:fromYScale="0.0"
android:interpolator="@android:anim/accelerate_decelerate_interpolator"
android:pivotX="50%"
android:pivotY="50%"
android:toXScale="1.0"
android:toYScale="1.0"
/>
(1) Transparency
<alpha
android:duration="5000"
android:fromAlpha="0"
android:toAlpha="1"
/>
<1> Rotation
<rotate
android:duration="500"
android:fromDegrees="0"
android:interpolator="@android:anim/accelerate_decelerate_interpolator"
android:pivotX="50%"
android:pivotY="50%"
android:toDegrees="+360"
/>
<1> Displacement
<translate
android:duration="10000"
android:fromXDelta="500"
android:fromYDelta="0"
android:toXDelta="0"
android:toYDelta="0"
/>
Kotlin code: apply the above XML file to the image
val loadAnimation : Animation
when(view?.id) {
/** 缩放 */
R.id.btnScales -> {
loadAnimation = AnimationUtils.loadAnimation(mContext, R.anim.scales)
imgView.startAnimation(loadAnimation)
}
/** 透明度 */
R.id.btnAlpha -> {
loadAnimation = AnimationUtils.loadAnimation(mContext, R.anim.alpha)
imgView.startAnimation(loadAnimation)
}
/** 旋转 */
R.id.btnRotate -> {
loadAnimation = AnimationUtils.loadAnimation(mContext, R.anim.rotate)
imgView.startAnimation(loadAnimation)
}
/** 位移 */
R.id.btnTranslate -> {
loadAnimation = AnimationUtils.loadAnimation(mContext, R.anim.translate)
imgView.startAnimation(loadAnimation)
}
/** 切换 */
R.id.btnChange -> {
val intent = Intent(mContext, SecondActivity::class.java)
mContext.startActivity(intent)
overridePendingTransition(R.anim.alpha, R.anim.rotate)
}
/** 闪烁 */
R.id.btnFlash -> {
val alphaAnimation = AlphaAnimation(0f, 0.5f)
alphaAnimation.duration = 100
alphaAnimation.repeatCount = 50
alphaAnimation.repeatMode = Animation.REVERSE
imgView.startAnimation(alphaAnimation)
}
}
frame animation
<animation-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:drawable="@drawable/ic_1"
android:duration="100" />
<item
android:drawable="@drawable/ic_2"
android:duration="100" />
<item
android:drawable="@drawable/ic_3"
android:duration="100" />
<item
android:drawable="@drawable/ic_4"
android:duration="100" />
<item
android:drawable="@drawable/ic_5"
android:duration="100" />
<item
android:drawable="@drawable/ic_6"
android:duration="100" />
<item
android:drawable="@drawable/ic_7"
android:duration="100" />
<item
android:drawable="@drawable/ic_8"
android:duration="100" />
</animation-list>
Kotlin code
imgAnimation.setImageResource(R.drawable.loading)
val animationDrawable : AnimationDrawable = imgAnimation.drawable as AnimationDrawable
animationDrawable.start()
The above is the code (Kotlin, not Java) implementation of Android animation implementation. If you have any questions, you can tell me by E-mail, and I will try my best to correct it.
In addition , I have put the code on Github: https:// github.com/fengwenyi/android-animation