API28之前如何使用AnimatedImageDrawable(GIF图片)?
How to use AnimatedImageDrawable (GIF image) before API 28?
在API28 AnimatedImageDrawable
中引入了显示gif图片:
https://developer.android.com/reference/android/graphics/drawable/AnimatedImageDrawable.html
Drawable for drawing animated images (like GIF).
问题是他们没有说明如何在 API 28.
之前将它与 AppCompat-v7
或 Support-v4
库一起使用以实现追溯兼容性
有谁知道在API 28之前是否可以使用它来显示GIF图像?
简短回答:不,您不能在 API 28 以下使用 AnimatedImageDrawable
,并且没有 class.
的兼容版本
对于您的问题,您可以根据自己的情况有不同的选择:
- 如果您已经在您的应用中使用
Picasso
或 Glide
,它们都会自动处理 GIF 图像,您可以使用它们代替 AnimatedImageDrawable(我不鼓励您只添加那些大库如果您不需要他们提供的其他功能,可以处理 GIF)。
- 您可以使用
WebView
将图像嵌入 HTML,然后在 WebView 中加载 HTML 页面
- 使用一个已知的库来做 API 17+,称为 Android Gif Drawable
For both options 1 and 2 you can find plenty of examples online. Some of them are in this thread:
使用 Android Movie class,这里是一个如何播放 GIF 的例子:
// initialize Movie object
Movie movie = Movie.decodeStream(resources.openRawResource(R.drawable.my_gif));
int startTime = System.currentTimeMillis();
// drawing GIF to Canvas
// this line will make your GIF looping
movie.setTime((int) (System.currentTimeMillis() - startTime) % movie.duration());
movie.draw(canvas, x, y); // pass the coordinates of drawing
在API28 AnimatedImageDrawable
中引入了显示gif图片:
https://developer.android.com/reference/android/graphics/drawable/AnimatedImageDrawable.html
Drawable for drawing animated images (like GIF).
问题是他们没有说明如何在 API 28.
之前将它与AppCompat-v7
或 Support-v4
库一起使用以实现追溯兼容性
有谁知道在API 28之前是否可以使用它来显示GIF图像?
简短回答:不,您不能在 API 28 以下使用 AnimatedImageDrawable
,并且没有 class.
对于您的问题,您可以根据自己的情况有不同的选择:
- 如果您已经在您的应用中使用
Picasso
或Glide
,它们都会自动处理 GIF 图像,您可以使用它们代替 AnimatedImageDrawable(我不鼓励您只添加那些大库如果您不需要他们提供的其他功能,可以处理 GIF)。 - 您可以使用
WebView
将图像嵌入 HTML,然后在 WebView 中加载 HTML 页面
- 使用一个已知的库来做 API 17+,称为 Android Gif Drawable
For both options 1 and 2 you can find plenty of examples online. Some of them are in this thread:
使用 Android Movie class,这里是一个如何播放 GIF 的例子:
// initialize Movie object
Movie movie = Movie.decodeStream(resources.openRawResource(R.drawable.my_gif));
int startTime = System.currentTimeMillis();
// drawing GIF to Canvas
// this line will make your GIF looping
movie.setTime((int) (System.currentTimeMillis() - startTime) % movie.duration());
movie.draw(canvas, x, y); // pass the coordinates of drawing