【HarmonyOS】【Demo】【JAVA UI】 鸿蒙怎么在Webview上添加组件
在大家HarmonyOS开发中,Webview组件上添加组件可能是很常见的功能了,HarmonyOS的webview和Android的webwiew存在一点点区别,今天来实现这个功能
使用项目布局显示webview搭建和webview加载链接的基本功能
解决webview覆盖层不显示问题
查看运行效果
基础的webview学习,大家参考如下链接 :
第一步 使用DependentLayout 简单大家一个layout布局界面
<?xml version="1.0" encoding="utf-8"?>
<DependentLayout
xmlns:ohos="http://schemas.huawei.com/res/ohos"
ohos:height="match_parent"
ohos:width="match_parent"
ohos:orientation="vertical"
ohos:alignment="bottom">
<ohos.agp.components.webengine.WebView
ohos:id="$+id:webview"
ohos:height="match_parent"
ohos:width="match_parent">
</ohos.agp.components.webengine.WebView>
<Image
ohos:id="$+id:myImage"
ohos:height="100vp"
ohos:width="100vp"
ohos:image_src="$media:icon"/>
</DependentLayout>
Java代码如下:
package com.harmony.alliance.mydemo.slice;
import com.harmony.alliance.mydemo.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.DirectionalLayout;
import ohos.agp.components.webengine.*;
import ohos.media.image.ImagePacker;
import ohos.media.image.PixelMap;
import ohos.media.image.common.PixelFormat;
import ohos.media.image.common.Size;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
public class MainAbilitySlice extends AbilitySlice {
private static final String EXAMPLE_URL = "https://developer.harmonyos.com/cn/docs/documentation/doc-references/js-apis-distributed-migration-0000001050024965";
@Override
public void onStart(Intent intent) {
super.onStart(intent);
super.setUIContent(ResourceTable.Layout_ability_main);
// dl_bottom.requestForceForwardTouchEvent()
WebView webView = (WebView) findComponentById(ResourceTable.Id_webview);
webView.getWebConfig().setJavaScriptPermit(true); // 如果网页需要使用JavaScript,增加此行;如何使用JavaScript下文有详细介绍
webView.getWebConfig().setWebStoragePermit(true);
webView.getWebConfig().setDataAbilityPermit(true);
webView.getWebConfig().setLoadsImagesPermit(true);
webView.getWebConfig().setMediaAutoReplay(true);
webView.getWebConfig().setLocationPermit(true);
webView.getWebConfig().setSecurityMode(WebConfig.SECURITY_SELF_ADAPTIVE);
webView.load(EXAMPLE_URL);
// HiLogUtils.PrintLog("webView.load======>>>"+EXAMPLE_URL);
}
@Override
public void onActive() {
super.onActive();
}
@Override
public void onForeground(Intent intent) {
super.onForeground(intent);
}
}
运行效果如下
第二步解决Webview覆盖层不显示的问题
这时候我们发现没有达到我想实现的效果,我们应该怎么解决呢?
2.1我们需要添加如下代码
private void setWindowBgToAdaptWebView() {
final String backgroundFileName = "_bg.jpg";
File file = new File(getContext().getFilesDir(), backgroundFileName);
if (file.exists()) {
getWindow().setBackground(file.getPath());
return;
}
PixelMap pixelMap = createBgPixelMap();
if (pixelMap == null) {
return;
}
ImagePacker imagePacker = ImagePacker.create();
try (OutputStream outputStream = new FileOutputStream(file)) {
ImagePacker.PackingOptions options = new ImagePacker.PackingOptions();
if (!imagePacker.initializePacking(outputStream, options)) {
return;
}
if (!imagePacker.addImage(pixelMap)) {
return;
}
if (imagePacker.finalizePacking() < 0) {
return;
}
} catch (IOException e) {
e.printStackTrace();
} finally {
imagePacker.release();
}
getWindow().setBackground(file.getPath());
}
private PixelMap createBgPixelMap() {
final int length = 10;
PixelMap.InitializationOptions initializationOptions = new PixelMap.InitializationOptions();
initializationOptions.size = new Size(length, length);
initializationOptions.pixelFormat = PixelFormat.ARGB_8888;
initializationOptions.editable = true;
int[] defaultColors = new int[length * length];
return PixelMap.create(defaultColors, initializationOptions);
}
2.2我们要在OnStart的方法添加如下代码
全部代码如下
package com.harmony.alliance.mydemo.slice;
import com.harmony.alliance.mydemo.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.DirectionalLayout;
import ohos.agp.components.webengine.*;
import ohos.media.image.ImagePacker;
import ohos.media.image.PixelMap;
import ohos.media.image.common.PixelFormat;
import ohos.media.image.common.Size;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
public class MainAbilitySlice extends AbilitySlice {
private static final String EXAMPLE_URL = "https://developer.harmonyos.com/cn/docs/documentation/doc-references/js-apis-distributed-migration-0000001050024965";
@Override
public void onStart(Intent intent) {
setWindowBgToAdaptWebView();
super.setUIContent(ResourceTable.Layout_ability_main);
// dl_bottom.requestForceForwardTouchEvent()
WebView webView = (WebView) findComponentById(ResourceTable.Id_webview);
webView.getWebConfig().setJavaScriptPermit(true); // 如果网页需要使用JavaScript,增加此行;如何使用JavaScript下文有详细介绍
webView.getWebConfig().setWebStoragePermit(true);
webView.getWebConfig().setDataAbilityPermit(true);
webView.getWebConfig().setLoadsImagesPermit(true);
webView.getWebConfig().setMediaAutoReplay(true);
webView.getWebConfig().setLocationPermit(true);
webView.getWebConfig().setSecurityMode(WebConfig.SECURITY_SELF_ADAPTIVE);
webView.load(EXAMPLE_URL);
// HiLogUtils.PrintLog("webView.load======>>>"+EXAMPLE_URL);
}
@Override
public void onActive() {
super.onActive();
}
@Override
public void onForeground(Intent intent) {
super.onForeground(intent);
}
private void setWindowBgToAdaptWebView() {
final String backgroundFileName = "_bg.jpg";
File file = new File(getContext().getFilesDir(), backgroundFileName);
if (file.exists()) {
getWindow().setBackground(file.getPath());
return;
}
PixelMap pixelMap = createBgPixelMap();
if (pixelMap == null) {
return;
}
ImagePacker imagePacker = ImagePacker.create();
try (OutputStream outputStream = new FileOutputStream(file)) {
ImagePacker.PackingOptions options = new ImagePacker.PackingOptions();
if (!imagePacker.initializePacking(outputStream, options)) {
return;
}
if (!imagePacker.addImage(pixelMap)) {
return;
}
if (imagePacker.finalizePacking() < 0) {
return;
}
} catch (IOException e) {
e.printStackTrace();
} finally {
imagePacker.release();
}
getWindow().setBackground(file.getPath());
}
private PixelMap createBgPixelMap() {
final int length = 10;
PixelMap.InitializationOptions initializationOptions = new PixelMap.InitializationOptions();
initializationOptions.size = new Size(length, length);
initializationOptions.pixelFormat = PixelFormat.ARGB_8888;
initializationOptions.editable = true;
int[] defaultColors = new int[length * length];
return PixelMap.create(defaultColors, initializationOptions);
}
}
第三步查看运行效果
这时候我们在看一下运行效果
更多精彩内容,请见华为开发者官方论坛→https://developer.huawei.com/consumer/cn/forum/home?ha_source=sanfang
【HarmonyOS】【Demo】【JAVA UI】 鸿蒙怎么在Webview上添加组件的更多相关文章
- cocos2d-x 3.4版本,videoPlayer和webView上添加sprite等cocos控件
本帖源于小弟自己在项目过程中为了解决在cocos2d-x中实现ios里videoPlayer自定义控件的需求,所以挖出来大神的一个帖子http://www.cocoachina.com/bbs/rea ...
- HarmonyOS Java UI之DirectionalLayout布局
在之前的章节中我使用的是Java 代码构建UI界面,从本节开始,将使用XML构建UI界面. 使用XML构建UI(默认你已经会在项目中创建XML布局文件)界面相对Java代码构建的好处是:结构清晰,代码 ...
- HarmonyOS Java UI之DependentLayout布局示例
DependentLayout简介 DependentLayout意为相对位置布局,与DirectionalLayout相比较有更多的排布方式,每个组件可以指定相对于其他同级组件的位置,也可以指定相对 ...
- WebView 上传文件 WebChromeClient之openFileChooser函数
原链接:http://blog.saymagic.cn/2015/11/08/webview-upload.html?utm_source=tuicool&utm_medium=referra ...
- Java Project部署到Tomcat服务器上
所有的JAVA程序员,在编写WEB程序时,一般都通过工具如 MyEclipse,编写一个WEB Project,通过工具让这个WEB程序和Tomcat关联.其实在我们可以通过JAVA程序部署到Tomc ...
- 【Java Spring Cloud 实战之路】添加一个SpringBootAdmin监控
0. 前言 在之前的几章中,我们先搭建了一个项目骨架,又搭建了一个使用nacos的gateway网关项目,网关项目中并没有配置太多的东西.现在我们就接着搭建在Spring Cloud 微服务中另一个重 ...
- 面向UI编程:ui.js 1.1 使用观察者模式完成组件之间数据流转,彻底分离组件之间的耦合,完成组件的高内聚
开头想明确一些概念,因为有些概念不明确会导致很多问题,比如你写这个框架为什么不去解决啥啥啥的问题,哎,心累. 什么是框架? 百度的解释:框架(Framework)是整个或部分系统的可重用设计,表现为一 ...
- java~gradle构建公用包并上传到仓库
java~gradle构建公用包并上传到仓库 我们一般会把公用的代码放在一个包里,然后其它 项目可以直接使用,就像你使用第三方包一样! 仓库 存储包的地方叫做仓库,一般可以分为本地仓库和远程仓库,本地 ...
- java~gradle构建公用包并上传到仓库~使用私有仓库的包
在新的项目里使用仓库的包 上一讲中我们说了java~gradle构建公用包并上传到仓库,如何发布公用的非自启动类的包到私有仓库,而这一讲我们将学习如何使用这些包,就像我们使用spring框架里的功能包 ...
随机推荐
- .NetCore下构建自己的服务配置中心-手动造轮子
本人主要利用IdentityServer4以及SignalR来实现,IdentityServer4作为认证,SignalR来交互配置,这里一些代码可能就是部分提出来,主要介绍实现原理及方法 实现配置中 ...
- (随手记)Javascript 的parseInt函数,在IE和非IE内核浏览器运行的不同结果
一段JS小程序: var str = "09"; var itr = parseInt(str); alert(itr); IE下运行,alert(0); 火狐和chrome下运行 ...
- 基于windows环境VsCode的ESP32开发环境搭建
1. 基于windows环境VsCode的ESP32开发环境搭建,网上有各类教程,但是我实测却不行. 例如我在vscode内安装的乐鑫插件,扩展配置项是下图这样: 而百度的各类博文却都是这样: 经过网 ...
- 51 Nod 1134 最长递增子序列 (动态规划基础)
原题链接:1134 最长递增子序列 题目分析:长度为 的数列 有多达 个子序列,但我们应用动态规划法仍可以很高效地求出最长递增子序列().这里介绍两种方法. 先考虑用下列变量设计动态规划的算法. ...
- C#进程调用FFmpeg操作音视频
项目背景 因为公司需要对音视频做一些操作,比如说对系统用户的发音和背景视频进行合成,以及对多个音视频之间进行合成,还有就是在指定的源背景音频中按照对应的规则在视频的多少秒钟内插入一段客户发音等一些复杂 ...
- [爱偷懒的程序员系列]-Section 1. “懒”是一切需求的根源
一直认为"懒"推进了科技的发展,因为"懒"而促生了各种各样的需求.科技的进步加速了各种信息的交互频率,站在台面上说是因为业务需要提高效率,成本需要降低,服务需要 ...
- 《剑指offer》刷题目录
<剑指offer>刷题目录 面试题03. 数组中重复的数字 面试题04. 二维数组中的查找 面试题05. 替换空格 面试题06. 从尾到头打印链表 面试题07. 重建二叉树 面试题09. ...
- Java基础(十)——枚举与注解
一.枚举 1.介绍 枚举类:类的对象只有有限个,确定的.当需要定义一组常量时,强烈建议使用枚举类.如果枚举类中只有一个对象,则可以作为单例模式的实现. 使用 enum 定义的枚举类默认继承了 java ...
- JVM完整详解:内存分配+运行原理+回收算法+GC参数等
不管是BAT面试,还是工作实践中的JVM调优以及参数设置,或者内存溢出检测等,都需要涉及到Java虚拟机的内存模型.内存分配,以及回收算法机制等,这些都是必考.必会技能. JVM内存模型 JVM内存模 ...
- 毫米转像素dpi
public static double MillimeterToPixel_X(double length) //length是毫米,1厘米=10毫米 { System.Windows.Forms. ...