यह उदाहरण दर्शाता है कि Android में Glide लाइब्रेरी का उपयोग करके एनिमेटेड GIF इमेज कैसे लोड और डिस्प्ले की जाती है।
चरण 1: नया प्रोजेक्ट बनाएं
Android Studio में File → New Project।
चरण 2: Glide निर्भरता जोड़ें
build.gradle (Module: app) में:
dependencies {
implementation 'com.github.bumptech.glide:glide:4.16.0'
annotationProcessor 'com.github.bumptech.glide:compiler:4.16.0'
}
नवीनतम वर्जन के लिए GitHub रिलीज चेक करें। सिंक करें।
चरण 3: लेआउट XML
res/layout/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"
android:padding="16dp"
tools:context=".MainActivity">
<ImageView
android:id="@+id/imageView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop"
android:contentDescription="GIF इमेज" />
<Button
android:id="@+id/btnShowGif"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:text="GIF दिखाएं"
android:layout_marginBottom="16dp" />
</RelativeLayout>
चरण 4: GIF फ़ाइल जोड़ें
अपनी GIF फ़ाइल (जैसे animation.gif) को res/drawable/ या res/raw/ फ़ोल्डर में रखें।
चरण 5: MainActivity.java
package com.app.sample;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import com.bumptech.glide.Glide;
public class MainActivity extends AppCompatActivity {
private ImageView imageView;
private Button btnShowGif;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageView = findViewById(R.id.imageView);
btnShowGif = findViewById(R.id.btnShowGif);
btnShowGif.setOnClickListener(v -> showGif());
}
private void showGif() {
// drawable से लोड करें
Glide.with(this)
.asGif()
.load(R.drawable.animation)
.into(imageView);
// या URL से:
// Glide.with(this).asGif().load("https://example.com/animation.gif").into(imageView);
}
}
चरण 6: AndroidManifest.xml
मानक मैनिफेस्ट। इंटरनेट परमिशन केवल URL से लोड करने पर आवश्यक।
वैकल्पिक: बिना लाइब्रेरी (API 28+)
// ImageView में सीधे सेट करें (एनिमेशन अपने आप चलेगा)
imageView.setImageResource(R.drawable.animation);
// या ImageDecoder से (API 28+)
ImageDecoder.decodeDrawable(ImageDecoder.createSource(resources, R.drawable.animation),
(decoder, info, source) -> decoder.setTargetSize(500, 500))
.also { imageView.setImageDrawable(it) };
ऐप चलाएं
डिवाइस कनेक्ट करें, Run करें। "GIF दिखाएं" बटन दबाकर एनिमेटेड GIF देखें।
