gpt4 book ai didi

android-在webview中加载gif动画

转载 作者:太空狗 更新时间:2023-10-29 14:55:05 25 4
gpt4 key购买 nike

我在 WebView 中显示一个 gif 文件。

enter image description here

在我的网页完成加载之前,此 gif 是可见的。

我是通过以下方式来做的:

方法一:在Webview中加载gif

我已将 loading.gif 放在 asset 文件夹中。

xml:

<WebView
android:id="@+id/webview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="2dp"
android:layout_centerHorizontal="true"/>

Java:

wv.setWebViewClient(new WebViewClient() {
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
WebView loading = (WebView)findViewById(R.id.webview1);
loading.loadUrl("file:///android_asset/loading.GIF");
loading.setBackgroundColor(Color.TRANSPARENT);
loading.setVisibility(View.VISIBLE);
}
@Override
public void onPageFinished(WebView view, String url) {
WebView loading = (WebView)findViewById(R.id.webview1);
loading.setVisibility(View.INVISIBLE);
}

});

方法二:在ImageView中加载gif帧

xml:

<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
android:oneshot="true">
<item android:drawable="@drawable/frame1" android:duration="50" />
<item android:drawable="@drawable/frame2" android:duration="50" />
<item android:drawable="@drawable/frame3" android:duration="50" />
etc...
</animation-list>

Java:

imageView.setBackgroundResource(R.drawable.movie);
AnimationDrawable anim = (AnimationDrawable) imageView.getBackground();
anim.start();

但是两种情况下动画都不流畅。不过第二种方法更好。我只想知道没有gif有没有办法达到同样的效果?如果那不可能的话,我想知道 gif 在所有像素密度下是否具有相同的大小。因为 gif 文件的尺寸是 px 而不是 dp

最佳答案

这是一个例子

如何将 gif 加载到 WebView

使用 .gif 文件的地址创建 html:

<html style="margin: 0;">
<body style="margin: 0;">
<img src="https://..../myimage.gif" style="width: 100%; height: 100%" />
</body>
</html>

将此文件存储到 Assets 目录中:

enter image description here

将此 html 加载到应用程序的 WebView 中:

    WebView webView =  (WebView)findViewById(R.id.webView);
webView = (WebView) findViewById(R.id.webView);
webView.loadUrl("file:///android_asset/html/webpage_gif.html");

这是一个complete example with two options (using WebView or ImageView) to load an animated gif .

enter image description here

关于android-在webview中加载gif动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32257431/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com