1

I have a custom class that extends the View class. How can I draw a GIF behind other things I am drawing on Canvas in the onDraw method?

There is a similar question but Movie class is deprecated:

How to play GIF in android

filipst
  • 1,547
  • 1
  • 30
  • 55

2 Answers2

1

Try this way by loading GIF using Glide in your onDraw() method:

Edit: based on discussion with @filipst about loading it on canvas, adding code in onResourceReady() method

@Override
protected void onDraw(Canvas canvas) {
    ...
    Glide.with(this.getContext())  // 'this' here is your custom view reference 
        .asGif() // We will define this to tell Glide about it's GIF format to load explicitly
        .load(R.raw.gif_test) // or even put it into drawable R.drawable.git_test
        .into(new SimpleTarget<GifDrawable>() { 
            @Override 
            public void onResourceReady(@NonNull GifDrawable resource, @Nullable Transition<? super GifDrawable> transition) { 
                resource.setBounds(0, 0, canvas.getWidth(), canvas.getHeight()); // Import to set bounds of canvas to load resource otherwise won't load
                resource.draw(canvas); 
                resource.start();
                //or 
                resource.startFromFirstFrame();
            } 
    });
    ...
}
Jeel Vankhede
  • 11,592
  • 2
  • 28
  • 58
  • What's the issue? – Jeel Vankhede Jan 18 '19 at 14:08
  • Glide.with(this.getContext()).asGif().load("https://media.giphy.com/media/xThtaoctdyU3xlMphe/giphy.gif").into(new SimpleTarget() { Override public void onResourceReady(@NonNull GifDrawable resource, @Nullable Transition super GifDrawable> transition) { resource.draw(c); } }); It is not drawing anything – filipst Jan 18 '19 at 14:09
  • Use `resource.start();` and then set your drawable. check out more http://bumptech.github.io/glide/javadocs/431/com/bumptech/glide/load/resource/gif/GifDrawable.html – Jeel Vankhede Jan 18 '19 at 14:13
  • I can't draw anything to canvas from onResourceReady. I tried to draw simple circle and it is not showing. – filipst Jan 18 '19 at 14:34
  • call `invalidate()` when you make any change to canvas. – Jeel Vankhede Jan 18 '19 at 14:36
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/186929/discussion-between-filipst-and-jeel-vankhede). – filipst Jan 18 '19 at 14:43
0

you can use Lottie here is the Library

From XML

<com.airbnb.lottie.LottieAnimationView
        android:id="@+id/animation_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"

        app:lottie_rawRes="@raw/hello_world"
        // or
        app:lottie_fileName="hello_world.json"

        // Loop indefinitely
        app:lottie_loop="true"
        // Start playing as soon as the animation is loaded
        app:lottie_autoPlay="true" />

Programmatically

@BindView(R.id.animation_view)
LottieAnimationView animation_view;

 animation_view.setImageAssetsFolder("images/");
        animation_view.setAnimation(R.raw.lightning_animation);
        animation_view.useHardwareAcceleration(true);
        animation_view.enableMergePathsForKitKatAndAbove(true);
        animation_view.setScaleType(ImageView.ScaleType.CENTER_CROP);
        animation_view.playAnimation();

It's an easy and simple library to use animation like GIF.

Tanveer Munir
  • 1,956
  • 1
  • 12
  • 27