阅读使用的WebView安卓GIF图像

阅读使用的WebView安卓GIF图像

本文介绍了阅读使用的WebView安卓GIF图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试使用web视图中的Andr​​oid 2.3.3 API 10读取GIF图像,它不是动画(它显示静态)。我该如何解决这个问题呢?是否有任何设置,我必须改变?

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

ActivtiyMain.xml:

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:

MainActivity.java:

public WebView Wv;

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

bet.gif:GIF图像添加到该文件夹​​的资产

bet.gif: gif images added to the assets folder.

推荐答案

当你正试图从资产目录加载GIF,它不是动态的,你应该使用GifWebView这一点。

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

activity_main.xml

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

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

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 的文件在您的资产目录。

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

从下面的链接下载这个充满deom

Download this full deom from below link

下载演示

这篇关于阅读使用的WebView安卓GIF图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 13:35