Skip to content

Commit

Permalink
Add glide plugin support for avif
Browse files Browse the repository at this point in the history
  • Loading branch information
penfeizhou authored and jingpeng committed Sep 12, 2023
1 parent 34135b5 commit 0d3c268
Show file tree
Hide file tree
Showing 9 changed files with 75 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import android.app.Activity;
import android.os.Bundle;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;

Expand All @@ -20,6 +21,9 @@ protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.activity_apnglib);
LinearLayout linearLayout = findViewById(R.id.layout);
String[] urls = new String[]{
"file:///android_asset/test.avif",
"file:///android_asset/wheel.avif",
"file:///android_asset/world-cup.avif",
"file:///android_asset/apng_detail_guide.png",
"file:///android_asset/1.gif",
"file:///android_asset/2.gif",
Expand All @@ -31,13 +35,13 @@ protected void onCreate(Bundle savedInstanceState) {
};
for (String url : urls) {
ImageView imageView = new ImageView(this);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(500, 500);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
layoutParams.bottomMargin = 50;
layoutParams.topMargin = 50;
linearLayout.addView(imageView, layoutParams);
Glide.with(imageView)
.load(url)
.set(AnimationDecoderOption.NO_ANIMATION_BOUNDS_MEASURE, true)
// .set(AnimationDecoderOption.NO_ANIMATION_BOUNDS_MEASURE, true)
.into(imageView);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ protected int getLoopCount() {
protected void release() {
if (avifDecoder != null) {
avifDecoder.release();
avifDecoder = null;
}
}

Expand All @@ -71,12 +72,20 @@ protected Rect read(AVIFReader reader) throws IOException {

@Override
public int getFrameCount() {
if (avifDecoder == null) {
return 0;
}
return avifDecoder.getFrameCount();
}

@Override
public Bitmap getFrameBitmap(int index) throws IOException {
return super.getFrameBitmap(index);
if (avifDecoder == null) {
return null;
}
Bitmap bitmap = Bitmap.createBitmap(getBounds().width() / getSampleSize(), getBounds().height() / getSampleSize(), Bitmap.Config.ARGB_8888);
avifDecoder.nthFrame(index, bitmap);
return bitmap;
}

@Override
Expand All @@ -103,7 +112,16 @@ protected void renderFrame(Frame<AVIFReader, AVIFWriter> frame) {
}
}
frameBuffer.rewind();
bitmap.copyPixelsToBuffer(frameBuffer);
try {
bitmap.copyPixelsToBuffer(frameBuffer);
} catch (Exception e) {
e.printStackTrace();
}
recycleBitmap(bitmap);
}

@Override
public int getSampleSize() {
return 1;
}
}
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ if (needReplaceLocal) {
substitute module('com.github.penfeizhou.android.animation:gif') using project(":gif")
substitute module('com.github.penfeizhou.android.animation:glide-plugin') using project(":plugin_glide")
substitute module('com.github.penfeizhou.android.animation:awebpencoder') using project(":awebpencoder")
substitute module('com.github.penfeizhou.android.animation:avif') using project(":avif")
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ public void run() {

private void initCanvasBounds(Rect rect) {
fullRect = rect;
frameBuffer = ByteBuffer.allocate((rect.width() * rect.height() / (sampleSize * sampleSize) + 1) * 4);
frameBuffer = ByteBuffer.allocate((rect.width() * rect.height() / (getSampleSize() * getSampleSize()) + 1) * 4);
if (mWriter == null) {
mWriter = getWriter();
}
Expand Down Expand Up @@ -438,7 +438,7 @@ public int getSampleSize() {
public boolean setDesiredSize(int width, int height) {
boolean sampleSizeChanged = false;
final int sample = getDesiredSample(width, height);
if (sample != this.sampleSize) {
if (sample != getSampleSize()) {
sampleSizeChanged = true;
final boolean tempRunning = isRunning();
workerHandler.removeCallbacks(renderTask);
Expand Down
1 change: 1 addition & 0 deletions plugin_glide/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ dependencies {
api("com.github.penfeizhou.android.animation:awebp:${rootProject.ext.Version}")
api("com.github.penfeizhou.android.animation:apng:${rootProject.ext.Version}")
api("com.github.penfeizhou.android.animation:gif:${rootProject.ext.Version}")
api("com.github.penfeizhou.android.animation:avif:${rootProject.ext.Version}")

testImplementation 'junit:junit:4.13'
androidTestImplementation 'androidx.test.ext:junit:1.1.2'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,13 @@ public final class AnimationDecoderOption {
"com.github.penfeizhou.animation.glide.AnimationDecoderOption.DISABLE_ANIMATION_BOUNDS_MEASURE", false);


/**
* If set to {@code true}, disables the Frame Animation Decoder {@link com.github.penfeizhou.animation.avif.AVIFDrawable}
* Defaults to {@code false}.
*/
public static final Option<Boolean> DISABLE_ANIMATION_AVIF_DECODER = Option.memory(
"com.github.penfeizhou.animation.glide.AnimationDecoderOption.DISABLE_AVIF_DECODER", false);

private AnimationDecoderOption() {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import com.bumptech.glide.load.engine.Resource;
import com.github.penfeizhou.animation.apng.decode.APNGDecoder;
import com.github.penfeizhou.animation.apng.decode.APNGParser;
import com.github.penfeizhou.animation.avif.decode.AVIFDecoder;
import com.github.penfeizhou.animation.avif.decode.AVIFParser;
import com.github.penfeizhou.animation.decode.FrameSeqDecoder;
import com.github.penfeizhou.animation.gif.decode.GifDecoder;
import com.github.penfeizhou.animation.gif.decode.GifParser;
Expand All @@ -31,7 +33,8 @@ public class ByteBufferAnimationDecoder implements ResourceDecoder<ByteBuffer, F
public boolean handles(@NonNull ByteBuffer source, @NonNull Options options) {
return (!options.get(AnimationDecoderOption.DISABLE_ANIMATION_WEBP_DECODER) && WebPParser.isAWebP(new ByteBufferReader(source)))
|| (!options.get(AnimationDecoderOption.DISABLE_ANIMATION_APNG_DECODER) && APNGParser.isAPNG(new ByteBufferReader(source)))
|| (!options.get(AnimationDecoderOption.DISABLE_ANIMATION_GIF_DECODER) && GifParser.isGif(new ByteBufferReader(source)));
|| (!options.get(AnimationDecoderOption.DISABLE_ANIMATION_GIF_DECODER) && GifParser.isGif(new ByteBufferReader(source)))
|| (!options.get(AnimationDecoderOption.DISABLE_ANIMATION_AVIF_DECODER) && AVIFParser.isAVIF(new ByteBufferReader(source)));
}

@Nullable
Expand All @@ -51,6 +54,8 @@ public ByteBuffer getByteBuffer() {
decoder = new APNGDecoder(loader, null);
} else if (GifParser.isGif(new ByteBufferReader(source))) {
decoder = new GifDecoder(loader, null);
} else if (AVIFParser.isAVIF(new ByteBufferReader(source))) {
decoder = new AVIFDecoder(loader, null);
} else {
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
import com.bumptech.glide.util.Util;
import com.github.penfeizhou.animation.apng.APNGDrawable;
import com.github.penfeizhou.animation.apng.decode.APNGDecoder;
import com.github.penfeizhou.animation.avif.AVIFDrawable;
import com.github.penfeizhou.animation.avif.decode.AVIFDecoder;
import com.github.penfeizhou.animation.decode.FrameSeqDecoder;
import com.github.penfeizhou.animation.gif.GifDrawable;
import com.github.penfeizhou.animation.gif.decode.GifDecoder;
Expand Down Expand Up @@ -103,6 +105,32 @@ public void recycle() {
gifDrawable.stop();
}

@Override
public void initialize() {
super.initialize();
}
};
} else if (frameSeqDecoder instanceof AVIFDecoder) {
final AVIFDrawable avifDrawable = new AVIFDrawable((AVIFDecoder) frameSeqDecoder);
avifDrawable.setAutoPlay(false);
avifDrawable.setNoMeasure(noMeasure);
return new DrawableResource<Drawable>(avifDrawable) {
@NonNull
@Override
public Class<Drawable> getResourceClass() {
return Drawable.class;
}

@Override
public int getSize() {
return avifDrawable.getMemorySize();
}

@Override
public void recycle() {
avifDrawable.stop();
}

@Override
public void initialize() {
super.initialize();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@
import com.bumptech.glide.load.ResourceDecoder;
import com.bumptech.glide.load.engine.Resource;
import com.github.penfeizhou.animation.apng.decode.APNGParser;
import com.github.penfeizhou.animation.avif.decode.AVIFParser;
import com.github.penfeizhou.animation.decode.FrameSeqDecoder;
import com.github.penfeizhou.animation.gif.decode.GifParser;
import com.github.penfeizhou.animation.io.ByteBufferReader;
import com.github.penfeizhou.animation.io.StreamReader;
import com.github.penfeizhou.animation.webp.decode.WebPParser;

Expand All @@ -34,7 +36,8 @@ public StreamAnimationDecoder(ResourceDecoder<ByteBuffer, FrameSeqDecoder> byteB
public boolean handles(@NonNull InputStream source, @NonNull Options options) {
return (!options.get(AnimationDecoderOption.DISABLE_ANIMATION_WEBP_DECODER) && WebPParser.isAWebP(new StreamReader(source)))
|| (!options.get(AnimationDecoderOption.DISABLE_ANIMATION_APNG_DECODER) && APNGParser.isAPNG(new StreamReader(source)))
|| (!options.get(AnimationDecoderOption.DISABLE_ANIMATION_GIF_DECODER) && GifParser.isGif(new StreamReader(source)));
|| (!options.get(AnimationDecoderOption.DISABLE_ANIMATION_GIF_DECODER) && GifParser.isGif(new StreamReader(source)))
|| (!options.get(AnimationDecoderOption.DISABLE_ANIMATION_AVIF_DECODER) && AVIFParser.isAVIF(new StreamReader(source)));
}

@Nullable
Expand Down

0 comments on commit 0d3c268

Please sign in to comment.