ภาพรวม
ตัวอย่างนี้สาธิตการสร้าง Animation ใน Android โดยใช้ XML Resource Files (ในโฟลเดอร์ res/anim/) และนำไปใช้กับ View ผ่าน AnimationUtils
ขั้นตอนที่ 1: สร้างโปรเจกต์ใหม่
เปิด Android Studio เลือก File → New Project
ขั้นตอนที่ 2: Layout (activity_main.xml)
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
xmlns:tools="https://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="16dp"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="50dp"
android:text="Tutorials Point"
android:textAlignment="center"
android:textColor="@android:color/holo_green_dark"
android:textSize="32sp"
android:textStyle="bold" />
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="Have a Wonderful day!"
android:textColor="@android:color/holo_purple"
android:textSize="24sp"
android:textStyle="bold" />
<Button
android:id="@+id/buttonZoom"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="@id/buttonBlink"
android:layout_marginBottom="16dp"
android:text="Zoom Animation" />
<Button
android:id="@+id/buttonBlink"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:text="Blink Animation" />
</RelativeLayout>
ขั้นตอนที่ 3: สร้าง Animation XML (res/anim/)
คลิกขวาที่ res → New → Android Resource Directory → Resource type: anim
blink.xml (Alpha Animation - กระพริบ)
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="https://schemas.android.com/apk/res/android">
<alpha
android:duration="600"
android:fromAlpha="0.0"
android:toAlpha="1.0"
android:repeatCount="infinite"
android:repeatMode="reverse"
android:interpolator="@android:anim/accelerate_interpolator" />
</set>
zoom.xml (Scale Animation - ซูม)
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="https://schemas.android.com/apk/res/android"
android:fillAfter="true">
<scale
android:duration="1000"
android:fromXScale="1.0"
android:toXScale="3.0"
android:fromYScale="1.0"
android:toYScale="3.0"
android:pivotX="50%"
android:pivotY="50%" />
</set>
Animation XML อื่นๆ ที่น่าสนใจ
slide_in_left.xml (Slide เข้าจอ)
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="https://schemas.android.com/apk/res/android">
<translate
android:duration="500"
android:fromXDelta="-100%"
android:toXDelta="0%"
android:interpolator="@android:anim/decelerate_interpolator" />
<alpha
android:duration="500"
android:fromAlpha="0.0"
android:toAlpha="1.0" />
</set>
rotate.xml (หมุน)
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="https://schemas.android.com/apk/res/android">
<rotate
android:duration="1000"
android:fromDegrees="0"
android:toDegrees="360"
android:pivotX="50%"
android:pivotY="50%"
android:repeatCount="infinite"
android:interpolator="@android:anim/linear_interpolator" />
</set>
bounce.xml (โบ้ง - ใช้ Interpolator)
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="https://schemas.android.com/apk/res/android">
<scale
android:duration="1000"
android:fromXScale="0.0"
android:toXScale="1.0"
android:fromYScale="0.0"
android:toYScale="1.0"
android:pivotX="50%"
android:pivotY="50%"
android:interpolator="@android:anim/bounce_interpolator" />
</set>
ขั้นตอนที่ 4: MainActivity.kt
import android.os.Bundle
import android.view.View
import android.view.animation.Animation
import android.view.animation.AnimationUtils
import android.widget.Button
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
class MainActivity : AppCompatActivity(), Animation.AnimationListener {
private lateinit var textView: TextView
private lateinit var buttonZoom: Button
private lateinit var buttonBlink: Button
private lateinit var zoomAnim: Animation
private lateinit var blinkAnim: Animation
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
title = "KotlinApp"
textView = findViewById(R.id.textView)
buttonZoom = findViewById(R.id.buttonZoom)
buttonBlink = findViewById(R.id.buttonBlink)
// โหลด Animation จาก XML
blinkAnim = AnimationUtils.loadAnimation(this, R.anim.blink)
blinkAnim.setAnimationListener(this)
zoomAnim = AnimationUtils.loadAnimation(this, R.anim.zoom)
zoomAnim.setAnimationListener(this)
buttonZoom.setOnClickListener {
textView.visibility = View.VISIBLE
textView.startAnimation(zoomAnim)
}
buttonBlink.setOnClickListener {
textView.visibility = View.VISIBLE
textView.startAnimation(blinkAnim)
}
}
override fun onAnimationStart(animation: Animation) {
// Animation เริ่มต้น
}
override fun onAnimationEnd(animation: Animation) {
// Animation จบ (สำหรับ non-repeating)
if (animation == zoomAnim) {
// ทำบางอย่างหลัง Zoom จบ
}
}
override fun onAnimationRepeat(animation: Animation) {
// Animation ทำซ้ำ (สำหรับ Blink)
}
}
วิธีการทำงานสมัยใหม่ (ViewPropertyAnimator / MotionLayout)
สำหรับ Animation แบบง่าย ควรใช้ ViewPropertyAnimator (API 12+) ไม่ต้องสร้าง XML:
// Fade In/Out
textView.animate().alpha(0f).setDuration(300).start()
textView.animate().alpha(1f).setDuration(300).start()
// Scale (Zoom)
textView.animate().scaleX(3f).scaleY(3f).setDuration(1000).start()
// Translate (Slide)
textView.animate().translationX(200f).setDuration(500).start()
// Rotate
textView.animate().rotation(360f).setDuration(1000).start()
// Chain animations
textView.animate()
.alpha(0f)
.setDuration(300)
.withEndAction {
textView.scaleX = 0.5f
textView.scaleY = 0.5f
textView.animate().alpha(1f).scaleX(1f).scaleY(1f).setDuration(300).start()
}
.start()
ขั้นตอนที่ 5: Manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
package="com.example.animationdemo">
<application ... >
<activity android:name=".MainActivity"> ... </activity>
</application>
</manifest>
Interpolator ที่พบบ่อย
| Interpolator | ลักษณะ |
|---|---|
@android:anim/linear_interpolator | ความเร็วคงที่ |
@android:anim/accelerate_interpolator | เริ่มช้า เร็วขึ้น |
@android:anim/decelerate_interpolator | เริ่มเร็ว ช้าลง |
@android:anim/accelerate_decelerate_interpolator | ช้า-เร็ว-ช้า (Default) |
@android:anim/bounce_interpolator | กระเด้งท้าย |
@android:anim/anticipate_interpolator | ถอยหลังก่อน แล้วกระโดดไป |
@android:anim/overshoot_interpolator | เกินเป้าหมาย แล้วกลับมา |
การทดสอบ
รันแอป กด "Zoom Animation" ข้อความจะขยาย 3 เท่า กด "Blink Animation" ข้อความจะกระพริบต่อเนื่อง