【问题标题】:Play GIF in custom view class在自定义视图类中播放 GIF
【发布时间】:2019-06-12 13:30:06
【问题描述】:

我有一个扩展View 类的自定义类。如何在 Canvas 上使用 onDraw 方法在其他东西后面绘制 GIF?

有一个类似的问题,但Movie 类已被弃用:

How to play GIF in android

【问题讨论】:

  • 你试过用 Glide 加载它吗?
  • 我还没有找到任何如何使用 Glide on the Canvas 绘制图像的示例

标签: android android-animation android-view gif


【解决方案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();
            } 
    });
    ...
}

【讨论】:

  • 有什么问题?
  • Glide.with(this.getContext()).asGif().load("media.giphy.com/media/xThtaoctdyU3xlMphe/… SimpleTarget() { Override public void onResourceReady(@NonNull GifDrawable resource, @Nullable Transition transition) { resource.draw(c); } }); 它没有画任何东西
  • 使用resource.start(); 然后设置你的drawable。查看更多bumptech.github.io/glide/javadocs/431/com/bumptech/glide/load/…
  • 我无法从 onResourceReady 将任何内容绘制到画布上。我试着画一个简单的圆圈,但它没有显示出来。
  • 当您对画布进行任何更改时,请致电 invalidate()
【解决方案2】:

你可以在这里使用 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 等动画的简单库。

【讨论】:

  • 我需要播放 GIF 文件,而不是动画。它必须在 Canvas 上的 onDraw 中
  • 它也可以播放 JSON 格式的 GIF
  • 基本上,如果您不希望循环使用一次,则 GIF 通过无限循环用于动画,但您可以轻松播放一次 GIF,只需将 GIF 文件转换为 JSON 格式并使用它。
  • 有没有办法直接在画布上使用?
  • 看看我觉得有办法使用canvasLink1,Link2
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-05-10
  • 1970-01-01
  • 1970-01-01
  • 2014-09-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多