1.activity转场动画
<item name="android:windowAnimationStyle">@style/MywindowAnim</item>
------------------------------------------------------------------------------
<style name="MywindowAnim">
<item name="android:activityOpenEnterAnimation">@anim/slide_in_left</item>
<item name="android:activityOpenExitAnimation">@anim/slide_out_left</item>
<item name="android:activityCloseEnterAnimation">@anim/slide_in_right</item>
<item name="android:activityCloseExitAnimation">@anim/slide_out_right</item>
</style>
anim
slide_in_left
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:duration="300"
android:fromXDelta="100%"
android:toXDelta="0" />
</set>
slide_in_right
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:duration="300"
android:fromXDelta="-100%"
android:toXDelta="0" />
</set>
slide_out_left
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:duration="300"
android:fromXDelta="0"
android:toXDelta="-100%" />
</set>
slide_out_right
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:duration="300"
android:fromXDelta="0"
android:toXDelta="100%" />
</set>
2.帧动画(AnimationDrawable)
class FramAnimActivity : AppCompatActivity() {
private val animationDrawable: AnimationDrawable by lazy { view.background as AnimationDrawable }
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_fram_anim)
animationDrawable.isOneShot = false
btnStart.setOnClickListener { animationDrawable.start() }
btnStop.setOnClickListener { animationDrawable.stop() }
}
}
-----------------------------------------------------------------------------
<View
android:id="@+id/view"
android:layout_width="300dp"
android:layout_height="300dp"
android:layout_centerInParent="true"
android:background="@drawable/loading" />
-----------------------------------------------------------------------------
drawable/loading:
<?xml version="1.0" encoding="utf-8"?>
<animation-list android:oneshot="true"
xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:drawable="@drawable/frame_1"
android:duration="100" />
<item
android:drawable="@drawable/frame_2"
android:duration="100" />
<item
android:drawable="@drawable/frame_3"
android:duration="100" />
</animation-list>
3.视图动画(AnimationUtils.loadAnimation)
class ViewAnimActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_view_anim)
}
fun onClick(view: View) {
when (view.id) {
R.id.viewAlphaAnimation -> {
val alphaAnimation = AnimationUtils.loadAnimation(this, R.anim.alpha)
view.startAnimation(alphaAnimation)
}
R.id.viewScaleAnimation -> {
val scaleAnimation = AnimationUtils.loadAnimation(this, R.anim.scale)
view.startAnimation(scaleAnimation)
}
R.id.viewTranslateAnimation -> {
val translateAnimation = AnimationUtils.loadAnimation(this, R.anim.translate)
view.startAnimation(translateAnimation)
}
R.id.viewRotateAnimation -> {
val rotateAnimation = AnimationUtils.loadAnimation(this, R.anim.rotate)
view.startAnimation(rotateAnimation)
}
R.id.viewSetAnimation -> {
val setAnimation = AnimationUtils.loadAnimation(this, R.anim.set)
view.startAnimation(setAnimation)
}
R.id.viewAccelerate -> {
val animationAccelerate = AnimationUtils.loadAnimation(this, R.anim.translate)
animationAccelerate.interpolator = AccelerateInterpolator()//平移先慢后快的效果
view.startAnimation(animationAccelerate)
}
R.id.viewLinear -> {
val animationLinear = AnimationUtils.loadAnimation(this, R.anim.translate)
animationLinear.interpolator = LinearInterpolator()
view.startAnimation(animationLinear)
}
}
}
}
anim
alpha:
<?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:toAlpha="0.1" />
</set>
rotate:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:fillAfter="true">
<rotate
android:duration="300"
android:pivotX="50%"
android:pivotY="50%"
android:repeatCount="6"
android:fromDegrees="0"
android:toDegrees="360" />
</set>
scale:
<?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:toXScale="2.0"
android:toYScale="1.0" />
</set>
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"
android:pivotX="50%"
android:pivotY="50%"
android:toDegrees="720" />
<translate
android:duration="1000"
android:fromXDelta="0"
android:fromYDelta="0"
android:startOffset="0"
android:toXDelta="500"
android:toYDelta="0" />
</set>
4.属性动画
class PropertyActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_property)
}
fun onClick(view: View) {
when (view.id) {
R.id.viewAlphaAnimation -> {
val alphaAnimator = AnimatorInflater.loadAnimator(this, R.animator.alpha)
alphaAnimator.setTarget(view)
alphaAnimator.start()
}
R.id.viewScaleAnimation -> {
ObjectAnimator.ofFloat(view, "scaleX", 1.0f, 3.0f).start()
}
R.id.viewTranslateAnimation -> {
view.animate().translationX(500f).setDuration(1000).start()
}
R.id.viewRotateAnimation -> {
view.animate().rotation(720f).start()
}
R.id.viewSetAnimation -> {
view.animate().rotation(720f).setDuration(1000).start()
view.animate().translationX(500f).setDuration(1000).setStartDelay(1000).start()
}
}
}
}