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

জাভাতে অ্যান্ড্রয়েড Fade In এবং Fade Out অ্যানিমেশন কীভাবে ব্যবহার করবেন?

ফেড ইন/আউট অ্যানিমেশন

AlphaAnimation এবং ObjectAnimator ব্যবহার করে ভিউতে ফেড ইন (দেখানোর) এবং ফেড আউট (লুকানোর) অ্যানিমেশন দেওয়া যায়।

উদাহরণ

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

ধাপ ২: লেআউট XML (res/layout/activity_main.xml)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:id="@+id/parent"
    xmlns:tools="https://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:gravity="center"
    android:orientation="vertical">
    <ImageView
        android:id="@+id/imageView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@mipmap/ic_launcher" />
    <Button
        android:id="@+id/fadeIn"
        android:text="Fade In"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <Button
        android:id="@+id/fadeOut"
        android:text="Fade Out"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</LinearLayout>

ধাপ ৩: জাভা কোড (src/MainActivity.java)

package com.example.andy.myapplication;

import android.animation.ObjectAnimator;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.RequiresApi;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.widget.Button;
import android.widget.ImageView;

public class MainActivity extends AppCompatActivity {

    @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        final ImageView imageView = findViewById(R.id.imageView);
        Button fadeIn = findViewById(R.id.fadeIn);
        Button fadeOut = findViewById(R.id.fadeOut);

        // Fade In: অদৃশ্য থেকে দৃশ্যমান (0.0f -> 1.0f)
        fadeIn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AlphaAnimation alphaAnimation = new AlphaAnimation(0.0f, 1.0f);
                alphaAnimation.setDuration(1000); // ১ সেকেন্ড
                alphaAnimation.setRepeatCount(1); // একবার রিপিট
                alphaAnimation.setRepeatMode(Animation.REVERSE); // উল্টা দিয়ে ফিরে আসবে
                imageView.startAnimation(alphaAnimation);
            }
        });

        // Fade Out: দৃশ্যমান থেকে অদৃশ্য (1f -> 0f)
        fadeOut.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ObjectAnimator fadeOutAnim = ObjectAnimator.ofFloat(imageView, "alpha", 1f, 0f);
                fadeOutAnim.setDuration(2000); // ২ সেকেন্ড
                fadeOutAnim.start();
            }
        });
    }
}

দুই পদ্ধতির তুলনা

বৈশিষ্ট্যAlphaAnimation (View Animation)ObjectAnimator (Property Animation)
API লেভেলAPI 1+API 11+
অ্যানিমেট করেড্রইং মাত্র (ভিউর আসল অবস্থা বদলায় না)প্রোপার্টি মান (ভিউর আসল অবস্থা বদলায়)
ক্লিক হ্যান্ডলিংঅনিমেশন পরেও পুরানো পজিশনে ক্লিক হয়নতুন পজিশনে ক্লিক কাজ করে
পারফরম্যান্সকমউচ্চ (GPU অ্যাক্সিলারেশন)

XML-এ অ্যানিমেশন সংজ্ঞায়িত করা (res/anim/fade_in.xml)

<?xml version="1.0" encoding="utf-8"?>
<alpha xmlns:android="https://schemas.android.com/apk/res/android"
    android:duration="1000"
    android:fromAlpha="0.0"
    android:toAlpha="1.0" />
// জাভাতে লোড করুন
Animation fadeIn = AnimationUtils.loadAnimation(this, R.anim.fade_in);
imageView.startAnimation(fadeIn);

সেরা অনুশীলন

  • নতুন প্রজেক্টে ObjectAnimator/Property Animation ব্যবহার করুন
  • AnimatorSet ব্যবহার করে একসাথে একাধিক অ্যানিমেশন চালান
  • android:animateLayoutChanges="true" লেআউটে দিয়ে অটো অ্যানিমেশন পান
  1. অ্যান্ড্রয়েড স্টুডিওতে স্ট্যাটিক ভেরিয়েবল কোথায় এবং কীভাবে ব্যবহার করবেন

  2. অ্যান্ড্রয়েডে ড্র্যাগ অ্যান্ড ড্রপ ইমপ্লিমেন্টেশন

  3. অ্যান্ড্রয়েডে মাউস এবং কীবোর্ড ব্যবহার করার পদ্ধতি

  4. অ্যান্ড্রয়েড টিভিতে ADB সেট আপ এবং ব্যবহার করার পদ্ধতি