Rehan ch Rehan ch - 1 year ago 48
Android Question

Gif play slow on certain gif picture

I'm using Glide library to load gif files into my app. Here what I implemented:

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


But for some reason the gif files are loading with very slow frame. I tried to google but I weren't able to figure it out. I am using Glide Version 3.7.0

Answer Source

You don't need any library, simply use this code: Step 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;
   }
  }

Step 2: Add following lines in res/attrs.xml

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

Step 3: Add this line your AndroidManifest.xml in specific activity

android:hardwareAccelerated="false"

Step 4: Create this view in your XML:

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

Step 5 In the parent activity where you created the view, add the following lines:

xmlns:components="http://schemas.android.com/apk/res-auto"
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download