2

I am using the following code to play the gif, but here it keeps on repeating the gif play.

Uri uri;
DraweeController controller = Fresco.newDraweeControllerBuilder()
    .setUri(uri)
    .setAutoPlayAnimations(true)
    . // other setters
    .build();
mSimpleDraweeView.setController(controller);

How can i play it once?

Fahim
  • 12,198
  • 5
  • 39
  • 57

4 Answers4

7

Well, I have not used fresco for GIF file but might be this one could help you out. Use below library for GIF file loading.
Refer this link
This is my code, and it works well:

public class MainActivity extends AppCompatActivity {

private GifImageView mGigImageView;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

     mGigImageView = (GifImageView) findViewById(R.id.mgif);

     GifDrawable gifDrawable = null;
     try {
          gifDrawable = new GifDrawable(getResources(), R.drawable.ani_1);
          gifDrawable.setLoopCount(1);
         } catch (IOException e) {
          e.printStackTrace();
          }
     mGigImageView.setImageDrawable(gifDrawable);
    }
}
Ravindra Kushwaha
  • 7,846
  • 14
  • 53
  • 103
Bhavnik
  • 2,020
  • 14
  • 21
1

You can use the Glide for GIF..It is better library.

Glide is quite similar to Picasso but this is much faster than Picasso. Glide consumes less memory than Picasso.

What that Glide has but Picasso doesn't An ability to load GIF Animation to a simple ImageView might be the most interesting feature of Glide. And yes, you can't do that with Picasso.

Some important links-
1. https://github.com/bumptech/glide
2. http://inthecheesefactory.com/blog/get-to-know-glide-recommended-by-google/en

You can also use the Ion library to load the gif.
Refer this link click here

Gradle dependencies for it:-

dependencies {
    ...

    compile 'com.android.support:support-v4:23.1.0'
    compile 'com.koushikdutta.ion:ion:2.+'
}

To load the gif from the drawable like below:-

ImageView imgView=(ImageView) view.findViewById(R.id.imageView);
Ion.with(imgView)
        .error(R.drawable.error_image)
        .animateGif(AnimateGifMode.ANIMATE)
        .load("android.resource://" + PackageName + "/" + R.drawable.loadingbh)
        .withBitmapInfo();

And load the Image from the URL like :-

Ion.with(imgView)
            .error(R.drawable.error_image)
            .animateGif(AnimateGifMode.ANIMATE)
            .load("https://www.beginnersheap.com/wp-content/uploads/2016/08/loading-BH.gif")  ///LOAD YOUR URL GIF
            .withBitmapInfo();
Ravindra Kushwaha
  • 7,846
  • 14
  • 53
  • 103
0
package com.splash;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Movie;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;

import com.exp.R;

public class SplashViewAnimation extends View {

    private Movie mMovie;
    private long mMovieStart;

    public SplashViewAnimation(Context context) {
        super(context);
        setFocusable(true);

        java.io.InputStream is;
        is = context.getResources().openRawResource(R.drawable.coca_cola2); // Put your gif file here.. 
        mMovie = Movie.decodeStream(is); 
    }

    public SplashViewAnimation(Context context, AttributeSet attrSet) {
        super(context, attrSet);
        setFocusable(true);

        java.io.InputStream is;
        is = context.getResources().openRawResource(R.drawable.coca_cola2);
        mMovie = Movie.decodeStream(is);
    }

    public SplashViewAnimation(Context context, AttributeSet attrSet, int defStyle) {
        super(context, attrSet, defStyle);
        setFocusable(true);

        java.io.InputStream is;
        is = context.getResources().openRawResource(R.drawable.coca_cola2);
        mMovie = Movie.decodeStream(is);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        canvas.drawColor(0x00000000);

        Paint p = new Paint();
        p.setAntiAlias(true);

        long now = android.os.SystemClock.uptimeMillis();
        if (mMovieStart == 0) { // first time
            mMovieStart = now;
        }

        if(mMovie != null){              
            int dur = mMovie.duration();
            if (dur == 0)
            {
                dur = 1000;
            }
            int relTime = (int) ((now - mMovieStart) % dur);

            mMovie.setTime(relTime);
            mMovie.draw(canvas,(int)((getWidth() - mMovie.width()) - getWidth()*.80),
                    (int)((getHeight() - (mMovie.height()-6)) - getHeight()*.10));

            invalidate();
        }
    }
    }
/// Use above code in main class :-

        SplashViewAnimation sp = new SplashViewAnimation(this);       
        FrameLayout mainLayout = (FrameLayout)findViewById(R.id.main_layout_id);

        LayoutParams layoutParams = new     LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
        layoutParams.gravity= Gravity.BOTTOM | Gravity.LEFT;          
        mainLayout.addView(sp,layoutParams);
Mike
  • 129
  • 1
  • 7
0

I know this is a very late reply. However, I couldn't find a correct answer after searching for over a couple of hours(I wanted to use only fresco and avoid adding another library for just one GIF). This could help someone like me searching for the answer. I could finally achieve it with the following workaround.

    ControllerListener controllerListener = new BaseControllerListener<ImageInfo>() {
                @Override
                public void onFinalImageSet(
                        String id,
                        @Nullable ImageInfo imageInfo,
                        @Nullable final Animatable anim) {
                    if (anim != null) {
                        try {
//This is important to handle the loop. We are getting the field from class and //setting the loop count
                            Field field = AbstractAnimatedDrawable.class.getDeclaredField("mLoopCount");
                            field.setAccessible(true);
                            field.set(anim, 1);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                        anim.start();
                    }
                }
            };

    DraweeController controller = Fresco.newDraweeControllerBuilder()
                    .setUri(uri).setAutoPlayAnimations(false).setControllerListener(controllerListener)
                    // other setters
                    .build();
    simpleImageView.setController(controller);

Hope this helps. Thank you.

Anudeep
  • 1,520
  • 1
  • 19
  • 38