Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> Android

Tạo Animation bằng file XML trong Android Kotlin

Giới thiệu

Ví dụ này minh họa cách tạo hiệu ứng animation (Zoom và Blink) bằng file XML trong thư mục res/anim/ và áp dụng lên View (TextView) trong Android Kotlin.

Các bước thực hiện

Bước 1: Tạo dự án mới

Mở Android Studio, chọn File → New Project, điền thông tin.

Bước 2: Giao diện (res/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="4dp"
    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:text="Zoom" />

    <Button
        android:id="@+id/buttonBlink"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:text="Blink" />

</RelativeLayout>

Bước 3: Tạo thư mục anim và file animation

  1. Chuột phải res → New → Android Resource Directory, Resource type: anim, Directory name: anim.
  2. Chuột phải anim → New → Animation Resource File.

res/anim/zoom.xml

<?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:fromYScale="1.0"
        android:pivotX="50%"
        android:pivotY="50%"
        android:toXScale="3.0"
        android:toYScale="3.0" />
</set>

res/anim/blink.xml

<?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>

Bước 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)

        zoomAnim = AnimationUtils.loadAnimation(this, R.anim.zoom)
        zoomAnim.setAnimationListener(this)

        blinkAnim = AnimationUtils.loadAnimation(this, R.anim.blink)
        blinkAnim.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) {}
    override fun onAnimationEnd(animation: Animation) {}
    override fun onAnimationRepeat(animation: Animation) {}
}

Bước 5: AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
    package="com.example.q11">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">

        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

    </application>
</manifest>

Chạy ứng dụng

Kết nối thiết bị, nhấn Run. Nhấn nút "Zoom" → TextView phóng to 3 lần. Nhấn "Blink" → TextView nhấp nháy vô hạn.

Lưu ý: Hệ thống View Animation (XML) cũ, hạn chế. Nên dùng Property Animation (ObjectAnimator, ValueAnimator) hoặc MotionLayout cho animation phức tạp, hiệu năng tốt hơn.