在Android WebView中加载网页图片,可以通过以下步骤实现:
<uses-permission android:name="android.permission.INTERNET" />
<WebView
android:id="@+id/webview"
android:layout_width="match_parent"
android:layout_height="match_parent" />
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class MainActivity extends AppCompatActivity {
private WebView webView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
webView = findViewById(R.id.webview);
WebSettings webSettings = webView.getSettings();
webSettings.setJavaScriptEnabled(true);
webView.setWebViewClient(new WebViewClient());
webView.loadUrl("https://example.com"); // 替换为您要加载的网页URL
}
}
onPageFinished()
方法来实现:import android.os.Bundle;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class MainActivity extends AppCompatActivity {
private WebView webView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
webView = findViewById(R.id.webview);
WebSettings webSettings = webView.getSettings();
webSettings.setJavaScriptEnabled(true);
webView.setWebViewClient(new CustomWebViewClient());
webView.loadUrl("https://example.com"); // 替换为您要加载的网页URL
}
private class CustomWebViewClient extends WebViewClient {
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
webView.evaluateJavascript("javascript:window.onload = function() { " +
"var images = document.getElementsByTagName('img'); " +
"for (var i = 0; i < images.length; i++) {" +
" images[i].style.display = 'block'; " +
" images[i].style.width = '100%'; " +
" images[i].style.height = 'auto'; " +
"} " +
"}", null);
}
}
}
这样,当网页加载完成后,所有的图片将自动显示出来。如果需要进一步处理图片,可以在JavaScript代码中添加相应的逻辑。