কম্পিউটার টিউটোরিয়াল

Android-এ XML ফাইল ব্যবহার করে অ্যানিমেশন তৈরির পদ্ধতি

Android-এ XML ফাইল ব্যবহার করে অ্যানিমেশন তৈরির পদ্ধতি

Android-এ অ্যানিমেশন দুইভাবে তৈরি করা যায়: XML-এ সংজ্ঞায়িত (রিসোর্স ফাইল হিসেবে) এবং প্রোগ্রাম্যাটিক্যালি (জাভা/কোটলিন কোডে)। XML-এ অ্যানিমেশন সংজ্ঞায়িত করা ভালো অনুশীলন কারণ এটি কোড থেকে আলাদা থাকে, পুনঃব্যবহারযোগ্য, এবং মেইনটেইন করা সহজ।

অ্যানিমেশন টাইপসমূহ

টাইপ বর্ণনা XML ট্যাগ
Alpha (ফেড ইন/আউট) দৃশ্যমানতা পরিবর্তন (অপাসিটি) <alpha>
Scale (জুম) আকার পরিবর্তন (বড়/ছোট) <scale>
Translate (সরানো) পজিশন পরিবর্তন (খিসানো) <translate>
Rotate (ঘুরানো) কোণ পরিবর্তন <rotate>
Set (সেট) একাধিক অ্যানিমেশন একসাথে/ক্রমাগত <set>

প্রয়োজনীয় ধাপসমূহ

ধাপ ১: নতুন প্রজেক্ট তৈরি করুন

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="4dp"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="শুভ দিন!"
        android:textSize="24sp"
        android:textStyle="bold" />

    <Button
        android:id="@+id/button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:text="অ্যানিমেশন শুরু করুন" />

</RelativeLayout>

ধাপ ৩: anim রিসোর্স ডিরেক্টরি এবং অ্যানিমেশন ফাইল তৈরি করুন

1. res ফোল্ডারে রাইট ক্লিক → New → Android Resource Directory

2. Resource type: anim সিলেক্ট করুন

3. anim ফোল্ডারের মধ্যে নিম্নলিখিত ফাইলগুলো তৈরি করুন:

res/anim/fade_in.xml (ফেড ইন)

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="https://schemas.android.com/apk/res/android"
    android:fillAfter="true">
    <alpha
        android:duration="1000"
        android:fromAlpha="0.0"
        android:toAlpha="1.0"
        android:interpolator="@android:anim/accelerate_interpolator" />
</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:toXScale="3.0"
        android:toYScale="3.0"
        android:pivotX="50%"
        android:pivotY="50%" />
</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:interpolator="@android:anim/accelerate_interpolator"
        android:repeatMode="reverse"
        android:repeatCount="infinite" />
</set>

res/anim/slide_up.xml (স্লাইড আপ)

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="https://schemas.android.com/apk/res/android">
    <translate
        android:duration="800"
        android:fromYDelta="100%"
        android:toYDelta="0%"
        android:interpolator="@android:anim/decelerate_interpolator" />
    <alpha
        android:duration="800"
        android:fromAlpha="0.0"
        android:toAlpha="1.0" />
</set>

res/anim/bounce.xml (বাউন্স ইফেক্ট)

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="https://schemas.android.com/apk/res/android"
    android:interpolator="@android:anim/bounce_interpolator">
    <scale
        android:duration="1000"
        android:fromXScale="0.5"
        android:fromYScale="0.5"
        android:toXScale="1.0"
        android:toYScale="1.0"
        android:pivotX="50%"
        android:pivotY="50%" />
</set>

ধাপ ৪: MainActivity.java বাস্তবায়ন করুন

import androidx.appcompat.app.AppCompatActivity;
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;

public class MainActivity extends AppCompatActivity implements Animation.AnimationListener {

    TextView textView;
    Button button;
    Animation currentAnimation;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        textView = findViewById(R.id.textView);
        button = findViewById(R.id.button);

        // ডিফল্ট অ্যানিমেশন লোড করুন
        currentAnimation = AnimationUtils.loadAnimation(this, R.anim.blink);
        currentAnimation.setAnimationListener(this);

        button.setOnClickListener(v -> {
            textView.setVisibility(View.VISIBLE);
            textView.startAnimation(currentAnimation);
        });
    }

    @Override
    public void onAnimationStart(Animation animation) {
        // অ্যানিমেশন শুরু হলে
    }

    @Override
    public void onAnimationEnd(Animation animation) {
        // অ্যানিমেশন শেষ হলে
    }

    @Override
    public void onAnimationRepeat(Animation animation) {
        // অ্যানিমেশন রিপিট হলে (ব্লিংকের জন্য)
    }
}

অতিরিক্ত: একাধিক অ্যানিমেশন সিকোয়েন্সিয়ালি চালানো

// প্রথমে ফেড ইন, তারপর জুম
Animation fadeIn = AnimationUtils.loadAnimation(this, R.anim.fade_in);
Animation zoom = AnimationUtils.loadAnimation(this, R.anim.zoom);

fadeIn.setAnimationListener(new Animation.AnimationListener() {
    @Override
    public void onAnimationStart(Animation animation) {}

    @Override
    public void onAnimationEnd(Animation animation) {
        // ফেড ইন শেষ হলে জুম শুরু করুন
        textView.startAnimation(zoom);
    }

    @Override
    public void onAnimationRepeat(Animation animation) {}
});

textView.startAnimation(fadeIn);

আধুনিক বিকল্প: ViewPropertyAnimator (API 12+) / MotionLayout

সরল অ্যানিমেশনের জন্য XML ছাড়াই সরাসরি ভিউতে অ্যানিমেট করা যায়:

// ফেড ইন
textView.animate()
    .alpha(1f)
    .setDuration(1000)
    .setStartDelay(200)
    .start();

// জুম + রোটেট একসাথে
textView.animate()
    .scaleX(2f)
    .scaleY(2f)
    .rotation(360f)
    .setDuration(1500)
    .setInterpolator(new OvershootInterpolator())
    .start();

// সিকোয়েন্সিয়াল
textView.animate()
    .alpha(0f)
    .setDuration(500)
    .withEndAction(() -> textView.animate()
        .alpha(1f)
        .scaleX(1.5f)
        .scaleY(1.5f)
        .setDuration(500)
        .start()
    );

ধাপ ৫: AndroidManifest.xml

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

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

ইন্টারপোলেটর (Interpolator) টাইপস

  • @android:anim/accelerate_interpolator - ধীরে শুরু, দ্রুত শেষ
  • @android:anim/decelerate_interpolator - দ্রুত শুরু, ধীরে শেষ
  • @android:anim/accelerate_decelerate_interpolator - ধীরে শুরু, মাঝপথে দ্রুত, ধীরে শেষ
  • @android:anim/bounce_interpolator - বাউন্স ইফেক্ট
  • @android:anim/overshoot_interpolator - লক্ষ্য অতিক্রম করে ফিরে আসা
  • @android:anim/anticipate_interpolator - বিপরীত দিকে একটু গিয়ে আসল দিকে যাওয়া
  • @android:anim/linear_interpolator - সমান গতিতে (ডিফল্ট)

অ্যাপ্লিকেশন চালানোর পদ্ধতি

আপনার Android ডিভাইস কম্পিউটারের সাথে সংযুক্ত করুন। Android Studio-তে Run আইকনে ক্লিক করুন। "অ্যানিমেশন শুরু করুন" বাটনে ক্লিক করলে টেক্সট ব্লিংক করবে।

Android-এ XML ফাইল ব্যবহার করে অ্যানিমেশন তৈরির পদ্ধতি

Android-এ XML ফাইল ব্যবহার করে অ্যানিমেশন তৈরির পদ্ধতি

  1. অ্যান্ড্রয়েড অ্যাপে TextToSpeech তৈরি করার পদ্ধতি

  2. Facebook-এ Android অ্যাপ তৈরি করার পদ্ধতি

  3. Android-এ মাল্টিলেভেল ListView (ExpandableListView) তৈরির পদ্ধতি

  4. Android-এ XML ফাইল ব্যবহার করে অ্যানিমেশন তৈরির পদ্ধতি