Sami hanna
Sami hanna

Reputation: 49

Adding GIF in android layout

I'm trying to add a GIF in XML layout

It works fine at first, but when I close android studio and re-open it again it gives me error!

Also, I have tried to lower GIF quality and its space , it works good at first but gives error after restart!

Here's my XML code:

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="#fff"
    android:orientation="vertical">

    <pl.droidsonroids.gif.GifImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/myimg"/>
</LinearLayout>

Error

enter image description here

Upvotes: 3

Views: 15759

Answers (2)

Fthr
Fthr

Reputation: 807

You can use Glide for regular ImageView

Glide.with(this).load(R.drawable.myimg).into(imageView)

Add the following dependency in build.gradle (module:app)

implementation 'com.github.bumptech.glide:glide:4.9.0'

Import:

import com.bumptech.glide.Glide

Reference: https://www.tutorialspoint.com/how-to-display-animated-gif-images-in-android-using-kotlin

Upvotes: 1

Mehul Gajjar
Mehul Gajjar

Reputation: 431

try this, working for me

Put your GIF file into Drawable folder

create Class GifImageView.java

public class GifImageView extends View {

      private InputStream mInputStream;
      private Movie mMovie;
      private int mWidth, mHeight;
      private long mStart;
      private Context mContext;

      public GifImageView(Context context) {
        super(context);
        this.mContext = context;
      }

      public GifImageView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
      }

      public GifImageView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.mContext = context;
        if (attrs.getAttributeName(1).equals("background")) {
          int id = Integer.parseInt(attrs.getAttributeValue(1).substring(1));
          setGifImageResource(id);
        }
      }


      private void init() {
        setFocusable(true);
        mMovie = Movie.decodeStream(mInputStream);
        mWidth = mMovie.width();
        mHeight = mMovie.height();

        requestLayout();
      }

      @Override
      protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        setMeasuredDimension(mWidth, mHeight);
      }

      @Override
      protected void onDraw(Canvas canvas) {

        long now = SystemClock.uptimeMillis();

        if (mStart == 0) {
          mStart = now;
        }

        if (mMovie != null) {

          int duration = mMovie.duration();
          if (duration == 0) {
            duration = 1000;
          }

          int relTime = (int) ((now - mStart) % duration);

          mMovie.setTime(relTime);

          mMovie.draw(canvas, 0, 0);
          invalidate();
        }
      }

      public void setGifImageResource(int id) {
        mInputStream = mContext.getResources().openRawResource(id);
        init();
      }

      public void setGifImageUri(Uri uri) {
        try {
          mInputStream = mContext.getContentResolver().openInputStream(uri);
          init();
        } catch (FileNotFoundException e) {
          Log.e("GIfImageView", "File not found");
        }
      }
    } 

in your .xml file

<com.gifLoader.GifImageView // **of your GifImageView.java**
      android:id="@+id/GifImageView"
      android:layout_height="@dimen/txt_size_1"
      android:layout_width="@dimen/txt_size_1"
      android:layout_alignParentBottom="true"
      android:layout_centerHorizontal="true"
      android:layout_marginBottom="@dimen/sb_progrees_margin_lr"
      android:layout_centerInParent="true" />

and in your activity

GifImageView gifImageView = (GifImageView) findViewById(R.id.GifImageView); 
gifImageView.setGifImageResource(R.drawable.ring);

Upvotes: 0

Related Questions