Android 在自定义视图类中播放GIF
我有一个自定义类,它扩展了Android 在自定义视图类中播放GIF,android,android-animation,android-view,gif,Android,Android Animation,Android View,Gif,我有一个自定义类,它扩展了视图类。在onDraw方法中,我在Canvas上绘制的其他东西后面如何绘制GIF 有一个类似的问题,但电影类已被弃用: 你可以用洛蒂这里是 来自XML <com.airbnb.lottie.LottieAnimationView android:id="@+id/animation_view" android:layout_width="wrap_content" android:layout_height="wra
视图
类。在onDraw
方法中,我在Canvas
上绘制的其他东西后面如何绘制GIF
有一个类似的问题,但电影
类已被弃用:
你可以用洛蒂这里是 来自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" />
使用类似GIF的动画是一个简单易行的库。通过在
onDraw()方法中使用加载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();
}
});
...
}
@覆盖
受保护的void onDraw(画布){
...
Glide.with(this.getContext())/“this”这是您的自定义视图引用
.asGif()//我们将定义它来告诉Glide要显式加载的GIF格式
.load(R.raw.gif\u test)//甚至将其放入drawable R.drawable.git\u test
.into(新的SimpleTarget(){
@凌驾
公共资源就绪(@NonNull gidrawable resource,@Nullable transition你试过用Glide加载它吗?我没有找到任何在画布上用Glide绘制图像的例子。我需要播放GIF文件,而不是动画。它必须在画布上的onDraw中。它也以JSON的形式播放GIF基本上,GIF用于通过无限循环进行动画,如果你需要的话我不想要一个循环,你可以使用一次,但你可以轻松地播放一次GIF,只需将GIF文件转换为JSON格式并使用它。有没有一种方法可以直接在画布上使用它?看看我认为有一种方法可以使用画布,有什么问题吗?Glide.with(this.getContext()).asGif().load(“SimpleTarget()){Override public void onResourceReady(@NonNull gifdawable resource,@null TransitionUseresource.start();
,然后设置您的可绘制。查看更多信息我无法从onResourceReady向画布绘制任何内容。我尝试绘制简单的圆,但它没有显示。callinvalidate()
当您对画布进行任何更改时。
@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();
}
});
...
}