如何在Android设备上显示gif [英] How to display gif on Android device
问题描述
我正在尝试在Android上显示gif图像.我在SO上找到了这段代码,这被某些用户称为正确的解决方案:
I'm trying to display gif images on Android. I found this code on SO, which is said a correct solution by some users :
webView wView = new webView(this);
wView.loadUrl("file:///android_asset/piggy.gif");
setContentView(view);
它说我的gif文件应该在资产文件夹中.如果我的gif文件位于可绘制文件夹中,该怎么办?最后一行不应该是这样的:
It says my gif file should be in assets folder. What should I do if my gif file is in drawable folder? And shouldn't the last line be like this :
setContentView(wView);
除此之外,如果您有一个在Android上显示gif的简单解决方案,我可以接受.谢谢.
Other than this, if you have an easy solution for displaying gifs on Android, I can accept. Thanks.
推荐答案
Android有一些基本要求.所有.gif/视频和自定义字体或(.otf)文件必须仅放置在资产文件夹中.据我所知,没有其他方法可以做到这一点.要访问资产的文件,您已经编写了一行代码:
Android has some basic requirements. All .gif / videos and custom fonts or (.otf) files must be only placed in assets folder. There is no other way of doing it as far as I know. To access the asset's files you have write a line of code :
访问资源的格式如下:
"android.resource://[package]/[res id]"
Uri myUri =("android.resource://" + context.getPackageName() + "/"
+ R.drwable.mygifimage);
try{
Url myUrl = myUri.toURL();
webView wView = new webView(this);
wView.loadUrl(myUrl);
setContentView(view);
}catch(Exception e){
e.printStackTrace()
}
这篇关于如何在Android设备上显示gif的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!