玩Gif一次使用Fresco Library

问题描述 投票:2回答:4

我使用以下代码来播放gif,但在这里它继续重复gif播放。

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

我该怎么玩一次?

android fresco
4个回答
6
投票

好吧,我没有使用壁画的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);
    }
}

1
投票

你可以使用Glide for GIF ..它是更好的库。

Glide与毕加索非常相似,但这比毕加索要快得多。 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();

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

0
投票

我知道这是一个非常晚的回复。但是,在搜索了几个小时之后我找不到正确的答案(我只想使用壁画并避免为一个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);

希望这可以帮助。谢谢。

© www.soinside.com 2019 - 2024. All rights reserved.