Read Gif Images using WebView android

前端 未结 4 1715
渐次进展
渐次进展 2021-01-01 08:30

When I try to read a gif images using a WebView in Android 2.3.3 API 10, it\'s not animated (it appears static). How can I solve this issue? Is there any setting I must chan

相关标签:
4条回答
  • 2021-01-01 08:34

    To use GIF in your android code,

    Step 1: Assets folder setting : In this folder you have to place your GIF and html code. Html code is following

    <html>
    <head>
        <style type='text/css'>body{margin:auto auto;text-align:center;} img{width:100%25;}</style>
    </head>
    <body>
    <img src="splash.gif" width="100%"/>
    </body>
    </html>

    In this code lets assume you have splash.gif in ur assets folder

    Step 2: Just load the webview with this url

    wvSplashScreen.loadUrl("file:///android_asset/splash.html");

    By these 2 simple step you can load GIF in your webview

    0 讨论(0)
  • 2021-01-01 08:44

    Animated gif's aren't supported yet, although they work on some devices. See: http://code.google.com/p/android/issues/detail?id=3422

    0 讨论(0)
  • 2021-01-01 08:45

    When you are trying to load gif from assets directory, its not animated, you should use GifWebView for this.

    activity_main.xml

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="#da4040" >
    
        <WebView
            android:id="@+id/webviewActionView"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:minHeight="200dp"
            android:minWidth="200dp"
            android:scrollbars="none" >
        </WebView>
    
    </RelativeLayout>
    

    MainActivity.java

    package com.test2;
    
    import java.io.InputStream;
    
    import android.app.Activity;
    import android.os.Bundle;
    import android.webkit.WebView;
    import android.webkit.WebViewClient;
    
    public class MainActivity extends Activity {
    
        WebView webviewActionView;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            InputStream stream = null;
            try {
                stream = getAssets().open("loading2.gif");
            } catch (Exception e) {
                e.printStackTrace();
            }
    
            webviewActionView = (WebView)findViewById(R.id.webviewActionView);
            webviewActionView.setWebViewClient(new MyWebViewClient());
            webviewActionView.getSettings().setJavaScriptEnabled(true);
    
            GifWebView view = new GifWebView(this, stream);
            webviewActionView.addView(view);
        }
    
        private class MyWebViewClient extends WebViewClient {
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return true;
            }
        }
    }
    

    GifWebView.java

    package com.test2;
    
    import java.io.InputStream;
    
    import android.content.Context;
    import android.graphics.Canvas;
    import android.graphics.Color;
    import android.graphics.Movie;
    import android.os.SystemClock;
    import android.view.View;
    
    public class GifWebView extends View {
        private Movie mMovie;
        InputStream mStream;
        long mMoviestart;
    
        public GifWebView(Context context, InputStream stream) {
            super(context);
            mStream = stream;
            mMovie = Movie.decodeStream(mStream);
        }
    
        @Override
        protected void onDraw(Canvas canvas) {
            canvas.drawColor(Color.TRANSPARENT);
            super.onDraw(canvas);
            final long now = SystemClock.uptimeMillis();
    
            if (mMoviestart == 0) {
                mMoviestart = now;
            }
            final int relTime = (int) ((now - mMoviestart) % mMovie.duration());
            mMovie.setTime(relTime);
            mMovie.draw(canvas, 10, 10);
            this.invalidate();
        }
    }
    

    Put "loading2.gif" file in your assets directory.

    Download this full deom from below link

    Download Demo

    0 讨论(0)
  • 2021-01-01 08:46

    Have you tried looking into this answer?

    To quote this user @Kantesh:

    <html>
    <body bgcolor="white">
        <table width="100%" height="100%">
            <tr>
                <td align="center" valign="center">
                    <font color="gray">Some text you display</font>
                    <br/>
                    <br/>
                    <br/>
                    <br/>
                    <br/>
                    <img src="yourGIF.gif">
                </td>
            </tr>
        </table>
    </body>
    
    0 讨论(0)
提交回复
热议问题