如何在 Android 设备上显示 gif [英] How to display gif on Android device

查看:28
本文介绍了如何在 Android 设备上显示 gif的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在 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 文件在 drawable 文件夹中,我该怎么办?最后一行不应该是这样的:

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆