2

我正在使用以下代码播放 gif,但在这里它不断重复播放 gif。

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

我怎么能玩一次?

4

4 回答 4

6

好吧,我没有将 fresco 用于 GIF 文件,但可能这个可以帮助你。使用下面的库来加载 GIF 文件。
请参阅此链接
这是我的代码,它运行良好:

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);
    }
}
于 2016-01-29T05:40:40.850 回答
1

您可以将 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。
参考这个链接点击这里

它的 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();
于 2016-01-29T05:08:59.667 回答
0

我知道这是一个很晚的答复。但是,在搜索了几个小时后,我找不到正确的答案(我只想使用 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);

希望这可以帮助。谢谢你。

于 2017-04-05T08:00:00.073 回答
0
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);
于 2016-01-29T05:30:59.463 回答