[英]Read Gif Images using WebView android
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). 当我尝试在Android 2.3.3 API 10中使用WebView读取gif图像时,它不是动画的(它看起来是静态的)。 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 images added to the assets folder. bet.gif:添加到资产文件夹的gif图像。
When you are trying to load gif from assets directory, its not animated, you should use GifWebView for this. 当你试图从资源目录加载gif时,它没有动画,你应该使用GifWebView。
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();
}
}
Put " loading2.gif " file in your assets directory. 将“ loading2.gif ”文件放在资产目录中。
Download this full deom from below link 从下面的链接下载这个完整的deom
To use GIF in your android code, 要在Android代码中使用GIF,
Step 1: Assets folder setting : In this folder you have to place your GIF and html code. 第1步:资产文件夹设置:在此文件夹中,您必须放置GIF和HTML代码。 Html code is following 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>
In this code lets assume you have splash.gif in ur assets folder 在此代码中,假设您在ur assets文件夹中有splash.gif
Step 2: Just load the webview with this url 第2步:只需使用此网址加载webview
wvSplashScreen.loadUrl("file:///android_asset/splash.html"); wvSplashScreen.loadUrl( “文件:///android_asset/splash.html”);
By these 2 simple step you can load GIF in your webview 通过这两个简单的步骤,您可以在Webview中加载GIF
Animated gif's aren't supported yet, although they work on some devices. 虽然它们适用于某些设备,但尚不支持动画gif。 See: http://code.google.com/p/android/issues/detail?id=3422 请参阅: http : //code.google.com/p/android/issues/detail?id = 3422
Have you tried looking into this answer ? 你试过这个答案吗?
To quote this user @Kantesh: 引用此用户@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>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.