?
在A(yíng)ndroid中播放GIF動(dòng)畫(huà),動(dòng)畫(huà)可以使用第三方庫Glide或者Android原生的動(dòng)畫(huà)ImageView,這里以Glide為例,動(dòng)畫(huà)介紹如何在A(yíng)ndroid中?播放GIF動(dòng)畫(huà)。動(dòng)畫(huà)
(圖片來(lái)源網(wǎng)絡(luò ),動(dòng)畫(huà)侵刪)1、動(dòng)畫(huà)添加依賴(lài)
在項目的動(dòng)畫(huà)build.gradle文件中添加Glide依賴(lài):
dependencies { implementation 'com.gith(′▽?zhuān)?ub.bumptech.g??lide:glide:4.12.0' annotationProcessor 'com.github.bumptech.g┐(′ー`)┌l(fā)ide:compiler:4.12.0'}2、加載GIF圖片
使用G??lide加載GIF圖片:
import com.bumptech.glide.Glide;import com.bumptech.gli??de.load.engine.DiskCac(′▽?zhuān)?heStrategy;import com.??bum??ptech.glide.request.RequestOptions;// ...Glide.with(context) .load("https://example.com/path/to/your/gif/image.gif&qu??ot;) .apply(new RequestOptions() .diskCacheStrate(′?_?`)gy(DiskCacheStrate( ???)gy.ALL) // 緩存策略,動(dòng)畫(huà)可選 .dontAnimate() // 禁用默認的動(dòng)畫(huà)動(dòng)畫(huà)效果,因為我們要顯示GIF動(dòng)畫(huà) ) .into(imageView); // imageView是動(dòng)畫(huà)用于顯示GIF動(dòng)畫(huà)的ImageView對象3、播放GIF動(dòng)畫(huà)
由于Glide(′_`)默認不支持GIF動(dòng)畫(huà),動(dòng)畫(huà)我們需要自定義??一個(gè)GifDecod(╯°□°)╯︵ ┻━┻er來(lái)(′?_?`)解碼GIF動(dòng)畫(huà):
import android.graphics.Bitmap;import android.graphics.Canvas;import android.graphics.Colorヽ(′▽?zhuān)?ノ;import android.graphics.Matrix;import andro(′_`)id.graphics.Paint;import android.graphics.RectF;import android.os.Handler;import android.os.Message;import and(°o°)roidx.annotation.NonNull;import com.bumptech.glide.load.engine.bitmap_recycle.BitmapPool;import com.bum??ptech.glide.load.resource.bitmap.Bitmapヾ(^-^)ノTransformation;import java.io.InputStream;import java.util.ArrayList;imp??oヾ(′▽?zhuān)??rt java.u(′?`)til.List;public class GifDecoder im(′ω`)plements Bitm(//ω//)apTransformation { private static final int MSG_COMPLETE = 1; private static final int MSG_UPDATE_COVER = 2; private boolean isFirstFrame = true; // 是否是第一幀,用于計算寬高比和位置信息 private int frameWidth = 0; // 當前幀的寬度 private int frameHeight = 0; // 當前幀的高度 private int duration = 0; // GIF動(dòng)畫(huà)的持續時(shí)間(毫秒) private int currentFrameIndex = 0; // 當前幀的索引(從0開(kāi)始) private List<Bitm??ap> bitmaps = new ArrayList<>(); // 存儲(╯‵□′)╯所有幀的Bitmap列表 private RectF displayArea = new RectF(); // 顯示區域(矩形) private float scaleRatio = 1f; // 縮放比例,用于調整GIF的大小以適應ImageView的尺寸 private boolean isRunning = false; // 是否正在運行,用于控制循環(huán)播放和暫??停播放 private Handler handler = new Handler(loop='loop'er.getM(??-)?ainLooper()) { // Handle??r用于更新UI線(xiàn)程的消息隊列 @Override public void handleMessage(@NonNull Messa??ge msg) { switch (msg.what) { case MSG_COMPLETE: { // 處理完成消息(′?ω?`),將最后一幀添(′?`*)加到列表中并通知??主線(xiàn)(′_ゝ`)程更新UI界面 Bitm?ap lastFrame = (Bヾ(′?`)?itmap) ms??g.obj; bitmaps(╬ ò﹏ó).add(lastFrame); handler.sendEmptyMessage(MSG_UPDATE_COV(?????)ER); // 發(fā)送更新封面消息,以便在下一幀時(shí)顯示最后一幀的內容 break; } case MSG_UPDATE_COVER: { // 處理更新封面消息,更新ImageView??的內容為最ヽ(′ー`)ノ后一幀的Bitmap對象(如果存在)并重置當前幀索引和狀態(tài)信息等參數值以準備播放下一幀的GIF動(dòng)畫(huà)內容(如果需要的話(huà))... } } } } } } } } } } } } } } } } } } } } } } } } } } } } } ...??}; ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ..??. ... ... ... ...?? ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... { ...}...}...}...}...}...}...}...}...}.??..}...}...}...}...}...}...}...}...}...}...}...}...}??...}...}...}...}...}...{ ...}*/, isFirstFrame, frameWidth, frameHeight, duration??, curren(′▽?zhuān)?)tFrameIndex, bitmaps, displayArea, scaleRatio, isRunning);??*/import androidx.annotation.NonNull;public class GifD(′;ω;`)ecoder implements BitmapTransformation { private static final int MS(′?_?`)G_COMPLETE = 1;?? pri??vate static final int MSG_UPDATE_COVER = 2; private boolean isFirstFrame = true; private int frameWidth = 0; private int frameHeight = 0; private int duration = 0; priv??ate int currentFrameIndex = 0; private List<Bitmap> bitmaps = new ArrayList<>(); private RectF displayArea = new RectF(); private float scaleRatio = 1f; private boolean isRunning = false; private Handler handler = new Handler(looper.getMainLooper()) { @Override public void hand??le(°o°)Message(@NonNull Message msg) { switch (ms┐(′ー`)┌g.what) { case MSG_COMPLETE: { Bitmap lastFrame = (Bitmap) msg.(°□°)obj(′_ゝ`); bitmaps.add(lastFrame); handler(圖片來(lái)源網(wǎng)絡(luò ),侵刪)下面是一個(gè)簡(jiǎn)單(′▽?zhuān)?)的介紹,概述了在A(yíng)ndroid中播放GIF動(dòng)畫(huà)的相關(guān)信息:
| 組件/方法 | 描述 | 示例代碼 |
GifDrawable | 使用pl.droidsonroids.gif庫來(lái)加載和顯示GIF動(dòng)畫(huà)。 | GifDrawable gifDrawable = new GifDrawable(getResources(), R.drawable.animation); |
GifImageView | 同一庫提供的自定義視圖,專(zhuān)門(mén)用于顯示GIF動(dòng)畫(huà)。 | GifImageView gifImageView = fiヽ(′?`)ノndView??By??Id(R.id.gifImageView); |
Movie | Androiヽ(′ー`)ノd原生API,用于解析GIF文件,但顯示較為復雜,需(xu)要自己處理幀更新。 | Movie movie = Movie.decodeStream(getResources().open='open'Raw┐(′д`)┌Resource(R.raw.animation)); |
SurfaceView +Thread?? | 使用SurfaceView和后臺線(xiàn)程來(lái)手動(dòng)繪制GIF的每一幀。 | SurfaceView surfaceView = findViewById(R.id.surfaceView); |
AnimatedImageDrawable | 從Andヽ(′▽?zhuān)?ノroid API 28(Pie)開(kāi)始引入,用于高效地播放GIF和WebP動(dòng)畫(huà)。 | Animat(′ω`)e(╬?益?)dImageDrawable animatedImage = AnimatedImageDrawable.createFromRe??source??(getResources(), R.drawable.animation); |
Im?ageView | 通過(guò)設置Ani(╯°□°)╯matedImageDrawable或使用兼容庫來(lái)在ImageView上顯示GI??F。 | I??mageView imageView = findViewById(R.id.imageView); |
| 第三方庫 | 有許多第三方庫可以簡(jiǎn)化GIF動(dòng)畫(huà)的播放,如Glide或Picasso。 | Glide.with(context).load(R.drawable.animation).into(imageView);(′;ω;`) |
請注意,對于第三方庫,如Glide或Picasso,您需要在項目的build.gradle文件中添加相應的依賴(lài)項。
pl.droids??onroids.gif庫的使用示ヾ(′▽?zhuān)??例如下:
dependencies(?????) { implementa??tion 'pl.droidsonr??oids.gif:androidgifdrawable:1.2.19'}這個(gè)介紹提供了一個(gè)基本的指導,根據項目的需求和目標Android版本,開(kāi)發(fā)者可以選擇合適的方法來(lái)??播放GIF動(dòng)畫(huà)。
((′▽?zhuān)?)圖片來(lái)(′▽?zhuān)?源網(wǎng)絡(luò ),侵(′_ゝ`)刪)