Skip to content

Instantly share code, notes, and snippets.

@nseidm1
Created March 4, 2014 04:37
Show Gist options
  • Save nseidm1/9340378 to your computer and use it in GitHub Desktop.
Save nseidm1/9340378 to your computer and use it in GitHub Desktop.
Custom image view that accepts a gif loaded by the setGif method taking a File. Gif decoding from: GIF support comes from Code Aurora Forums: https://www.codeaurora.org/cgit/quic/la/platform/packages/apps/Gallery2/tree/src/com/android/gallery3d/util?h=jb_chocolate_rb4.1&id=2b133c9747af26701a12d60caef3e7e14cf55536
package com.project.vegassms.classes;
import java.io.File;
import java.io.FileInputStream;
import java.util.Observable;
import java.util.Observer;
import android.content.Context;
import android.graphics.Bitmap;
import android.os.Handler;
import android.os.Looper;
import android.util.AttributeSet;
import android.widget.ImageView;
import com.concentriclivers.mms.com.android.mms.ui.managers.ExecutorManager;
import com.google.common.io.ByteStreams;
import com.koushikdutta.ion.gif.GifAction;
import com.koushikdutta.ion.gif.GifDecoder;
import com.project.vegassms.R;
import com.project.vegassms.helpers.MessageBus;
import com.project.vegassms.helpers.MessageBus.Command;
public class LayoutOverrideImageView extends ImageView implements Observer {
private Bitmap[] bitmaps = new Bitmap[0];
private int[] delays = new int[0];
private int index = 0;
private boolean playing = false;
private boolean attached = false;
private boolean pause = false;
private File currentlyDisplayedFile = new File("");
private Handler handler = new Handler(Looper.getMainLooper());
public LayoutOverrideImageView(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
public void requestLayout() {
}
@Override
protected void onAttachedToWindow() {
super.onAttachedToWindow();
MessageBus.sInstance.addObserver(this);
attached = true;
}
@Override
protected void onDetachedFromWindow() {
super.onDetachedFromWindow();
MessageBus.sInstance.deleteObserver(this);
attached = false;
stopPlaying();
}
public void stopPlaying() {
playing = false;
handler.removeCallbacks(gifPlayer);
}
public void startPlaying() {
stopPlaying();
playing = true;
handler.post(gifPlayer);
}
public void pause() {
pause = true;
}
public void resume() {
pause = false;
if (playing)
startPlaying();
}
public void setGif(final File gifFile) {
if (currentlyDisplayedFile.getAbsolutePath().equals(gifFile.getAbsolutePath())) {
startPlaying();
return;
}
setImageResource(R.drawable.transparent);
ExecutorManager.sInstance.mExecutor.execute(new Runnable() {
@Override
public void run() {
try {
currentlyDisplayedFile = gifFile;
byte[] image = ByteStreams.toByteArray(new FileInputStream(gifFile));
GifDecoder decoder = new GifDecoder(image, 0, image.length, new GifAction() {
@Override
public boolean parseOk(boolean parseStatus, int frameIndex) {
return true;
}
});
decoder.run();
bitmaps = new Bitmap[decoder.getFrameCount()];
delays = decoder.getDelays();
for (int i = 0; i < decoder.getFrameCount(); i++)
bitmaps[i] = decoder.getFrameImage(i);
startPlaying();
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
private Runnable gifPlayer = new Runnable() {
@Override
public void run() {
if (!attached || pause || !playing)
return;
if (index < bitmaps.length)
setImageBitmap(bitmaps[index]);
if (playing && index < delays.length)
handler.postDelayed(this, delays[index]);
if (index >= bitmaps.length - 1) {
index = 0;
} else {
index++;
}
}
};
@Override
public void update(Observable observable, Object data) {
Command command = (Command) data;
switch (command.mCommand) {
case Command.START_GIF:
resume();
break;
case Command.STOP_GIF:
pause();
break;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment