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:
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:
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();
}
});
...
}
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.