在Android布局中添加GIF

时间:2017-03-20 08:35:59

标签: android xml animated-gif

我尝试在XML布局中添加GIF

一开始工作正常,但是当我关闭android studio并再次重新打开它时 它给了我错误!

另外,我试图降低GIF质量及其空间,一开始效果很好但重启后会出错!

这是我的XML代码:

<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>

错误

enter image description here

2 个答案:

答案 0 :(得分:0)

试试这个,为我工作

  

将您的GIF文件放入可绘制文件夹

     

创建类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");
        }
      }
    } 
  您的.xml文件中的

<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" />
  

和您的活动

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

答案 1 :(得分:0)

您可以将 Glide 用于常规 ImageView

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

在 build.gradle (module:app) 中添加如下依赖

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

导入:

import com.bumptech.glide.Glide

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