Glide: দ্রুত ও কার্যকর ইমেজ লোডিং লাইব্রেরি
Glide হলো Google-র রিকমেন্ডেড ইমেজ লোডিং লাইব্রেরি যা ক্যাশিং, রিসাইজিং, ট্রান্সফরমেশন এবং লাইফসাইকেল-অয়ের লোডিং সাপোর্ট করে।
ডিপেন্ডেন্সি যোগ করুন (build.gradle)
dependencies {
// Glide Core
implementation 'com.github.bumptech.glide:glide:4.16.0'
// Annotation Processor (Req for generated API)
annotationProcessor 'com.github.bumptech.glide:compiler:4.16.0'
// Optional: OkHttp Integration
implementation 'com.github.bumptech.glide:okhttp3-integration:4.16.0'
}
ধাপ ১: লেআউট (activity_main.xml)
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="16dp">
<Button
android:id="@+id/btnDrawable"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_centerVertical="true"
android:text="Load Drawable" />
<ImageView
android:id="@+id/imageView"
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_centerHorizontal="true"
android:scaleType="centerCrop"
android:src="@mipmap/ic_launcher" />
<Button
android:id="@+id/btnUrl"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_centerVertical="true"
android:text="Load URL" />
<Button
android:id="@+id/btnPlaceholder"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/btnDrawable"
android:layout_alignParentStart="true"
android:layout_marginTop="12dp"
android:text="Placeholder" />
<Button
android:id="@+id/btnError"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/btnUrl"
android:layout_alignParentEnd="true"
android:layout_marginTop="12dp"
android:text="Error Handling" />
<Button
android:id="@+id/btnResize"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/btnPlaceholder"
android:layout_alignParentStart="true"
android:layout_marginTop="12dp"
android:text="Resize" />
<Button
android:id="@+id/btnRotate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/btnError"
android:layout_alignParentEnd="true"
android:layout_marginTop="12dp"
android:text="Rotate" />
</RelativeLayout>
ধাপ ২: MainActivity.java
package com.example.glidedemo;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import androidx.appcompat.app.AppCompatActivity;
import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
ImageView imageView;
Button btnDrawable, btnUrl, btnPlaceholder, btnError, btnResize, btnRotate;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initViews();
}
private void initViews() {
imageView = findViewById(R.id.imageView);
btnDrawable = findViewById(R.id.btnDrawable);
btnUrl = findViewById(R.id.btnUrl);
btnPlaceholder = findViewById(R.id.btnPlaceholder);
btnError = findViewById(R.id.btnError);
btnResize = findViewById(R.id.btnResize);
btnRotate = findViewById(R.id.btnRotate);
btnDrawable.setOnClickListener(this);
btnUrl.setOnClickListener(this);
btnPlaceholder.setOnClickListener(this);
btnError.setOnClickListener(this);
btnResize.setOnClickListener(this);
btnRotate.setOnClickListener(this);
}
@Override
public void onClick(View view) {
int id = view.getId();
if (id == R.id.btnDrawable) {
// Drawable Resource
Glide.with(this).load(R.drawable.your_image).into(imageView);
} else if (id == R.id.btnUrl) {
// Network Image
Glide.with(this)
.load("https://example.com/image.jpg")
.placeholder(R.drawable.placeholder)
.into(imageView);
} else if (id == R.id.btnPlaceholder) {
// Placeholder Only (Invalid URL)
Glide.with(this)
.load("invalid_url")
.placeholder(R.drawable.placeholder)
.into(imageView);
} else if (id == R.id.btnError) {
// Error Placeholder
Glide.with(this)
.load("invalid_url")
.placeholder(R.drawable.placeholder)
.error(R.drawable.error_image)
.into(imageView);
} else if (id == R.id.btnResize) {
// Resize with RequestOptions
Glide.with(this)
.load(R.drawable.your_image)
.apply(new RequestOptions().override(200, 200))
.into(imageView);
} else if (id == R.id.btnRotate) {
// Rotation (Glide 4.x-এ Transformations ব্যবহার করুন)
Glide.with(this)
.load(R.drawable.your_image)
.apply(new RequestOptions().transform(new Rotate(90)))
.into(imageView);
}
}
}
গুরুত্বপূর্ণ: Custom Transformation (Rotate)
import com.bumptech.glide.load.resource.bitmap.TransformationUtils;
import com.bumptech.glide.load.engine.bitmap_recycle.BitmapPool;
import java.security.MessageDigest;
public class Rotate extends com.bumptech.glide.load.Key {
private final int degrees;
public Rotate(int degrees) { this.degrees = degrees; }
@Override
public void updateDiskCacheKey(MessageDigest messageDigest) {
messageDigest.update(("rotate_" + degrees).getBytes(CHARSET));
}
// Implement Transformation interface for actual rotation
// Better: Use built-in .transform(new Rotate(90)) with Glide 4.16+
}
নোট: Glide 4.x-এ
RequestOptions.transform()ব্যবহার করুন।bitmapTransform()ডিপ্রিকেটেড।
Glide এর প্রধান ফিচারস
| ফিচার | মেথড/সিনট্যাক্স |
|---|---|
| Basic Load | Glide.with(context).load(url).into(imageView) |
| Placeholder | .placeholder(R.drawable.placeholder) |
| Error Placeholder | .error(R.drawable.error) |
| Fallback (Null URL) | .fallback(R.drawable.fallback) |
| Resize | .apply(new RequestOptions().override(w, h)) |
| Center Crop | .apply(new RequestOptions().centerCrop()) |
| Circle Crop | .apply(RequestOptions.circleCropTransform()) |
| Rounded Corners | .apply(RequestOptions.bitmapTransform(new RoundedCorners(16))) |
| GIF Support | অটোমেটিক (as GifDrawable) |
| Thumbnail | .thumbnail(Glide.with(...).load(smallUrl)) |
| Disk Cache Strategy | .apply(new RequestOptions().diskCacheStrategy(DiskCacheStrategy.ALL)) |
| Skip Memory Cache | .apply(new RequestOptions().skipMemoryCache(true)) |
| Listener/Callback | .listener(new RequestListener<Drawable>() {...}) |
Glide vs Picasso vs Coil
| ফিচার | Glide | Picasso | Coil (Kotlin) |
|---|---|---|---|
| GIF Support | ✅ Native | ❌ Limited | ✅ Native |
| Video Thumbnail | ✅ | ❌ | ✅ |
| Kotlin API | ✅ Extension | ⚠️ Wrapper | ✅ Native |
| Size (Method Count) | ~4700 | ~850 | ~2000 |
| Performance | Excellent | Good | Excellent |
সুপারিশ: নতুন প্রজেক্টে Coil (Kotlin-first) বা Glide ব্যবহার করুন। Picasso মেইনটেনেন্স মোডে।