3

I'm using this library as a guide to display a 'gif' file. When using the saved gif in drawableit display correctly the gif I call it like this

<pl.droidsonroids.gif.GifImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/imgView1"
    android:src="@drawable/gifFile"
    />

but when removing the src and tried like initializing the GifImageView in the Activity like this

 GifImageView gifFromFile = (GifImageView) findViewById(R.id.imgView1);
gifFromFile.setImageBitmap(Utils.getBitmapImagefromStorage(context, imageHome));

Where getBitmapImagefromStorage get the path and imageHome gets the filename the gif file doesn't play its only display like an image. It was said that If given drawable is not a GIF then mentioned Views work like plain ImageView and ImageButton. But the file I'm providing is a gif.I'm wondering if I'm using it correctly.

Also the file can be png or gif so I needed to support the two.

natsumiyu
  • 3,087
  • 7
  • 27
  • 52

4 Answers4

3

From XML

The simplest way is to use GifImageView (or GifImageButton) like a normal ImageView:

<pl.droidsonroids.gif.GifImageView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:src="@drawable/src_anim"
    android:background="@drawable/bg_anim"
    />

If drawables declared by android:src and/or android:background are GIF files then they will be automatically recognized as GifDrawables and animated. If given drawable is not a GIF then mentioned Views work like plain ImageView and ImageButton.

GifTextView allows you to use GIFs as compound drawables and background.

<pl.droidsonroids.gif.GifTextView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:drawableTop="@drawable/left_anim"
    android:drawableStart="@drawable/left_anim"
    android:background="@drawable/bg_anim"
    />

From Java code GifImageView, GifImageButton and GifTextView have also hooks for setters implemented. So animated GIFs can be set by calling setImageResource(int resId) and setBackgroundResource(int resId)

GifDrawable can be constructed directly from various sources:

 //asset file
    GifDrawable gifFromAssets = new GifDrawable( getAssets(), "anim.gif" );

    //resource (drawable or raw)
    GifDrawable gifFromResource = new GifDrawable( getResources(), R.drawable.anim );

    //byte array
    byte[] rawGifBytes = ...
    GifDrawable gifFromBytes = new GifDrawable( rawGifBytes );

    //FileDescriptor
    FileDescriptor fd = new RandomAccessFile( "/path/anim.gif", "r" ).getFD();
    GifDrawable gifFromFd = new GifDrawable( fd );

    //file path
    GifDrawable gifFromPath = new GifDrawable( "/path/anim.gif" );

    //file
    File gifFile = new File(getFilesDir(),"anim.gif");
    GifDrawable gifFromFile = new GifDrawable(gifFile);

    //AssetFileDescriptor
    AssetFileDescriptor afd = getAssets().openFd( "anim.gif" );
    GifDrawable gifFromAfd = new GifDrawable( afd );

    //InputStream (it must support marking)
    InputStream sourceIs = ...
    BufferedInputStream bis = new BufferedInputStream( sourceIs, GIF_LENGTH );
    GifDrawable gifFromStream = new GifDrawable( bis );

    //direct ByteBuffer
    ByteBuffer rawGifBytes = ...
    GifDrawable gifFromBytes = new GifDrawable( rawGifBytes );

InputStreams are closed automatically in finalizer if GifDrawable is no longer needed so you don't need to explicitly close them. Calling recycle() will also close underlying input source.

Note that all input sources need to have ability to rewind to the beginning. It is required to correctly play animated GIFs (where animation is repeatable) since subsequent frames are decoded on demand from source.

reference : https://github.com/koral--/android-gif-drawable

Mr Robot
  • 3,341
  • 7
  • 39
  • 70
1
GifTextView gifImageView;

In onCreate

gifImageView = (GifTextView) findViewById(R.id.imageView);

Call this method in onCreate

public void playGif(){
        Animation fadeout = new AlphaAnimation(1.f, 1.f);
        fadeout.setDuration(2500); // You can modify the duration here
        fadeout.setAnimationListener(new Animation.AnimationListener() {

            @Override
            public void onAnimationStart(Animation animation) {
                gifImageView.setBackgroundResource(R.drawable.gif_image);//your gif file
            }

            @Override
            public void onAnimationRepeat(Animation animation) {
            }

            @Override
            public void onAnimationEnd(Animation animation) {

            }
        });
        gifImageView.startAnimation(fadeout);
    }

In your layout file

<pl.droidsonroids.gif.GifTextView
        android:id="@+id/imageView"
        android:layout_width="fill_parent"
        android:scaleType="fitXY"
        android:layout_height="fill_parent"
        />
nishith kumar
  • 971
  • 7
  • 20
0

Try this way to work with GIF in your app

public class PlayGifView extends View{

     private static final int DEFAULT_MOVIEW_DURATION = 1000;

     private int mMovieResourceId;
     private Movie mMovie;

     private long mMovieStart = 0;
     private int mCurrentAnimationTime = 0;

     @SuppressLint("NewApi")
     public PlayGifView(Context context, AttributeSet attrs) {
         super(context, attrs);

        /**
         * Starting from HONEYCOMB have to turn off HardWare acceleration to draw
         * Movie on Canvas.
         */
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
            setLayerType(View.LAYER_TYPE_SOFTWARE, null);
        }
    }

     public void setImageResource(int mvId){
         this.mMovieResourceId = mvId;
    mMovie = Movie.decodeStream(getResources().openRawResource(mMovieResourceId));
    requestLayout();
}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    if(mMovie != null){
        setMeasuredDimension(mMovie.width(), mMovie.height());
    }else{
        setMeasuredDimension(getSuggestedMinimumWidth(), getSuggestedMinimumHeight());
    }
}

@Override
protected void onDraw(Canvas canvas) {
    if (mMovie != null){
        updateAnimtionTime();
        drawGif(canvas);
        invalidate();
    }else{
        drawGif(canvas);
    }
}

private void updateAnimtionTime() {
    long now = android.os.SystemClock.uptimeMillis();

    if (mMovieStart == 0) {
        mMovieStart = now;
    }
    int dur = mMovie.duration();
    if (dur == 0) {
        dur = DEFAULT_MOVIEW_DURATION;
    }
    mCurrentAnimationTime = (int) ((now - mMovieStart) % dur);
}

private void drawGif(Canvas canvas) {
    mMovie.setTime(mCurrentAnimationTime);
    mMovie.draw(canvas, 0, 0);
    canvas.restore();
}

}

in your activity class

PlayGifView pGif = (PlayGifView) findViewById(R.id.viewGif);
pGif.setImageResource(R.drawable.yourgifimage);

then in your XML

<YourPackageName.PlayGifView
            android:id="@+id/viewGif"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center" />
Aditya Vyas-Lakhan
  • 13,007
  • 15
  • 58
  • 92
0

I tried using GifDrawable

File gifFile = new File(context.getExternalFilesDir(null)
                    .getAbsolutePath(), imageHome);
GifDrawable gifFromPath  = new GifDrawable(gifFile);

And then instead of using setImageBitmap I use setImageDrawable

GifImageView gifImageView = (GifImageView) findViewById(R.id.textViewDealFinder);
        gifImageView.setImageDrawable(gifFromPath);
natsumiyu
  • 3,087
  • 7
  • 27
  • 52