0

As the title elaborates I need to implement a way of showing gif images as a sequence in a single imageview container.

the things i have tried so far:

used glide library and found no luck with it since they are using some weird internal threading mechanism to animate gifs so making a sequence is not possible with it.

then i used android-gif-drawable library and found some piece of code but still couldn't manage it to work. below i have posted the class used to create this ChainedGifDrawable custom drawable. any help would be highly appreciated in this.

   public static class ChainedGifDrawable extends Drawable implements AnimationListener {
    private final pl.droidsonroids.gif.GifDrawable[] mDrawables;
    private int mCurrentIndex;

    public ChainedGifDrawable(List<pl.droidsonroids.gif.GifDrawable> drawables) {
        mDrawables = drawables.toArray(new pl.droidsonroids.gif.GifDrawable[0]);

        for (pl.droidsonroids.gif.GifDrawable drawable : drawables) {
            drawable.addAnimationListener(this);

        }
    }

    @Override
    public void onAnimationCompleted(int loopNumber) {
        Log.e("Called ","Checked");
        mCurrentIndex++;
        mCurrentIndex %= mDrawables.length;
        mDrawables[mCurrentIndex].setCallback(getCallback());
        mDrawables[mCurrentIndex].invalidateSelf();
    }

    @Override
    public void invalidateSelf() {
        mDrawables[mCurrentIndex].invalidateSelf();
        Log.e("Called invalidateSelf "+mCurrentIndex ,"Checked");
    }

    @Override
    public void scheduleSelf(@NonNull Runnable what, long when) {
        mDrawables[mCurrentIndex].scheduleSelf(what, when);
        Log.e("Called scheduleSelf "+mCurrentIndex ,"Checked");
    }

    @Override
    public void unscheduleSelf(@NonNull Runnable what) {
        mDrawables[mCurrentIndex].unscheduleSelf(what);
        Log.e("Called unscheduleSelf "+mCurrentIndex ,"Checked");
    }

    @Override
    public int getIntrinsicWidth() {
        Log.e("Called getIntrinsicWidth "+mCurrentIndex ,"Checked");
        return mDrawables[mCurrentIndex].getIntrinsicWidth();
    }

    @Override
    public int getIntrinsicHeight() {
        Log.e("Called getIntrinsicHeight "+mCurrentIndex ,"Checked");
        return mDrawables[mCurrentIndex].getIntrinsicHeight();
    }

    @Override
    protected void onBoundsChange(Rect bounds) {
        for (pl.droidsonroids.gif.GifDrawable drawable : mDrawables) {
            drawable.setBounds(bounds);
        }
        Log.e("Called onboundsChange "+mCurrentIndex ,"Checked");
    }


    @Override
    public void draw(@NonNull Canvas canvas) {
        mDrawables[mCurrentIndex].draw(canvas);
        Log.e("Called draw "+mCurrentIndex ,"Checked");
    }

    @Override
    public void setAlpha(@IntRange(from = 0, to = 255) int alpha) {
        for (pl.droidsonroids.gif.GifDrawable drawable : mDrawables) {
            drawable.setAlpha(alpha);
        }
        Log.e("Called setAlpha "+mCurrentIndex ,"Checked");
    }

    @Override
    public void setColorFilter(@Nullable ColorFilter colorFilter) {
        for (pl.droidsonroids.gif.GifDrawable drawable : mDrawables) {
            drawable.setColorFilter(colorFilter);
        }
        Log.e("Called setcolorfilter "+mCurrentIndex ,"Checked");
    }

    @Override
    public int getOpacity() {
        Log.e("Called getOpacity  "+mCurrentIndex ,"Checked");
        return PixelFormat.TRANSLUCENT;
    }

    public void setAnimationEnabled() {

        mDrawables[0].setCallback(getCallback());
    }
}
Chathu_sm
  • 85
  • 9
  • https://stackoverflow.com/questions/9494413/play-downloaded-gif-image-in-android – Goku Nov 15 '17 at 06:54
  • Thnx for the reply..but i know how to play a single gif file..i just need to play a sequence of gifs in one image view – Chathu_sm Nov 15 '17 at 07:16

1 Answers1

0

after wasting more than 2 days finally i could figure out a way to do this.It's not an efficient one but it works at least for now.I will post the code for anyone who might come across with a similar problem. Note: I have used android-gif-drawable library to make this work.

First I have Created an Array list with GifDrawable Objects and assigned eachone an AnimationListner.

   drawableList = new ArrayList<>();
            count = 0;
            for (j = 0; j < wordsActivity.displayItemsList.size(); j++) {
                Log.e("J value: ", String.valueOf(j));
                pl.droidsonroids.gif.GifDrawable firstDrawable = pl.droidsonroids.gif.GifDrawable.createFromResource(getResources(), wordsActivity.displayItemsList.get(j).getGifId());
                firstDrawable.setLoopCount(0);
                Transform transform = new CornerRadiusTransform(300);
                firstDrawable.setTransform(transform);
                firstDrawable.addAnimationListener(WordsGridViewPagerFragment.this);
                if (j == 0) {
                    wordsActivity.wordsImage.setImageDrawable(firstDrawable);
                }
                drawableList.add(firstDrawable);
            }

then in the implemented onAnimationCompleted method I have created the logic to move in to the next gif animation and keep repeat the task.

   @Override
public void onAnimationCompleted(int loopNumber) {
    Log.e("animation " + count, "completed");
    count++;
    if (count < drawableList.size()) {

        wordsActivity.wordsImage.setImageDrawable(drawableList.get(count));
        Log.e("animation " + count, "started");

    }else{
        count=0;
        wordsActivity.wordsImage.setImageDrawable(drawableList.get(0));

    }
}
Chathu_sm
  • 85
  • 9