如果webview html内容很少<img>
标签,当用户在 web View 中点击图像时,是否可以在新 Activity 中以全屏模式打开该图像?
最佳答案
是的。基本上可以从 WebView
中调用任何类型的 native Android 代码。您定义要公开的方法,以便从类中的 WebView
中进行调用,将此类的实例传递给 WebView
,从那时起,这些方法就可以调用了就像 JavaScript 方法一样。这是示例(这是从我的另一个答案复制粘贴的;您需要将视频 Intent 替换为图像显示 Intent ):
从 Web View 中调用 native 代码:
创建 Web View 时添加 javascript 接口(interface)(基本上是 java 类,其方法将公开以通过 Web View 中的 javascript 调用)。
JavaScriptInterface jsInterface = new JavaScriptInterface(this);
webView.getSettings().setJavaScriptEnabled(true);
webView.addJavascriptInterface(jsInterface, "JSInterface");
javascript接口(interface)类本身的定义(这是我从我的另一个答案中获取的示例类,并以 native Intent 打开视频)
public class JavaScriptInterface {
private Activity activity;
public JavaScriptInterface(Activity activiy) {
this.activity = activiy;
}
public void startVideo(String videoAddress){
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.parse(videoAddress), "video/3gpp"); // The Mime type can actually be determined from the file
activity.startActivity(intent);
}
}
现在,如果您想从页面的 html 中调用此代码,请提供以下方法:
<script>
function playVideo(video){
window.JSInterface.startVideo(video);
}
</script>
因此,您需要向 JSInterface
添加适当的方法,并从 Web 应用程序中调用代码。
关于android - 以全屏模式从 WebView 打开图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9719012/