এই উদাহরণটি প্রদর্শন করে কীভাবে অ্যান্ড্রয়েড অ্যাপে কোটলিন ব্যবহার করে XML ফাইল দিয়ে অ্যানিমেশন তৈরি করা যায়।
পদক্ষেপ ১: নতুন প্রজেক্ট তৈরি করুন
Android Studio-তে যান File → New Project এবং প্রজেক্ট তৈরি করার জন্য প্রয়োজনীয় সব বিবরণ পূরণ করুন।
পদক্ষেপ ২: 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="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="8dp"
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>
পদক্ষেপ ৩: src/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)
// 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) {}
override fun onAnimationEnd(animation: Animation) {}
override fun onAnimationRepeat(animation: Animation) {}
}
পদক্ষেপ ৪: anim রিসোর্স ডিরেক্টরি এবং ফাইল তৈরি করুন
- Right-click
res→New→Android Resource Directory - Resource type:
animসিলেক্ট করুন - anim ফোল্ডারে রাইট-ক্লিক →
New→Animation Resource File
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:interpolator="@android:anim/accelerate_interpolator"
android:repeatCount="infinite"
android:repeatMode="reverse"
android:toAlpha="1.0" />
</set>
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/fade_in.xml
<alpha xmlns:android="https://schemas.android.com/apk/res/android"
android:duration="1000"
android:fromAlpha="0.0"
android:toAlpha="1.0" />
res/anim/slide_up.xml
<set xmlns:android="https://schemas.android.com/apk/res/android">
<translate
android:duration="500"
android:fromYDelta="100%"
android:toYDelta="0%" />
<alpha
android:duration="500"
android:fromAlpha="0.0"
android:toAlpha="1.0" />
</set>
res/anim/bounce.xml
<set xmlns:android="https://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/bounce_interpolator">
<scale
android:duration="1000"
android:fromXScale="0.0"
android:fromYScale="0.0"
android:pivotX="50%"
android:pivotY="50%"
android:toXScale="1.0"
android:toYScale="1.0" />
</set>
পদক্ষেপ ৫: 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>
আধুনিক বিকল্প: MotionLayout, AnimatedVectorDrawable, বা Jetpack Compose-এ Animation API ব্যবহার করুন জটিল অ্যানিমেশনের জন্য।
অ্যাপ্লিকেশন চালান
আপনার আসল অ্যান্ড্রয়েড ডিভাইস কম্পিউটারের সাথে সংযুক্ত আছে বলে ধরে। Android Studio-তে আপনার প্রজেক্টের এক্টিভিটি ফাইল খুলুন এবং টুলবারের রান আইকনে ক্লিক করুন। আপনার মোবাইল ডিভাইস সিলেক্ট করুন এবং ডিভাইসে ডিফল্ট স্ক্রিন দেখুন।