热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

【HarmonyOS】【JAVAUI】webView动画加载资源加载动画交互

在HarmonyOS中webview加载网页的时候,需要有进度条,或者加载动画进行用户感知的交互,这样可以优化用户体验,因此

 在HarmonyOS中webview加载网页的时候,需要有进度条,或者加载动画进行用户感知的交互,这样可以优化用户体验,因此今天写一篇加载动画(效果如下)用于同学们进行学习,怎么实现?首先我们需要学习“CommonDialog”“ WebView”“动画开发指导”三个知识储备

我们分为“准备阶段”,“自定义CommonDialog实现”,“动画实现”,“webview的实现”,“运行效果”五个步骤进行实现。

20220119-090152(WeLinkPC).gif

1.准备阶段

在resources \base\ media\目录下准备一张loading图片(图片如下)存放位置如下

image.png

Loading图片

image.png

存放位置

2.       自定义CommonDialog的实现

2.1新建xml命名为general_dialog.xml,在该xml文件绘画一张图片(代码如下)

效果图如下

image.png

2.2新建GeneralDialog文件,我们参考HarmonyOS 的自定义CommonDialog场景示例

具体代码如下

package com.harmony.alliance.mydemo.utils;import java.util.Optional;import com.harmony.alliance.mydemo.ResourceTable;
import ohos.agp.animation.Animator;
import ohos.agp.animation.AnimatorProperty;
import ohos.agp.animation.AnimatorValue;
import ohos.agp.colors.RgbColor;
import ohos.agp.components.*;
import ohos.agp.components.element.ShapeElement;
import ohos.agp.utils.LayoutAlignment;
import ohos.agp.window.dialog.CommonDialog;
import ohos.agp.window.service.WindowManager;
import ohos.app.Context;public class GeneralDialog {private float dim = -1f;private CommonDialog sDialog;private Context mContext;private boolean mOutsideTouchClosable = false;public GeneralDialog(Context context){this.mContext=context;}public void show() {if (sDialog != null) {sDialog.show();if (dim >= 0) {changeDialogDim(sDialog, dim);}}}public void remove(){if (sDialog != null) {sDialog.destroy();}}public void create() {sDialog = new CommonDialog(mContext);sDialog.setSize(ComponentContainer.LayoutConfig.MATCH_CONTENT, ComponentContainer.LayoutConfig.MATCH_CONTENT);sDialog.setAlignment(LayoutAlignment.CENTER);sDialog.setOffset(0,0);sDialog.setTransparent(true);sDialog.setContentCustomComponent(initDialog(sDialog));sDialog.setAutoClosable(mOutsideTouchClosable);}private void changeDialogDim(CommonDialog dialog, float dim) {Optional configOpt = dialog.getWindow().getLayoutConfig();configOpt.ifPresent(config -> {config.dim = dim;dialog.getWindow().setLayoutConfig(config);});}public interface ClickedListener{void onClick(GeneralDialog dialog);}private Component initDialog(CommonDialog sDialog) {Component dialogLayout = LayoutScatter.getInstance(mContext).parse(ResourceTable.Layout_general_dialog, null, false);dialogLayout.setBackground(new ShapeElement(){{setRgbColor(RgbColor.fromArgbInt(ResourceTool.getColor(mContext, ResourceTable.Color_bg_dialog_light, 0xffffff)));setCornerRadius(ResourceTool.getFloat(mContext, ResourceTable.Float_dialog_corner_radius, 0));}});Image image= (Image) dialogLayout.findComponentById(ResourceTable.Id_loading);return dialogLayout;}}

 2.2.3在MainAbility的onStart的方法下调用如下代码

GeneralDialog mGeneralDialog = new GeneralDialog(getContext());mGeneralDialog.create();mGeneralDialog.show();

效果如下

image.png

2.3动画实现

2.3.1动画的功能我们可以参考HarmonyOS动画开发指导的AnimatorProperty的相关知识点,接下来我们initDialog开启image的动画功能代码如下

Image image= (Image) dialogLayout.findComponentById(ResourceTable.Id_loading);animatorProperty = new AnimatorProperty();animatorProperty.setTarget(image);animatorProperty.rotate(360)//无限循环.setLoopedCount(AnimatorValue.INFINITE)//反弹力效果.setCurveType(Animator.CurveType.BOUNCE);if(sDialog!=null){sDialog.setDestroyedListener(new CommonDialog.DestroyedListener() {@Overridepublic void onDestroy() {if(animatorProperty.isRunning()){animatorProperty.stop();}}});}

2.3.2在GeneralDialog类写一个开启动画方法(代码如下)

public void StartanimatorProperty(){if(animatorProperty!=null&&animatorProperty.isRunning()){animatorProperty.stop();}if(animatorProperty!=null&&!animatorProperty.isRunning()){if(!animatorProperty.isRunning()){animatorProperty.start();}}}

2.3.3封一个工具类用于显示和播放和消失loading弹框(代码如下)

package com.harmony.alliance.mydemo.utils;import ohos.app.Context;public class LoadingDialogUtils {private static GeneralDialog mGeneralDialog;public static GeneralDialog getInstance(Context mContext){if(mGeneralDialog==null){mGeneralDialog=new GeneralDialog(mContext);}return mGeneralDialog;}public static void show(Context mContext){LoadingDialogUtils.getInstance(mContext);mGeneralDialog.create();mGeneralDialog.show();mGeneralDialog.StartanimatorProperty();}public static void dismiss(Context mContext){LoadingDialogUtils.getInstance(mContext);mGeneralDialog.remove();}
}

2.3.4开启动画代码如下

LoadingDialogUtils.show(MainAbility.this);

关闭动画代码如下

LoadingDialogUtils.dismiss(MainAbility.this);

 2.4   webview的实现

webview 加载网页我们可以参考HarmonyOS的WebView的组件

2.4.1我们学观测Web状态的setWebAgent(代码如下)

webView.setWebAgent(new WebAgent() {@Overridepublic void onLoadingPage(WebView webview, String url, PixelMap favicon) {super.onLoadingPage(webview, url, favicon);//todo 页面开始加载时自定义处理 开启动画}@Overridepublic void onPageLoaded(WebView webview, String url) {super.onPageLoaded(webview, url);// todo 页面加载结束后自定义处理 关闭动画}@Overridepublic void onLoadingContent(WebView webview, String url) {super.onLoadingContent(webview, url);// 加载资源时自定义处理}@Overridepublic void onError(WebView webview, ResourceRequest request, ResourceError error) {super.onError(webview, request, error);//todo 发生错误时自定义处理 关闭动画}});

2.4.2我们新建abilitySlice的java类,新建layout布局代码如下

2.4.3 webview的java类代码如下

package com.harmony.alliance.mydemo.slice;import com.harmony.alliance.mydemo.ResourceTable;
import com.harmony.alliance.mydemo.utils.LoadingDialogUtils;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.webengine.*;
import ohos.media.image.PixelMap;public class NewMyWebview extends AbilitySlice {private WebView mMyWebview;private static final String EXAMPLE_URL = "https://developer.harmonyos.com/cn/docs/documentation/doc-references/js-apis-fa-calls-pa-examples-0000000000618000";@Overrideprotected void onStart(Intent intent) {super.onStart(intent);setUIContent(ResourceTable.Layout_new_my_webview);mMyWebview= (WebView) findComponentById(ResourceTable.Id_my_webView);WebConfig webConfig = mMyWebview.getWebConfig();webConfig.setJavascriptPermit(true);webConfig.setWebStoragePermit(true);webConfig.setDataAbilityPermit(true);webConfig.setLoadsImagesPermit(true);webConfig.setMediaAutoReplay(true);webConfig.setLocationPermit(true);webConfig.setSecurityMode(WebConfig.SECURITY_SELF_ADAPTIVE);mMyWebview.setWebAgent(new WebAgent() {@Overridepublic void onLoadingPage(WebView webview, String url, PixelMap favicon) {super.onLoadingPage(webview, url, favicon);//todo 页面开始加载时自定义处理 开启动画LoadingDialogUtils.show(NewMyWebview.this);}@Overridepublic void onPageLoaded(WebView webview, String url) {super.onPageLoaded(webview, url);// todo 页面加载结束后自定义处理 关闭动画LoadingDialogUtils.dismiss(NewMyWebview.this);}@Overridepublic void onLoadingContent(WebView webview, String url) {super.onLoadingContent(webview, url);// 加载资源时自定义处理}@Overridepublic void onError(WebView webview, ResourceRequest request, ResourceError error) {super.onError(webview, request, error);//todo 发生错误时自定义处理 关闭动画LoadingDialogUtils.dismiss(NewMyWebview.this);}});mMyWebview.load(EXAMPLE_URL);}
}

2.5   运行效果如下

全部代码如下

2.5.1general_dialog.xml代码

2.5.2GeneralDialog的java类

//请根据实际工程/包名引入package com.harmony.alliance.mydemo.utils;import java.util.Optional;import com.harmony.alliance.mydemo.ResourceTable;import ohos.agp.animation.Animator;import ohos.agp.animation.AnimatorProperty;import ohos.agp.animation.AnimatorValue;import ohos.agp.colors.RgbColor;import ohos.agp.components.*;import ohos.agp.components.element.ShapeElement;import ohos.agp.utils.LayoutAlignment;import ohos.agp.window.dialog.CommonDialog;import ohos.agp.window.service.WindowManager;import ohos.app.Context;public class GeneralDialog {private float dim = -1f;private AnimatorProperty animatorProperty;private CommonDialog sDialog;private Context mContext;private boolean mOutsideTouchClosable = false;public GeneralDialog(Context context){this.mContext=context;}public void show() {if (sDialog != null) {sDialog.show();if (dim >= 0) {changeDialogDim(sDialog, dim);}}}public void remove(){if (sDialog != null) {sDialog.destroy();}}public void create() {sDialog = new CommonDialog(mContext);sDialog.setSize(ComponentContainer.LayoutConfig.MATCH_CONTENT, ComponentContainer.LayoutConfig.MATCH_CONTENT);sDialog.setAlignment(LayoutAlignment.CENTER);sDialog.setOffset(0,0);sDialog.setTransparent(true);sDialog.setContentCustomComponent(initDialog(sDialog));sDialog.setAutoClosable(mOutsideTouchClosable);}public void StartanimatorProperty(){if(animatorProperty!=null&&animatorProperty.isRunning()){animatorProperty.stop();}if(animatorProperty!=null&&!animatorProperty.isRunning()){if(!animatorProperty.isRunning()){animatorProperty.start();}}}private void changeDialogDim(CommonDialog dialog, float dim) {Optional configOpt = dialog.getWindow().getLayoutConfig();configOpt.ifPresent(config -> {config.dim = dim;dialog.getWindow().setLayoutConfig(config);});}public interface ClickedListener{void onClick(GeneralDialog dialog);}private Component initDialog(CommonDialog sDialog) {Component dialogLayout = LayoutScatter.getInstance(mContext).parse(ResourceTable.Layout_general_dialog, null, false);dialogLayout.setBackground(new ShapeElement(){{setRgbColor(RgbColor.fromArgbInt(ResourceTool.getColor(mContext, ResourceTable.Color_bg_dialog_light, 0xffffff)));setCornerRadius(ResourceTool.getFloat(mContext, ResourceTable.Float_dialog_corner_radius, 0));}});Image image= (Image) dialogLayout.findComponentById(ResourceTable.Id_loading);animatorProperty = new AnimatorProperty();animatorProperty.setTarget(image);animatorProperty.rotate(360)//无限循环.setLoopedCount(AnimatorValue.INFINITE)//反弹力效果.setCurveType(Animator.CurveType.BOUNCE);if(sDialog!=null){sDialog.setDestroyedListener(new CommonDialog.DestroyedListener() {@Overridepublic void onDestroy() {if(animatorProperty.isRunning()){animatorProperty.stop();}}});}return dialogLayout;}}

2.5.3LoadingDialogUtils的工具类如下

package com.harmony.alliance.mydemo.utils;import ohos.app.Context;public class LoadingDialogUtils {private static GeneralDialog mGeneralDialog;private static GeneralDialog getInstance(Context mContext){if(mGeneralDialog==null){mGeneralDialog=new GeneralDialog(mContext);}return mGeneralDialog;}public static void show(Context mContext){LoadingDialogUtils.getInstance(mContext);mGeneralDialog.create();mGeneralDialog.show();mGeneralDialog.StartanimatorProperty();}public static void dismiss(Context mContext){LoadingDialogUtils.getInstance(mContext);mGeneralDialog.remove();} }

2.5.4webViewAbilitySlice的layout的xml代码如下

2.5.5 WebViewAbiltySlice的类代码如下

package com.harmony.alliance.mydemo.slice;import com.harmony.alliance.mydemo.ResourceTable;import com.harmony.alliance.mydemo.utils.LoadingDialogUtils;import ohos.aafwk.ability.AbilitySlice;import ohos.aafwk.content.Intent;import ohos.agp.components.webengine.*;import ohos.media.image.PixelMap;public class NewMyWebview extends AbilitySlice {private WebView mMyWebview;private static final String EXAMPLE_URL = "https://developer.harmonyos.com/cn/docs/documentation/doc-references/js-apis-fa-calls-pa-examples-0000000000618000";@Overrideprotected void onStart(Intent intent) {super.onStart(intent);setUIContent(ResourceTable.Layout_new_my_webview);mMyWebview= (WebView) findComponentById(ResourceTable.Id_my_webView);WebConfig webConfig = mMyWebview.getWebConfig();webConfig.setJavascriptPermit(true);webConfig.setWebStoragePermit(true);webConfig.setDataAbilityPermit(true);webConfig.setLoadsImagesPermit(true);webConfig.setMediaAutoReplay(true);webConfig.setLocationPermit(true);webConfig.setSecurityMode(WebConfig.SECURITY_SELF_ADAPTIVE);mMyWebview.setWebAgent(new WebAgent() {@Overridepublic void onLoadingPage(WebView webview, String url, PixelMap favicon) {super.onLoadingPage(webview, url, favicon);//todo 页面开始加载时自定义处理 开启动画LoadingDialogUtils.show(NewMyWebview.this);}@Overridepublic void onPageLoaded(WebView webview, String url) {super.onPageLoaded(webview, url);// todo 页面加载结束后自定义处理 关闭动画LoadingDialogUtils.dismiss(NewMyWebview.this);}@Overridepublic void onLoadingContent(WebView webview, String url) {super.onLoadingContent(webview, url);// 加载资源时自定义处理}@Overridepublic void onError(WebView webview, ResourceRequest request, ResourceError error) {super.onError(webview, request, error);//todo 发生错误时自定义处理 关闭动画LoadingDialogUtils.dismiss(NewMyWebview.this);}});mMyWebview.load(EXAMPLE_URL);}}

效果如下

20220119-090152(WeLinkPC).gif


推荐阅读
  • 树莓派语音控制的配置方法和步骤
    本文介绍了在树莓派上实现语音控制的配置方法和步骤。首先感谢博主Eoman的帮助,文章参考了他的内容。树莓派的配置需要通过sudo raspi-config进行,然后使用Eoman的控制方法,即安装wiringPi库并编写控制引脚的脚本。具体的安装步骤和脚本编写方法在文章中详细介绍。 ... [详细]
  • Android开发实现的计时器功能示例
    本文分享了Android开发实现的计时器功能示例,包括效果图、布局和按钮的使用。通过使用Chronometer控件,可以实现计时器功能。该示例适用于Android平台,供开发者参考。 ... [详细]
  • Java太阳系小游戏分析和源码详解
    本文介绍了一个基于Java的太阳系小游戏的分析和源码详解。通过对面向对象的知识的学习和实践,作者实现了太阳系各行星绕太阳转的效果。文章详细介绍了游戏的设计思路和源码结构,包括工具类、常量、图片加载、面板等。通过这个小游戏的制作,读者可以巩固和应用所学的知识,如类的继承、方法的重载与重写、多态和封装等。 ... [详细]
  • YOLOv7基于自己的数据集从零构建模型完整训练、推理计算超详细教程
    本文介绍了关于人工智能、神经网络和深度学习的知识点,并提供了YOLOv7基于自己的数据集从零构建模型完整训练、推理计算的详细教程。文章还提到了郑州最低生活保障的话题。对于从事目标检测任务的人来说,YOLO是一个熟悉的模型。文章还提到了yolov4和yolov6的相关内容,以及选择模型的优化思路。 ... [详细]
  • 本文介绍了使用kotlin实现动画效果的方法,包括上下移动、放大缩小、旋转等功能。通过代码示例演示了如何使用ObjectAnimator和AnimatorSet来实现动画效果,并提供了实现抖动效果的代码。同时还介绍了如何使用translationY和translationX来实现上下和左右移动的效果。最后还提供了一个anim_small.xml文件的代码示例,可以用来实现放大缩小的效果。 ... [详细]
  • Spring源码解密之默认标签的解析方式分析
    本文分析了Spring源码解密中默认标签的解析方式。通过对命名空间的判断,区分默认命名空间和自定义命名空间,并采用不同的解析方式。其中,bean标签的解析最为复杂和重要。 ... [详细]
  • 开发笔记:加密&json&StringIO模块&BytesIO模块
    篇首语:本文由编程笔记#小编为大家整理,主要介绍了加密&json&StringIO模块&BytesIO模块相关的知识,希望对你有一定的参考价值。一、加密加密 ... [详细]
  • Android系统移植与调试之如何修改Android设备状态条上音量加减键在横竖屏切换的时候的显示于隐藏
    本文介绍了如何修改Android设备状态条上音量加减键在横竖屏切换时的显示与隐藏。通过修改系统文件system_bar.xml实现了该功能,并分享了解决思路和经验。 ... [详细]
  • Python瓦片图下载、合并、绘图、标记的代码示例
    本文提供了Python瓦片图下载、合并、绘图、标记的代码示例,包括下载代码、多线程下载、图像处理等功能。通过参考geoserver,使用PIL、cv2、numpy、gdal、osr等库实现了瓦片图的下载、合并、绘图和标记功能。代码示例详细介绍了各个功能的实现方法,供读者参考使用。 ... [详细]
  • 本文介绍了Android 7的学习笔记总结,包括最新的移动架构视频、大厂安卓面试真题和项目实战源码讲义。同时还分享了开源的完整内容,并提醒读者在使用FileProvider适配时要注意不同模块的AndroidManfiest.xml中配置的xml文件名必须不同,否则会出现问题。 ... [详细]
  • 在Xamarin XAML语言中如何在页面级别构建ControlTemplate控件模板
    本文介绍了在Xamarin XAML语言中如何在页面级别构建ControlTemplate控件模板的方法和步骤,包括将ResourceDictionary添加到页面中以及在ResourceDictionary中实现模板的构建。通过本文的阅读,读者可以了解到在Xamarin XAML语言中构建控件模板的具体操作步骤和语法形式。 ... [详细]
  • r2dbc配置多数据源
    R2dbc配置多数据源问题根据官网配置r2dbc连接mysql多数据源所遇到的问题pom配置可以参考官网,不过我这样配置会报错我并没有这样配置将以下内容添加到pom.xml文件d ... [详细]
  • 基于dlib的人脸68特征点提取(眨眼张嘴检测)python版本
    文章目录引言开发环境和库流程设计张嘴和闭眼的检测引言(1)利用Dlib官方训练好的模型“shape_predictor_68_face_landmarks.dat”进行68个点标定 ... [详细]
  • iOS超签签名服务器搭建及其优劣势
    本文介绍了搭建iOS超签签名服务器的原因和优势,包括不掉签、用户可以直接安装不需要信任、体验好等。同时也提到了超签的劣势,即一个证书只能安装100个,成本较高。文章还详细介绍了超签的实现原理,包括用户请求服务器安装mobileconfig文件、服务器调用苹果接口添加udid等步骤。最后,还提到了生成mobileconfig文件和导出AppleWorldwideDeveloperRelationsCertificationAuthority证书的方法。 ... [详细]
  • (三)多表代码生成的实现方法
    本文介绍了一种实现多表代码生成的方法,使用了java代码和org.jeecg框架中的相关类和接口。通过设置主表配置,可以生成父子表的数据模型。 ... [详细]
author-avatar
小小可爱天使
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有