在android中的ImageView中添加gif图像
我在 imageView 中添加了一个动画 gif 图像。我无法将其视为 gif 图像。那里没有动画。它只是以静止图像的形式出现。我想知道如何将其显示为 gif 图像。
I added an animated gif image in an imageView. I am not able to view it as a gif image. No animation is there. It's appearing just as a still image. I would like to know how can i show it as a gif image.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(19)
在您的 build.gradle(Module:app) 中,添加
android-gif-drawable< /code>
作为依赖项,添加以下代码:
UPDATE: As of Android Gradle Plugin 3.0.0, the new command for compiling is
implementation
, so the above line might have to be changed to:然后同步您的项目。
同步结束后,转到您的布局文件并添加以下代码:
就这样,您可以使用简单的 ImageView 来管理它。
In your build.gradle(Module:app), add
android-gif-drawable
as a dependency by adding the following code:UPDATE: As of Android Gradle Plugin 3.0.0, the new command for compiling is
implementation
, so the above line might have to be changed to:Then sync your project.
When synchronization ends, go to your layout file and add the following code:
And that's it, you can manage it with a simple ImageView.
首先,将 GIF 图像复制到应用程序的 Asset 文件夹中
创建以下类并粘贴代码
AnimationActivity:-
GifDecoder:-
GifDecoderView:-
GifMovieView:-
GifWebView:-
我认为这可能对你有帮助......:)
First, copy your GIF image into Asset Folder of your app
create following classes and paste the code
AnimationActivity: -
GifDecoder:-
GifDecoderView:-
GifMovieView:-
GifWebView:-
I Think It Might Help You... :)
与最新的 Glide 库
一起使用 Gradle:
在 Activity 或 Fragment 中:
with latest Glide library
use Gradle:
in activity or fragment:
使用VideoView。
原生ImageView不支持动画图像。您有两种选择来显示动画 gif 文件:
VideoView
ImageView
并将 gif 文件拆分为几个部分,然后对其应用动画Use VideoView.
Natively ImageView does not support animated image. You have two options to show animated gif file
VideoView
ImageView
and Split the gif file into several parts and then apply animation to it在 Android 中显示 GIF
创建扩展 View 类的
CustomGifView.java
中调用此类
现在在 XML NP
修改 AndroidManifest.xml 以关闭 hardwareAccelerated。
参考
另一个技巧
对于动画功能,您可以访问
Showing GIF In Android
Create
CustomGifView.java which extends View Class
Now call this class in your XML
N.P
Modify AndroidManifest.xml to turn OFF hardwareAccelerated.
Reference
Another Tricks
For animation functionality you may visit
我建议你使用 Glide 库。要使用 Glide,您需要添加此项以将这些依赖项添加
到您的 grandle (Module:app) 文件中。
然后使用这行代码加载您的 gif 图片
有关 Glide 的更多信息
I would suggest you to use Glide library. To use Glide you need to add this to add these dependencies
to your grandle (Module:app) file.
Then use this line of code to load your gif image
More Information on Glide
使用Webview加载gif就像
Use Webview to load gif like as
根据 Ahmad Dwaik '术士的评论,我有尝试了以下代码并且它有效。
PS:如果您的 .gif 图像适合您的 Web 视图,则此功能有效,反之亦然,否则,如果图像大于 Web 视图,则滚动条将启用,并且用户可以滚动图像(又名 Web 视图)。因此,我们在使用它时需要小心,根据图像为 webview 提供适当的大小或编辑适合您的 webview 的图像。
Based on Ahmad Dwaik 'Warlock's comment, I have tried the following code and it worked.
P.S : this things works if you .gif image fits your webview or viceversa else if the image is bigger than the webview the scrollbar gets enabled and user can scroll the image AKA webview. So we need to be careful when we use this, to give proper size to the webview as per image or edit the image that fits your webview.
正如 @Ahmad 在评论中所说,你可以只需使用以下代码即可显示 gif!
只需简单地将这段代码放入 onCreate 方法中,就可以开始了!另外,将您的 gif 放入
assets
文件夹中(如果您没有 asset 文件夹,请在src/main
下创建一个)As @Ahmad said in a comment, you can just use the following code to display a gif!
Just simply pop this code in the onCreate method and you're good to go! Also, place your gif inside the
assets
folder (if you don't have the assets folder, create one undersrc/main
)GIFImageView
XML 中的
Java 文件中
我们需要使用 gifImageView.setLayerType(View.LAYER_TYPE_SOFTWARE, null); 因为当启用硬件加速时,GIF 图像无法在这些设备上工作。在以上 (4.x) 的设备上启用了硬件加速。
GIFImageView
In XML
In Java File
We need to use
gifImageView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
as when hardware accelerated enabled, GIF image not work on those device. Hardware accelerated is enabled on devices above(4.x).我们可以使用 Ion 库轻松在 imageview 上添加动画 gif 图像。
教程视频:: https://www.youtube.com/watch?v=IqKtpdeIpjA
We can easily add animated gif image on imageview using Ion library.
Tutorial video :: https://www.youtube.com/watch?v=IqKtpdeIpjA
您可以通过库 Fresco by Facebook 显示任何 gif 图像:
You can display any gif image via library Fresco by Facebook:
这对我有用:
在您的 build.gradle(项目)中,在 buildscript{} 和 allprojects {} 中编写 mavenCentral() 。它应该看起来像这样:
然后,在 build.gradle(module) 中添加依赖项{}这个片段:
compile 'pl.droidsonroids.gif:android-gif-drawable:1.2.4'
它应该看起来像这样:
把你的 .可绘制文件夹中的 gif 图像。
现在转到应用程序>分辨率>布局> Activity_main.xml 并为您的 .gif 添加此片段:
您就完成了:)
有用的链接:
https://github.com/koral--/android-gif-drawable
https://www.youtube.com/watch?v=EOFY0cwNjuk
希望这会有所帮助。
This is what worked for me:
In your build.gradle (project) write mavenCentral() in the buildscript{} and allprojects {}. It should look like this:
Then, in build.gradle(module) add in dependencies{} this snippet:
compile 'pl.droidsonroids.gif:android-gif-drawable:1.2.4'
it should look like this:
Put your .gif image in your drawable folder.
Now go to app > res > layout > activity_main.xml and add this snipped for your .gif:
And you're done :)
Helpful links:
https://github.com/koral--/android-gif-drawable
https://www.youtube.com/watch?v=EOFY0cwNjuk
Hope this helps.
首先在
module:app
build.gradle 文件中添加依赖然后在布局文件中
Firstly add a dependency in the
module:app
build.gradle fileThen, in the layout file
Gif 还可以通过几行代码在 Web 视图中显示,并且无需任何第三方库。
这样您甚至可以从 SD 卡加载 gif。无需将图像复制到您的资源文件夹。
采取网络视图。
使用可以从 SD 卡打开 gif 文件,而不仅仅是从资产文件夹中打开,如许多示例所示。
Gif's can also be displayed in web view with couple of lines of code and without any 3rd party libraries.
This way you can even load the gif from your SD card. No need to copy images to your Asset folder.
Take a web view.
Use can open a gif file from SD card not just from asset folder as shown in many examples.
我遇到了使用问题
,而且我找不到要添加到我的项目中的 jar 文件。
因此,为了显示 gif,我使用 WebView,如下所示:
I faced problem to use
And also I could not find the jar file to add to my project.
So, in order to show gif, I use WebView like this:
在 android 中显示 GIF 文件
在 build.gradle 文件中添加以下依赖项。
在布局 -
activity_xxxxx.xml
文件中添加GifImageview
如下。在您的 Java 文件中,您可以访问如下所示的 gif。
Display GIF file in android
Add the following dependency in your build.gradle file.
In the layout -
activity_xxxxx.xml
file add theGifImageview
as below.In your Java file , u can access the gif as below.
对我来说又快又简单。无需添加依赖项:
经过2天的搜索,终于找到了这个方法,对我来说效果很好,我在:这里解释了它。也许对某人有用
Fast and Simple for me. no need to add dependencies:
After 2 days search, finally I found this method that works fine for me, I explained it at: here . maybe its useful for someone