我有一个自定义类,该类扩展了View
类。如何通过Canvas
方法在onDraw
上绘制的其他东西后面绘制GIF?
存在类似的问题,但不推荐使用Movie
类:
答案 0 :(得分:1)
通过以下方式尝试通过在您的onDraw()
方法中使用 Glide 加载 GIF :
编辑:基于与 @filipst 的讨论,有关将其加载到画布上,并以onResourceReady()
方法添加代码
@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();
}
});
...
}
答案 1 :(得分:0)
您可以使用Lottie,这里是Library
从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" />
以编程方式
@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();
这是一个使用GIF等动画的简单易用的库。