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,217
  • 7
  • 30
  • 54

3 Answers3

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
  • 981
  • 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,409
  • 16
  • 61
  • 96
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,217
  • 7
  • 30
  • 54