【问题标题】:Read Gif Images using WebView android使用 WebView android 读取 Gif 图像
【发布时间】:2016-03-09 15:19:00
【问题描述】:

当我尝试在 Android 2.3.3 API 10 中使用 WebView 读取 gif 图像时,它不是动画的(它看起来是静态的)。我该如何解决这个问题?有什么我必须更改的设置吗?

ActivtiyMain.xml:

<WebView
    android:id="@+id/webView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/textView1"
    android:layout_below="@+id/textView1"
    android:layout_marginTop="14dp" 
    />

MainActivity.java:

public WebView Wv;

Wv = (WebView) findViewById(R.id.webView1);
Wv.loadUrl("file:///android_asset/bet.gif");

bet.gif:添加到资产文件夹的 gif 图像。

【问题讨论】:

    标签: android webview gif


    【解决方案1】:

    当您尝试从资产目录加载 gif 时,它不是动画的,您应该为此使用 GifWebView。

    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();
        }
    }
    

    将“loading2.gif”文件放入您的资产目录。

    从下面的链接下载这个完整的deom

    Download Demo

    【讨论】:

    • @Ajay_Adddon:确切地说,至少描述问题的问题/您的答案解决了什么问题,提供需要更改以使请求的动画运行的基本内容。一个完整的工作示例的附加链接是一个奖励,但不是问题的答案。
    • @Ajay_Addon 图片在左上角如何居中对齐?
    • 它适用于我,但对于不同的屏幕尺寸,我想要不同尺寸的 webview,我无法使用它,
    • 在我的测试中,如果动画 gif 使用增量帧,Movie 类在 Android OS 4.0 之前的版本中无法正常播放。
    【解决方案2】:

    要在你的 android 代码中使用 GIF,

    第 1 步: 资产文件夹设置: 在此文件夹中,您必须放置 GIF 和 html 代码。 html代码如下

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

    在此代码中,假设您的资产文件夹中有 splash.gif

    第 2 步:只需使用此 url 加载 webview

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

    通过这两个简单的步骤,您可以在 webview 中加载 GIF

    【讨论】:

      【解决方案3】:

      尚不支持动画 gif,但它们可以在某些设备上使用。见:http://code.google.com/p/android/issues/detail?id=3422

      【讨论】:

        【解决方案4】:

        你试过调查this answer吗?

        引用此用户@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>
        

        【讨论】:

          猜你喜欢
          • 2011-06-17
          • 1970-01-01
          • 2015-11-21
          • 2015-05-27
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-11-13
          相关资源
          最近更新 更多