Android WebView: 在页面加载时显示图片
创始人
2024-08-19 08:00:10
0

在Android WebView中加载页面时显示图片的解决方法是使用WebViewClient的shouldInterceptRequest方法来拦截图片请求,并在加载图片之前显示一个占位图。

以下是一个示例代码:

import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.webkit.WebResourceRequest;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

    private WebView webView;
    private ImageView imageView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        webView = findViewById(R.id.webView);
        imageView = findViewById(R.id.imageView);

        webView.setWebViewClient(new WebViewClient() {
            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                super.onPageStarted(view, url, favicon);

                // 显示占位图
                imageView.setImageResource(R.drawable.placeholder);
                imageView.setVisibility(View.VISIBLE);
            }

            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);

                // 页面加载完成后隐藏占位图
                imageView.setVisibility(View.GONE);
            }

            @Override
            public WebResourceResponse shouldInterceptRequest(WebView view, WebResourceRequest request) {
                if (request.getUrl().toString().endsWith(".jpg") || request.getUrl().toString().endsWith(".png")) {
                    // 使用占位图替换图片
                    Drawable placeholder = getResources().getDrawable(R.drawable.placeholder);
                    Bitmap placeholderBitmap = ((BitmapDrawable)placeholder).getBitmap();
                    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
                    placeholderBitmap.compress(Bitmap.CompressFormat.PNG, 100, byteArrayOutputStream);
                    byte[] placeholderData = byteArrayOutputStream.toByteArray();
                    ByteArrayInputStream placeholderInputStream = new ByteArrayInputStream(placeholderData);
                    WebResourceResponse response = new WebResourceResponse("image/png", "UTF-8", placeholderInputStream);
                    return response;
                }

                return super.shouldInterceptRequest(view, request);
            }
        });

        // 加载网页
        webView.loadUrl("https://example.com");
    }
}

在这个示例中,我们首先在WebViewClient的onPageStarted方法中显示一个ImageView,并设置一个占位图作为初始图像。然后,在onPageFinished方法中隐藏ImageView。

接下来,在shouldInterceptRequest方法中,我们拦截图片请求,并使用占位图替换原始图片。这里我们使用一个drawable资源作为占位图,将其转换为Bitmap,然后使用ByteArrayInputStream将其转换为WebResourceResponse,以便在WebView中显示。

最后,在onCreate方法中,我们设置WebViewClient为WebView的客户端,并使用loadUrl方法加载网页。

请注意,这只是一个简单的示例,可能无法处理所有情况。您可能需要根据您的需求进行适当的修改和调整。

相关内容

热门资讯

重大通报!微信小程序微乐房间怎... 重大通报!微信小程序微乐房间怎么开挂,微乐自建房辅助软件-其实是真的有辅助教程(有挂方式)所有人都在...
攻略讲解!微信小程序微乐辅助器... 攻略讲解!微信小程序微乐辅助器,微乐自建房脚本如何下载-确实存在有辅助教程(讲解有挂)1、微信小程序...
玩家攻略推荐!微信小程序微乐辅... 玩家攻略推荐!微信小程序微乐辅助器,收集微乐小程序游戏破解器-果然是真的有辅助方法(有挂细节)1、全...
必看攻略!微信微乐小程序有挂黑... 必看攻略!微信微乐小程序有挂黑科技,微乐小程游戏破解器下载-确实存在有辅助插件(有挂猫腻)进入游戏-...
研究成果!微信小程序微乐房间怎... 研究成果!微信小程序微乐房间怎么开挂,在哪里下载微乐自建房辅助软件-原来真的有辅助器(详细教程)1、...
一分钟揭秘!微信微乐小程序修改... 一分钟揭秘!微信微乐小程序修改器,微乐透视辅助工具-真是是有辅助方法(有挂分享)1、微信微乐小程序修...
玩家必看!微信小程序微乐辅助免... 玩家必看!微信小程序微乐辅助免费,微乐自建房辅助工具-一贯是有辅助脚本(有挂助手)微信小程序微乐辅助...
每日必看教程!微乐家乡麻辣自建... 每日必看教程!微乐家乡麻辣自建房辅助app,提高微乐运气的方法-原来有辅助教程(有挂总结)1、每一步...
一分钟了解!微乐小程序辅助工具... 一分钟了解!微乐小程序辅助工具,微信微乐有挂没有-都是真的是有辅助教程(有挂解惑)微乐小程序辅助工具...
玩家必看分享!微信微乐小程序游... 玩家必看分享!微信微乐小程序游戏辅助脚本,微乐富足和靠谱吗-确实存在有辅助方法(有挂解密)玩家必看分...