How to play animated GIF image from url in android

2019-03-06 10:39发布

I am integrating giphy to my android app..

How can i play animated gif image from URL in android? Should I use ImageView, WebView, VideoView etc? For example if i want to play animation from this URL.

4条回答
孤傲高冷的网名
2楼-- · 2019-03-06 11:00

I'd recommend using a third-party library like Glide that can support GIFs.

I made a quick example app for displaying a GIF from Giphy's API here.

Hope that helps

查看更多
相关推荐>>
3楼-- · 2019-03-06 11:11

Just create a html string and load that into android webview. Tested solution.

http://developer.android.com/reference/android/webkit/WebView.html#loadData(java.lang.String, java.lang.String, java.lang.String)

 String x = "<!DOCTYPE html><html><body><img src=\"http://goo.gl/uPJ9P2\" alt=\"Smileyface\" width=\"100%\" height=\"100%\"></body></html>";

 webView.loadData(x, "text/html", "utf-8");   
查看更多
做个烂人
4楼-- · 2019-03-06 11:16

Try this way

    Movie movie;
    GifView(Context context) {
        super(context);
        movie = Movie.decodeStream(
                context.getResources().openRawResource(
                        R.drawable.some_gif));
    }
    @Override
    protected void onDraw(Canvas canvas) {   
        if (movie != null) {
            movie.setTime(
                (int) SystemClock.uptimeMillis() % movie.duration());
            movie.draw(canvas, 0, 0);
            invalidate();
        }
    }
查看更多
走好不送
5楼-- · 2019-03-06 11:27

The general sketch of the solution is to use employ custom View which draws asks a Movie to draw itself to the Canvas periodically.

The first step is building the Movie instance. There is factory called decodeStream that can make a movie given an InputStream but it isn't enough to use the stream from a UrlConnection. If you try this you will get an IOException when the movie loader tries to call reset on the stream. The hack, unfortunate as it is, is to use a separated BufferedInputStream with a manually-set mark to tell it to save enough data that reset won't fail. Luckily, the URLConnection can tell us how much data to expect. I say this hack is unfortunate because it effectively requires the entire image to be buffered in memory (which is no problem for desktop apps, but it is a serious issue on a memory-constrained mobile device).

Here is a snip of the Movie setup code:

URL url = new URL(gifSource);
URLConnection conn = url.openConnection();
InputStream is = conn.getInputStream();
BufferedInputStream bis = new BufferedInputStream(is);
bis.mark(conn.getContentLength());
Movie movie = Movie.decodeStream(bis);
bis.close();

Next, you need to create a view that will display this Movie. A subclass of View with a custom onDraw will do the trick (assuming it has access to the Movie you created with the previous code).

@Override protected void onDraw(Canvas canvas) {
    if(movie != null) {
        long now = android.os.SystemClock.uptimeMillis();
        int dur = Math.max(movie.duration(), 1); // is it really animated?
        int pos = (int)(now % dur);
        movie.setTime(pos);
        movie.draw(canvas, x, y);
    }
}

The view won't trigger itself to be redrawn without help, and blindly calling invalidate() at the end of onDraw is just an energy waste. In another thread (probably the one you used to download the image data), you can post messages to the main thread, asking for the view to be invalidated at a steady (but not insane) pace.

Handler handler = new Handler();
new Thread() {
    @Override public void run() {
        // ... setup the movie (using the code from above)
        // ... create and display the custom view, passing the movie

        while(!Thread.currentThread().isInterrupted()) {
            handler.post(new Runnable() {
                public void run(){
                    view.invalidate();
                }
            });
            try {
                Thread.sleep(50); // yields 20 fps
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
            }
        }
    }
}.start();

hope it will help you to understand

查看更多
登录 后发表回答