简体   繁体   English

如何在Android中使用gif图像?

[英]How to use gif images in android?

I want to fetch the image from server through web service and displayed in my app. 我想通过Web服务从服务器获取图像并显示在我的应用程序中。 So how to use Gif images directly from drawable or from web service. 因此,如何直接从可绘制图像或Web服务中使用Gif图像。

In my opinion webview is best for gif image to load. 我认为webview最适合加载gif图像。 You can do it easily by extending the WebView. 您可以通过扩展WebView轻松地做到这一点。

Step-1 //create a class 步骤1 //创建一个类

public class MyGifView extends WebView {
    public MyGifView(Context context, String path) {
        super(context);
        loadUrl(path);
    }
}

Step-2 In your Activity 步骤2在您的活动中

         //If it is an url
        MyGifView myGifView = new MyGifView(this, "http://www.picgifs.com/animal-graphics/animal-graphics/kangaroo/animal-graphics-kangaroo-363368.gif");
            //If gif image in assets folder
        MyGifView view = new MyGifView(this, "file:///android_asset/mygif.gif"); 

         //If gif image in drawable folder
        MyGifView view = new MyGifView(this, "file:///android_res/drawable/mygif.gif"); 
        yourlayout.addView(myGifView); //add to desire layout 
                OR
        setContentView(myGifView);   //add to content view

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM