当前位置:网站首页>WebView injects Js code to realize large image adaptive screen click image preview details
WebView injects Js code to realize large image adaptive screen click image preview details
2022-08-09 13:16:00 【Xie Dong_】
Opening title: When the webview in android loads the web page through the url, if the web page happens not to be individually adapted to the mobile phone, then the url we get is directly loaded into the webview by means of loadUrl.Some adaptation problems are prone to occur. Even if you set the web page adaptation through websettings, it is difficult to deal with the styles in some details, especially the image resources. Some CSS styles are done according to the adaptation of the PC side and loaded to the mobile phone.It is easy to have style problems. Let's take the example of processing pictures on the webpage before and after the adaptation.
No picture, no truth:
As you can clearly see in the above picture, the left side is not adapted. The picture on the webview exceeds the screen display. You can only preview the full picture by sliding left and right. The right picture is the picture that adapts to the screen after injecting js code., you can clearly see that the width of the picture on the screen is the width of the screen, and by injecting js code to the picture on the web page, the click preview function is added.
Implementation idea analysis:
1. The webvie above is a web page loaded by loadurl, so it is impossible for us to change the structure and style of the html code before loading the web page. We start from another point of view and use webviewClient to monitor the web page loading finish and then perform interception processing, and then inject the corresponding js code to complete our expected effect.
2. If you are loading a web page in the form of loadData, you can use regular expressions to add styles. I will not go into details. This blog post mainly explains the first method, and readers can implement it by themselves.
Sample code:
1. Since js code needs to be injected, it must be set to support JavaScript references for webview
wvCompContent.getSettings().setJavaScriptEnabled(true);
2. Next, we give the ResizeImgWebViewClient that we extend from WebviewClient to the set, and inject the js code that makes the image adapt to the screen width after the web page is loaded.
wvCompContent.setWebViewClient(new ResizeImgWebviewClient());
public class ResizeImgWebviewClient extends WebViewClient {@Overridepublic void onPageFinished(WebView view, String url) {super.onPageFinished(view, url);resizeImg(view);addImgClickEvent(view);}/*** Add image click event** @param view*/private void addImgClickEvent(WebView view) {view.loadUrl("javascript:(function(){" +"var objs = document.getElementsByTagName(\"img\"); " +"for(var i=0;i
Now that we have completed the operation of the image width and height adaptive screen, readers may notice that there is a js code to add click events to the image on the ResizeImgWebviewClient. This involves the interaction of the web page to the native, so we need to addJavaScriptInterace, bind the response JSBridge into it
wvCompContent.addJavascriptInterface(new JsBridge(), "JsBridge");
3. In jsBridge, we intercept the specific operation of the image on the web page to process the response. For example, you can start a new Activity or fragment and then load the image uploaded from the web page through image loading tools such as glideThe url to realize the operation of clicking the picture to view the picture details, I posted the code of the preview picture, which is the picture preview tool class encapsulated in my project, for readers' reference only
public class JsBridge {/*** Response to click on image event on webview (large image preview)** @param url*/@JavascriptInterfacepublic void openImage(String url) {List images = new ArrayList<>();LocalMedia localMedia = new LocalMedia();localMedia.setPath(url);images.add(localMedia);MediaConfig.getInstance().picturePreview(AppManager.getInstance().currentActivity(), 0, images);}}
At this point, the whole process is over. To sum up, by intercepting and injecting js, we have completed the operation of adjusting the width and height of the pictures that were not previously adapted to the mobile phone, and also through the web page.The interaction to the native adds a click event to the picture on the web page, and adds a logical operation of clicking to view the picture details after listening to the click event on the native.
边栏推荐
- 用 API Factory 产品生成 API 文档
- LeetCode #101. Symmetric Binary Tree
- 李开复花上千万投的缝纫机器人,团队出自大疆
- 基于STM32+铂电阻设计的测温仪
- shell脚本------函数的格式,传参,变量,递归,数组
- 太卷了... 腾讯一面被问到内存满了,会发生什么?
- How to upload local file trial version in binary mode in ABAP report
- Scala 高阶(七):集合内容汇总(上篇)
- Go-based web access parameters
- 2022牛客多校(六)M. Z-Game on grid
猜你喜欢
Say goodbye to the AI era of hand looms
在“Extend the Omniverse”比赛中构建用于 3D 世界的工具
超越CLIP的多模态模型,只需不到1%的训练数据!南加大最新研究来了
[Interview high-frequency questions] Linked list high-frequency questions that can be gradually optimized
ansible-cmdb友好展示ansible收集主机信息
Resolved IndentationError: unindent does not match any oute r indentation Level
h264协议
GPT-3组合DALL·E,60秒内搞定游戏设定和原型动画!网友看后:这游戏想玩
Fragment中嵌套ViewPager数据空白页异常问题分析
MySQL principle and optimization of Group By optimization techniques
随机推荐
手写大根堆
【HCIP持续更新】IS-IS协议原理与配置
国产抗新冠口服药每瓶不超300元/ 我国IPv6网络全面建成/ 谷歌入局折叠屏手机...今日更多新鲜事在此...
如何修改data work上jdbc驱动的版本
使用注解将EventBus封装抽取到基类
罗振宇折戟创业板/ B站回应HR称用户是Loser/ 腾讯罗技年内合推云游戏掌机...今日更多新鲜事在此...
微信支付开发流程
LeetCode热题(11.合并两个有序链表)
在“Extend the Omniverse”比赛中构建用于 3D 世界的工具
Nature:猪死亡1小时后,器官再次运转
香港服务器如何进行加密?
Here comes the question: Can I successfully apply for 8G memory on a machine with 4GB physical memory?
PM2 configuration file
Flutter入门进阶之旅(三)Text Widgets
阿里高工带来的20022最新面试总结太香了
Flutter入门进阶之旅(五)Image Widget
自定义VIEW实现应用内消息提醒上下轮播
K个结点的组内逆序调整
腾讯欲成育碧最大股东/ 米哈游招NLP内容生成研究员/ AI发现四千余物种濒临灭绝...今日更多新鲜事在此...
AQS Synchronization Component - FutureTask Analysis and Use Cases