动画GIF没有播放

时间:2018-04-30 06:54:39

标签: android gif

我在android xml布局中添加了一个动画GIF文件,没有java处理程序,但gif没有播放。在以下相关问题之一中,接受以下方法作为解决方案。

gradle这个

 dependencies {
        implementation 'pl.droidsonroids.gif:android-gif-drawable:1.2.+'
    } 

布局

<pl.droidsonroids.gif.GifTextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:scaleX="1.05"
                android:scaleY="0.9"
                android:background="@drawable/for_op" />

4 个答案:

答案 0 :(得分:2)

<强> FYI

如果 android:src和/或android:background 声明的 drawables 是GIF文件,那么它们将自动识别为GifDrawables并设置动画。如果给定的drawable不是GIF,那么Views就像普通的ImageView一样工作。我已经注意到了。

注意

您应该使用最新版本 1.2.12 ,而不是添加 +

dependencies {
    compile 'pl.droidsonroids.gif:android-gif-drawable:1.2.12'
}

答案 1 :(得分:1)

首先制作一个GifView类

   import android.annotation.SuppressLint;
   import android.content.Context;
   import android.graphics.Canvas;
   import android.graphics.Movie;
   import android.os.Build;
   import android.util.AttributeSet;
   import android.view.View;


     public class GIFView extends View {

     private static final int DEFAULT_MOVIEW_DURATION = 1000;
     private int mMovieResourceId;
     private Movie mMovie;
     int movieWidth;
     int movieHeight;
     private long mMovieStart = 0;
     private int mCurrentAnimationTime = 0;

     @SuppressLint("NewApi")
     public GIFView(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;
    int w=500;

    mMovie = Movie.decodeStream(getResources().openRawResource(mMovieResourceId));

    requestLayout();
}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    if(mMovie != null){
        setMeasuredDimension(mMovie.width(), mMovie.height());
        movieWidth = mMovie.width();
        movieHeight = 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);
}
public int getMovieWidth() {
    return movieWidth;
}

public int getMovieHeight() {
    return movieHeight;
}
private void drawGif(Canvas canvas) {
    mMovie.setTime(mCurrentAnimationTime);
    mMovie.draw(canvas, 0,0);
    canvas.scale(0f, 0f);

    canvas.restore();
}
}

然后在xml中声明这件事

    <yourpackagename.GIFView
    android:layout_width="match_parent"
    android:id="@+id/gif"
    android:layout_gravity="center"
    android:layout_height="100dp"
      />

然后最后在活动

    GIFView gif;
    gif =(GIFView) findViewById(R.id.gif);
    gif.setImageResource(R.drawable.loading); // your loading gif

答案 2 :(得分:1)

首先你应该实现这个库。

implementation 'pl.droidsonroids.gif:android-gif-drawable:1.2.7'

现在在xml文件中添加此代码。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_splash_screen"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <pl.droidsonroids.gif.GifTextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/logo" />

</RelativeLayout>

现在在Activity中实现延迟以显示要显示的gif动画时间。

 int time = 4000;

      new Handler().postDelayed(new Runnable()
            {
                @Override
                public void run()
                {
                   startActivity(new Intent(SplashScreen.this, YourActivity.class));
                }
            }, time);

答案 3 :(得分:0)

您也可以使用Glide,因为它正式支持谷歌加载图像,GIF等有效的内存管理。我建议你使用相同的,因为我之前使用它,它对我来说很完美。

在此处查看https://github.com/bumptech/glide

将其添加到Gradle:

repositories {
  mavenCentral()
  google()
}

dependencies {
  implementation 'com.github.bumptech.glide:glide:4.7.1'
  annotationProcessor 'com.github.bumptech.glide:compiler:4.7.1'
}

如何使用它来加载Gif:

Glide.with(this).load(R.raw.file.gif)
                    .crossFade()
                    .dontAnimate()
                    .into(new GlideDrawableImageViewTarget(imageView) {
                        @Override
                        public void onLoadStarted(Drawable placeholder) {
                            super.onLoadStarted(placeholder);
                           //DO your loader related stuff here
                        }

                        @Override
                        public void onResourceReady(GlideDrawable drawable, GlideAnimation anim) {
                            super.onResourceReady(drawable, anim);
                            //Close loader here 
                              imageView.setImageDrawable(drawable);
                          } 

                        @Override
                        public void onLoadFailed(Exception e, Drawable errorDrawable) {
                            super.onLoadFailed(e, errorDrawable);
                            //Close loader and set error placeholder here.

                            imageView.setImageResource(R.drawable.ic_home_image_placeholder);
                        }
                    });

另外,为了检查您的文件是否为gif,您可以使用下面分享的方法:

private int getResourceType(String assetName) {
        int id = 0;
        if (assetName != null && !assetName.trim().isEmpty()) {
            int start = assetName.indexOf(".");
            int end = assetName.length();
            if(start >= 0 && end <= assetName.length()) {
                String type = assetName.substring(assetName.indexOf("."), assetName.length());
                if (type != null && type.equalsIgnoreCase(".gif")) {
                    id = 1;
                } else if(type != null && (type.equalsIgnoreCase(".png") || type.equalsIgnoreCase(".jpg")
                        || type.equalsIgnoreCase(".jpeg"))) {
                    id = 2;
                } else {
                    id = 0;
                }
            }
        }
        return id;
    }