某些gif图片上的Gif播放速度很慢

时间:2017-07-26 14:26:31

标签: android android-glide

我正在使用Glide库将gif文件加载到我的应用中。这是我实施的内容:

GlideDrawableImageViewTarget imageViewPreview = new GlideDrawableImageViewTarget(gifSplach);
    Glide.with(this)
            .load("http://i.imgur.com/Cpw2pk6.gif")
            .listener(new RequestListener<String, GlideDrawable>() {
                @Override
                public boolean onException(Exception e, String model, Target<GlideDrawable> target, boolean isFirstResource) {


                    return false;
                }

                @Override
                public boolean onResourceReady(GlideDrawable resource, String model, Target<GlideDrawable> target, boolean isFromMemoryCache, boolean isFirstResource) {

                    //Toast.makeText(activity, ""+e.getMessage(), Toast.LENGTH_SHORT).show();

                    return false;
                }
            })
            .diskCacheStrategy(DiskCacheStrategy.ALL)

            .into(imageViewPreview);

但由于某种原因,gif文件加载的帧非常慢。我试图谷歌,但我无法搞清楚。我使用的是Glide版本3.7.0

2 个答案:

答案 0 :(得分:0)

您不需要任何库,只需使用以下代码: 第1步

public class GifView extends View {
 public Movie mMovie;
  public long movieStart;
private int gifId;

 public GifView(Context context) {
super(context);
 }

  public GifView(Context context, AttributeSet attrs) {
super(context, attrs); 

 initializeView(attrs.getAttributeResourceValue
 ("http://schemas.android.com/apk/r
    es-auto", "src", 0));
 }

   public GifView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);


 initializeView(attrs.getAttributeResourceValue(
"http://schemas.android.com/apk/
  res-auto", "src", 0));
}

 private void initializeView(final int id) {
InputStream is = getContext().getResources().openRawResource(id);
mMovie = Movie.decodeStream(is);
this.gifId = id;
 }

  @Override
  protected void onDraw(Canvas canvas) {
   canvas.drawColor(Color.TRANSPARENT);
   super.onDraw(canvas);
  long now = android.os.SystemClock.uptimeMillis();

  if (movieStart == 0) {
    movieStart = now;
  }

if (mMovie != null) {
    int relTime = (int) ((now - movieStart) % mMovie.duration());
    mMovie.setTime(relTime);
    mMovie.draw(canvas, getWidth() - mMovie.width(), getHeight() - 
  mMovie.height());
    this.invalidate();
  }
   }

      public void setGIFResource(int resId) {
    this.gifId = resId;
    initializeView(this.gifId);
   }

   public int getGIFResource() {
   return this.gifId;
   }
  }

第2步:在res / attrs.xml中添加以下行

 <declare-styleable name="GIFView">
 <attr name="src" format="reference" />
</declare-styleable>

第3步:在特定活动中添加此行AndroidManifest.xml

android:hardwareAccelerated="false"

第4步:在XML中创建此视图:

<com.thigale.testproject.GifView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
components:src="@drawable/loading" />

步骤5在创建视图的父活动中,添加以下行:

xmlns:components="http://schemas.android.com/apk/res-auto"

答案 1 :(得分:0)

我缩小.gif大小并从drawable加载它。它对我有用。

GlideDrawableImageViewTarget imageViewPreview = new GlideDrawableImageViewTarget(gifSplach);
Glide.with(this)
        .load(R.drawable.gif_splash)
        .listener(new RequestListener<String, GlideDrawable>() {
            @Override
            public boolean onException(Exception e, String model, Target<GlideDrawable> target, boolean isFirstResource) {


                return false;
            }

            @Override
            public boolean onResourceReady(GlideDrawable resource, String model, Target<GlideDrawable> target, boolean isFromMemoryCache, boolean isFirstResource) {

                //Toast.makeText(activity, ""+e.getMessage(), Toast.LENGTH_SHORT).show();

                return false;
            }
        })
        .diskCacheStrategy(DiskCacheStrategy.ALL)

        .into(imageViewPreview);