AnimationDrawable এবং গ্রেডিয়েন্ট ব্যবহার করে ডাইনামিক রঙ পরিবর্তন
এই উদাহরণটি দেখায় কীভাবে XML-এ সংজ্ঞায়িত AnimationDrawable-কে রানটাইমে লোড করে রঙ অ্যানিমেশন চালানো যায়।
ধাপ ১: প্রজেক্ট তৈরি করুন
Android Studio-তে নতুন প্রজেক্ট।
ধাপ ২: লেআউট (activity_main.xml) - LinearLayout ব্যাকগ্রাউন্ড হিসেবে
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
xmlns:app="https://schemas.android.com/apk/res-auto"
xmlns:tools="https://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/background"
android:id="@+id/activity_main"
android:orientation="vertical"
tools:context=".MainActivity"
tools:ignore="InvalidId">
</LinearLayout>
ধাপ ৩: MainActivity.java - অ্যানিমেশন শুরু করুন
package com.app.sample;
import androidx.appcompat.app.AppCompatActivity;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.widget.LinearLayout;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
LinearLayout yourLayout = findViewById(R.id.activity_main);
AnimationDrawable animationDrawable = (AnimationDrawable) yourLayout.getBackground();
// ফেড ডিউরেশন সেট করুন
animationDrawable.setEnterFadeDuration(4000);
animationDrawable.setExitFadeDuration(4000);
// অ্যানিমেশন শুরু করুন
animationDrawable.start();
}
}
ধাপ ৪: ড্রয়েবল রিসোর্স ফাইলস
res/drawable/background.xml - অ্যানিমেশন লিস্ট
<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="https://schemas.android.com/apk/res/android">
<item
android:drawable="@drawable/gradient1"
android:duration="4000" />
<item
android:drawable="@drawable/gradient2"
android:duration="4000" />
</animation-list>
res/drawable/gradient1.xml - প্রথম গ্রেডিয়েন্ট
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="https://schemas.android.com/apk/res/android">
<gradient
android:angle="225"
android:endColor="#044fab"
android:startColor="#21d6d3" />
</shape>
res/drawable/gradient2.xml - দ্বিতীয় গ্রেডিয়েন্ট
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="https://schemas.android.com/apk/res/android">
<gradient
android:angle="45"
android:endColor="#933c94"
android:startColor="#517f95" />
</shape>
ধাপ ৫: ম্যানিফেস্ট
সাধারণ কনফিগারেশন।
রানটাইমে রঙ পরিবর্তন করার অন্যান্য পদ্ধতি
১. DrawableCompat.setTint() (API 19+)
import androidx.core.graphics.drawable.DrawableCompat; Drawable drawable = ContextCompat.getDrawable(context, R.drawable.ic_icon); Drawable wrapped = DrawableCompat.wrap(drawable); DrawableCompat.setTint(wrapped, Color.RED); imageView.setImageDrawable(wrapped);
২. ColorFilter ব্যবহার
// সিম্পল টিন্ট drawable.setColorFilter(Color.RED, PorterDuff.Mode.SRC_IN); // বা লাইটিং কলার ফিল্টার drawable.setColorFilter(new LightingColorFilter(Color.BLACK, Color.YELLOW));
৩. GradientDrawable প্রোগ্রামেটিক্যালি তৈরি
GradientDrawable gradient = new GradientDrawable(
GradientDrawable.Orientation.LEFT_RIGHT,
new int[]{Color.RED, Color.BLUE}
);
gradient.setCornerRadius(16f);
view.setBackground(gradient);
৪. VectorDrawable টিন্ট (সর্বোত্তম SVG-র জন্য)
<ImageView
android:src="@drawable/vector_icon"
android:tint="@color/primary" />
// বা কোডে
imageView.setImageTintList(ColorStateList.valueOf(Color.RED));