【问题标题】:Animated GIF not playing动画 GIF 无法播放
【发布时间】:2018-04-30 15:32:00
【问题描述】:

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

分级

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

【问题讨论】:

  • 发布你的java类
  • 早期的解决方案没有任何java类!!
  • github.com/koral--/android-gif-drawable/issues/474 。通过使用最新版本drawable:1.2.12
  • 您也可以使用 Glide,它是 google 支持的官方库,用于加载 gif、图像等,具有更好的内存管理。要我显示代码吗?
  • 改了就可以了,不好意思!

标签: android gif


【解决方案1】:

仅供参考

如果 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'
}

【讨论】:

    【解决方案2】:

    首先制作一个类 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
    

    【讨论】:

      【解决方案3】:

      首先你应该实现这个库。

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

      【讨论】:

        【解决方案4】:

        您也可以使用 Glide,因为它是 google 官方支持的,用于加载图像、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;
            }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2016-12-17
          • 2021-05-17
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-08-24
          • 2021-10-15
          • 2017-10-01
          相关资源
          最近更新 更多