Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> Android

Cách hiển thị ảnh GIF animation trong Android dùng Glide

Ví dụ này minh họa cách hiển thị ảnh GIF động trong Android sử dụng thư viện Glide (phổ biến, nhẹ, hỗ trợ GIF tốt).

Bước 1: Tạo dự án mới

Tạo dự án mới trong Android Studio.

Bước 2: Thêm dependency Glide

Trong build.gradle (Module: app):

dependencies {
    implementation 'com.github.bumptech.glide:glide:4.16.0'
    annotationProcessor 'com.github.bumptech.glide:compiler:4.16.0'
}

Sync project sau khi thêm.

Bước 3: Cấu hình layout (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:layout_above="@id/btnShowGif"
        android:scaleType="centerCrop" />
    <Button
        android:id="@+id/btnShowGif"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:onClick="showGif"
        android:text="HIỂN THỊ GIF" />
</RelativeLayout>

Bước 4: Code MainActivity (src/MainActivity.java)

package com.app.sample;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import com.bumptech.glide.Glide;

public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
    
    public void showGif(View view) {
        ImageView imageView = findViewById(R.id.imageView);
        // Load GIF từ drawable (đặt file .gif vào res/drawable)
        Glide.with(this)
             .asGif() // Ép load as GIF
             .load(R.drawable.tenor) // File gif tên tenor.gif trong drawable
             .into(imageView);
    }
}

Bước 5: Cấu hình Manifest

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
    package="app.com.sample">
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>

Chạy ứng dụng

Đặt file GIF vào res/drawable/, chạy app và nhấn nút "HIỂN THỊ GIF".

Cách hiển thị ảnh GIF animation trong Android dùng Glide