【问题标题】:Play Gif Once using Fresco Library使用 Fresco 库播放 Gif 一次
【发布时间】:2023-03-28 17:31:01
【问题描述】:

我正在使用下面的代码来播放 gif,但是这里它一直在重复播放 gif。

Uri uri;
DraweeController controller = Fresco.newDraweeControllerBuilder()
    .setUri(uri)
    .setAutoPlayAnimations(true)
    . // other setters
    .build();
mSimpleDraweeView.setController(controller);

我怎样才能玩一次?

【问题讨论】:

    标签: android fresco


    【解决方案1】:

    好吧,我没有使用 fresco 来制作 GIF 文件,但也许这个可以帮助你。使用以下库加载 GIF 文件。
    Refer this link
    这是我的代码,效果很好:

    public class MainActivity extends AppCompatActivity {
    
    private GifImageView mGigImageView;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    
         mGigImageView = (GifImageView) findViewById(R.id.mgif);
    
         GifDrawable gifDrawable = null;
         try {
              gifDrawable = new GifDrawable(getResources(), R.drawable.ani_1);
              gifDrawable.setLoopCount(1);
             } catch (IOException e) {
              e.printStackTrace();
              }
         mGigImageView.setImageDrawable(gifDrawable);
        }
    }
    

    【讨论】:

    • 我已经在使用这个了.. 由于壁画支持 gif,我可以摆脱这个库
    • fresco 不支持播放 gif 一次。很棒的答案,谢谢。
    • 非常感谢您,在搜索 2 天后您的解决方案有效。非常感谢。
    • @NaeemIbrahim:很高兴它帮助了你 :)
    【解决方案2】:

    您可以将 Glide 用于 GIF。它是更好的库。

    Glide 与 Picasso 非常相似,但比 Picasso 快得多。 Glide 比 Picasso 消耗更少的内存。

    Glide 有但 Picasso 没有 将 GIF 动画加载到简单的 ImageView 的能力可能是 Glide 最有趣的功能。是的,你不能用毕加索做到这一点。

    一些重要的链接-
    1.https://github.com/bumptech/glide
    2.http://inthecheesefactory.com/blog/get-to-know-glide-recommended-by-google/en

    您也可以使用 Ion 库来加载 gif。
    请参阅此链接 click here

    Gradle 依赖项:-

    dependencies {
        ...
    
        compile 'com.android.support:support-v4:23.1.0'
        compile 'com.koushikdutta.ion:ion:2.+'
    }
    

    从drawable加载gif如下:-

    ImageView imgView=(ImageView) view.findViewById(R.id.imageView);
    Ion.with(imgView)
            .error(R.drawable.error_image)
            .animateGif(AnimateGifMode.ANIMATE)
            .load("android.resource://" + PackageName + "/" + R.drawable.loadingbh)
            .withBitmapInfo();
    

    并从 URL 加载图像,例如:-

    Ion.with(imgView)
                .error(R.drawable.error_image)
                .animateGif(AnimateGifMode.ANIMATE)
                .load("https://www.beginnersheap.com/wp-content/uploads/2016/08/loading-BH.gif")  ///LOAD YOUR URL GIF
                .withBitmapInfo();
    

    【讨论】:

    • 我知道这个库...我不想增加我的 apk 大小。我想使用我的应用程序中现有的库
    • 如何用glide播放一次gif?
    【解决方案3】:
    package com.splash;
    
    import android.content.Context;
    import android.graphics.Canvas;
    import android.graphics.Movie;
    import android.graphics.Paint;
    import android.util.AttributeSet;
    import android.view.View;
    
    import com.exp.R;
    
    public class SplashViewAnimation extends View {
    
        private Movie mMovie;
        private long mMovieStart;
    
        public SplashViewAnimation(Context context) {
            super(context);
            setFocusable(true);
    
            java.io.InputStream is;
            is = context.getResources().openRawResource(R.drawable.coca_cola2); // Put your gif file here.. 
            mMovie = Movie.decodeStream(is); 
        }
    
        public SplashViewAnimation(Context context, AttributeSet attrSet) {
            super(context, attrSet);
            setFocusable(true);
    
            java.io.InputStream is;
            is = context.getResources().openRawResource(R.drawable.coca_cola2);
            mMovie = Movie.decodeStream(is);
        }
    
        public SplashViewAnimation(Context context, AttributeSet attrSet, int defStyle) {
            super(context, attrSet, defStyle);
            setFocusable(true);
    
            java.io.InputStream is;
            is = context.getResources().openRawResource(R.drawable.coca_cola2);
            mMovie = Movie.decodeStream(is);
        }
    
        @Override
        protected void onDraw(Canvas canvas) {
            canvas.drawColor(0x00000000);
    
            Paint p = new Paint();
            p.setAntiAlias(true);
    
            long now = android.os.SystemClock.uptimeMillis();
            if (mMovieStart == 0) { // first time
                mMovieStart = now;
            }
    
            if(mMovie != null){              
                int dur = mMovie.duration();
                if (dur == 0)
                {
                    dur = 1000;
                }
                int relTime = (int) ((now - mMovieStart) % dur);
    
                mMovie.setTime(relTime);
                mMovie.draw(canvas,(int)((getWidth() - mMovie.width()) - getWidth()*.80),
                        (int)((getHeight() - (mMovie.height()-6)) - getHeight()*.10));
    
                invalidate();
            }
        }
        }
    /// Use above code in main class :-
    
            SplashViewAnimation sp = new SplashViewAnimation(this);       
            FrameLayout mainLayout = (FrameLayout)findViewById(R.id.main_layout_id);
    
            LayoutParams layoutParams = new     LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
            layoutParams.gravity= Gravity.BOTTOM | Gravity.LEFT;          
            mainLayout.addView(sp,layoutParams);
    

    【讨论】:

      【解决方案4】:

      我知道这是一个很晚的回复。但是,在搜索了几个小时后,我找不到正确的答案(我只想使用 fresco 并避免为一个 GIF 添加另一个库)。这可以帮助像我这样的人寻找答案。我终于可以通过以下解决方法实现它。

          ControllerListener controllerListener = new BaseControllerListener<ImageInfo>() {
                      @Override
                      public void onFinalImageSet(
                              String id,
                              @Nullable ImageInfo imageInfo,
                              @Nullable final Animatable anim) {
                          if (anim != null) {
                              try {
      //This is important to handle the loop. We are getting the field from class and //setting the loop count
                                  Field field = AbstractAnimatedDrawable.class.getDeclaredField("mLoopCount");
                                  field.setAccessible(true);
                                  field.set(anim, 1);
                              } catch (Exception e) {
                                  e.printStackTrace();
                              }
                              anim.start();
                          }
                      }
                  };
      
          DraweeController controller = Fresco.newDraweeControllerBuilder()
                          .setUri(uri).setAutoPlayAnimations(false).setControllerListener(controllerListener)
                          // other setters
                          .build();
          simpleImageView.setController(controller);
      

      希望这会有所帮助。 谢谢。

      【讨论】:

      • AbstractAnimatedDrawable 类未找到。
      猜你喜欢
      • 2016-12-17
      • 2018-09-16
      • 1970-01-01
      • 2019-06-30
      • 1970-01-01
      • 2016-01-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多