3

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: assets フォルダーに追加された gif 画像。

4

4 に答える 4

6

アニメーション化されていないアセット ディレクトリから 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 を以下のリンクからダウンロードしてください

デモをダウンロード

于 2013-04-01T05:54:00.397 に答える
5

Android コードで GIF を使用するには、

ステップ 1: Assets フォルダーの設定: このフォルダーに、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");

これらの 2 つの簡単な手順で、GIF を webview に読み込むことができます

于 2016-03-09T07:19:00.987 に答える
0

この答えを調べてみましたか?

このユーザー @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>
于 2013-03-31T22:19:31.800 に答える