提交 284bbcd0 编写于 作者: S Sam Judd

Use % to determine if GIF frame is transparent.

Fixes #214
上级 af30ddf8
......@@ -4,6 +4,7 @@ package com.bumptech.glide.gifencoder;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.util.Log;
import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
......@@ -35,6 +36,10 @@ import java.io.OutputStream;
*/
public class AnimatedGifEncoder {
private static final String TAG = "AnimatedGifEncoder";
// The minimum % of an images pixels that must be transparent for us to set a transparent index automatically.
private static final double MIN_TRANSPARENT_PERCENTAGE = 4d;
private int width; // image size
......@@ -384,14 +389,23 @@ public class AnimatedGifEncoder {
int pixelsIndex = 0;
hasTransparentPixels = false;
int totalTransparentPixels = 0;
for (final int pixel : pixelsInt) {
if (pixel == Color.TRANSPARENT) {
hasTransparentPixels = true;
totalTransparentPixels++;
}
pixels[pixelsIndex++] = (byte) (pixel & 0xFF);
pixels[pixelsIndex++] = (byte) ((pixel >> 8) & 0xFF);
pixels[pixelsIndex++] = (byte) ((pixel >> 16) & 0xFF);
}
double transparentPercentage = 100 * totalTransparentPixels / (double) pixelsInt.length;
// Assume images with greater where more than n% of the pixels are transparent actually have transparency.
// See issue #214.
hasTransparentPixels = transparentPercentage > MIN_TRANSPARENT_PERCENTAGE;
if (Log.isLoggable(TAG, Log.DEBUG)) {
Log.d(TAG, "got pixels for frame with " + transparentPercentage + "% transparent pixels");
}
}
/**
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册