实现Webp相关类仿照Glide中的Gif,参考GlideWebpSupport
1、WebpFrameLoader,加载每帧图⽚
aphics.Bitmap;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.os.SystemClock;
import com.bumptech.glide.Glide;
import com.bumptech.glide.RequestBuilder;
import com.bumptech.glide.RequestManager;
import com.bumptech.glide.gifdecoder.GifDecoder;
import com.bumptech.glide.load.Key;
import com.bumptech.glide.load.Transformation;
import com.bumptech.ine.DiskCacheStrategy;
import com.bumptech.ine.bitmap_recycle.BitmapPool;
import com.quest.RequestOptions;
import com.quest.target.SimpleTarget;
import com.ansition.Transition;
import com.bumptech.glide.util.Preconditions;
import com.bumptech.glide.util.Util;
import java.nio.ByteBuffer;
import java.security.MessageDigest;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
public class WebpFrameLoader {
final RequestManager requestManager;
private final GifDecoder gifDecoder;
private final Handler handler;
private final List<FrameCallback> callbacks;
private final BitmapPool bitmapPool;
private boolean isRunning;
private boolean isLoadPending;
private boolean startFromFirstFrame;
private RequestBuilder<Bitmap> requestBuilder;
private DelayTarget current;
private boolean isCleared;
private DelayTarget next;
private Bitmap firstFrame;
private Transformation<Bitmap> transformation;
public WebpFrameLoader(Glide glide, GifDecoder gifDecoder, int width, int height, Transformation<Bitmap> transformation, Bitmap firstFrame) {
BitmapPool(), Glide.Context()), gifDecoder, null, getRequestBuilder(Glide.Context()), width, height), transformation, firstFrame);    }
svg canvasWebpFrameLoader(BitmapPool bitmapPool, RequestManager requestManager, GifDecoder gifDeco
der, Handler handler, RequestBuilder<Bitmap> requestBuilder, Trans        this.callbacks = new ArrayList();
this.isRunning = false;
this.isLoadPending = false;
this.startFromFirstFrame = false;
if (handler == null) {
handler = new MainLooper(), new FrameLoaderCallback());
}
this.bitmapPool = bitmapPool;
this.handler = handler;
this.gifDecoder = gifDecoder;
this.setFrameTransformation(transformation, firstFrame);
}
private static RequestBuilder<Bitmap> getRequestBuilder(RequestManager requestManager, int width, int height) {
return requestManager.asBitmap().apply(RequestOptions.diskCacheStrategyOf(DiskCacheStrategy.NONE).skipMemoryCache(true).override(width, height));
}
void setFrameTransformation(Transformation<Bitmap> transformation, Bitmap firstFrame) {
this.firstFrame = Preconditions.checkNotNull(firstFrame);
}
Transformation<Bitmap> getFrameTransformation() {
ansformation;
}
Bitmap getFirstFrame() {
return this.firstFrame;
}
void subscribe(FrameCallback frameCallback) {
if (this.isCleared) {
throw new IllegalStateException("Cannot subscribe to a cleared frame loader");
} else {
boolean start = this.callbacks.isEmpty();
if (ains(frameCallback)) {
throw new IllegalStateException("Cannot subscribe twice in a row");
} else {
this.callbacks.add(frameCallback);
if (start) {
this.start();
}
}
}
}
void unsubscribe(FrameCallback frameCallback) {
ve(frameCallback);
if (this.callbacks.isEmpty()) {
this.stop();
}
}
int getWidth() {
CurrentFrame().getWidth();
}
int getHeight() {
CurrentFrame().getHeight();
}
int getSize() {
return ByteSize() + FrameSize();
}
int getCurrentIndex() {
return this.current != null ? this.current.index : -1;
}
private int getFrameSize() {
CurrentFrame().getWidth(), CurrentFrame().getHeight(), CurrentFrame().getConfig());    }
ByteBuffer getBuffer() {
return Data().asReadOnlyBuffer();
}
int getFrameCount() {
return FrameCount();
}
int getLoopCount() {
return TotalIterationCount();
}
private void start() {
if (!this.isRunning) {
this.isRunning = true;
this.isCleared = false;
this.loadNextFrame();
}
}
private void stop() {
this.isRunning = false;
}
void clear() {
this.callbacks.clear();
this.stop();
if (this.current != null) {
this.current = null;
}
if ( != null) {
< = null;
}
this.gifDecoder.clear();
this.isCleared = true;
}
Bitmap getCurrentFrame() {
return this.current != null ? Resource() : this.firstFrame;
}
private void loadNextFrame() {
if (this.isRunning && !this.isLoadPending) {
if (this.startFromFirstFrame) {
setFrameIndex();
this.startFromFirstFrame = false;
}
this.isLoadPending = true;
int delay = NextDelay();
long targetTime = SystemClock.uptimeMillis() + (long) delay;
this.gifDecoder.advance();
< = new DelayTarget(this.handler, CurrentFrameIndex(), targetTime);
}
private void recycleFirstFrame() {
if (this.firstFrame != null) {
this.bitmapPool.put(this.firstFrame);
this.firstFrame = null;
}
}
void setNextStartFromFirstFrame() {
Preconditions.checkArgument(!this.isRunning, "Can\'t restart a running animation");
this.startFromFirstFrame = true;
}
void onFrameReady(DelayTarget delayTarget) {
if (this.isCleared) {
this.handler.obtainMessage(2, delayTarget).sendToTarget();
} else {
if (Resource() != null) {
DelayTarget previous = this.current;
this.current = delayTarget;
for (int i = this.callbacks.size() - 1; i >= 0; --i) {
FrameCallback cb = (i);

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。