ObjectAnimator ব্যবহার করে View-এর Alpha অ্যানিমেট করা
এই উদাহরণে দেখানো হয়েছে কিভাবে ObjectAnimator.ofFloat(view, "alpha", value) ব্যবহার করে একটি TextView-এর অপাসিটি (Opacity) ০.৬ (৬০%) তে আনার অ্যানিমেশন করা যায়। বাটন ক্লিক করলে অ্যানিমেশন শুরু হয়।
ধাপ ১: প্রজেক্ট তৈরি করুন
Android Studio-তে নতুন প্রজেক্ট শুরু করুন।
ধাপ ২: লেআউট (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"
tools:context=".MainActivity">
<TextView
android:id="@+id/textView1"
android:layout_width="match_parent"
android:layout_height="100dp"
android:layout_centerHorizontal="true"
android:layout_marginTop="86dp"
android:background="#011ffd"
android:gravity="center"
android:text="TextView"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#FFFFFF" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/textView1"
android:layout_centerHorizontal="true"
android:layout_marginTop="48dp"
android:text="অপাসিটি সেট করুন (৬০%)" />
</RelativeLayout>
ধাপ ৩: MainActivity.java
package com.example.sample;
import android.animation.ObjectAnimator;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
TextView textView;
Button button;
ObjectAnimator animator;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = findViewById(R.id.textView1);
button = findViewById(R.id.button1);
// "alpha" প্রপার্টি ০.৬ (৬০% অপাসিটি) এ অ্যানিমেট করুন
animator = ObjectAnimator.ofFloat(textView, "alpha", 0.6f);
animator.setDuration(4000); // ৪ সেকেন্ডে অ্যানিমেশন
button.setOnClickListener(v -> animator.start());
}
}
অন্য পদ্ধতিগুলো
১. সেট করা (বিনা অ্যানিমেশন)
// XML-এ android:alpha="0.6" // Java-এ view.setAlpha(0.6f);
২. পুরানো API (API 11-নিচে) - AlphaAnimation
import android.view.animation.AlphaAnimation; AlphaAnimation alpha = new AlphaAnimation(1.0f, 0.6f); alpha.setDuration(4000); alpha.setFillAfter(true); view.startAnimation(alpha);
অপাসিটি ভ্যালু রেঞ্জ
1.0f= ১০০% দৃশ্যমান (ডিফল্ট)0.5f= ৫০% পারদর্শক0.0f= সম্পূর্ণ অদৃশ্য (লেআউট জায়গা দখল করে)
নোট: View.GONE হলে লেআউটে জায়গা নেয় না, alpha=0 হলে জায়গা নেয়।
ফলাফল
বাটনে ট্যাপ করলে নীল TextView ৪ সেকেন্ড ধরে ধীরে ধীরে ৬০% অপাসিটিতে আসবে।