在自定义视图中播放 GIF class
Play GIF in custom view class
我有一个扩展 View
class 的自定义 class。如何在 onDraw
方法中在 Canvas
上绘制的其他内容后面绘制 GIF?
有一个类似的问题,但 Movie
class 已弃用:
How to play GIF in android
你可以在这里使用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 等动画的简单易用的库。
通过在您的 onDraw()
方法中使用 Glide 加载 GIF 来尝试这种方式:
编辑:基于与 @filipst 关于在 canvas 上加载它的讨论,在 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();
}
});
...
}
我有一个扩展 View
class 的自定义 class。如何在 onDraw
方法中在 Canvas
上绘制的其他内容后面绘制 GIF?
有一个类似的问题,但 Movie
class 已弃用:
How to play GIF in android
你可以在这里使用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 等动画的简单易用的库。
通过在您的 onDraw()
方法中使用 Glide 加载 GIF 来尝试这种方式:
编辑:基于与 @filipst 关于在 canvas 上加载它的讨论,在 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();
}
});
...
}