ফেড ইন/আউট অ্যানিমেশন
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" লেআউটে দিয়ে অটো অ্যানিমেশন পান