• 学习鸿蒙基础(11)


    目录

    一、Navigation容器

    二、web组件

    三、video视频组件

    四、动画

    1、属性动画 .animation()

    2、 转场动画 transition()  配合animateTo()

    3、页面间转场动画


    一、Navigation容器

    Navigation组件一般作为页面的根容器,包括单页面、分栏和自适应三种显示模式。同时,Navigation提供了属性来设置页面的标题栏、工具栏、导航栏等。
    Navigation组件的页面包含主页和内容页。主页由标题栏、内容区和工具栏组成,可在内容区中使用NavRouter子组件实现导航栏功能。内容页主要显示NavDestination子组件中的内容。
    NavRouter是配合Navigation使用的特殊子组件,默认提供点击响应处理,不需要开发者自定义点击事件逻辑。
    NavRouter有且仅有两个子组件,其中第二个子组件必须是NavDestination。NavDestination是配合NavRouter使用的特殊子组件,用于显示Navigation组件的内容页。当开发者点击NavRouter组件时,会跳转到对应的NavDestination内容区。

    1. @Entry
    2. @Component
    3. struct PageNavigation {
    4. @State message: string = 'Hello World'
    5. private list: Object[] = [{ name: "ccb", value: "中国建设银行" }, {
    6. name: "icbc", value: "中国工商银行" }, { name: "cnc", value: "中国银行" }]
    7. build() {
    8. Navigation() {
    9. List() {
    10. ForEach(this.list, (item) => {
    11. ListItem() {
    12. NavRouter() {
    13. Text(item.name).backgroundColor(Color.White).fontSize(30)
    14. .textAlign(TextAlign.Center).width("100%")
    15. NavDestination() {
    16. Text(item.value).width("100%").height("100%").backgroundColor(Color.White)
    17. }
    18. }
    19. }
    20. })
    21. }
    22. }
    23. .mode(NavigationMode.Auto)
    24. .title("笔记")
    25. .backgroundColor(Color.White)
    26. .menus([{
    27. value: '',
    28. icon: "images/search.png",
    29. action: () => {
    30. console.log("点击了搜索")
    31. }
    32. }])
    33. .toolBar({
    34. items: [{
    35. value: '左侧',
    36. icon: "images/search.png",
    37. action: () => {
    38. console.log("点击了左侧")
    39. }
    40. }, {
    41. value: '右侧',
    42. icon: "images/search.png",
    43. action: () => {
    44. console.log("点击了右侧")
    45. }
    46. }]
    47. })
    48. .width("100%")
    49. .height("100%")
    50. }
    51. }
    二、web组件

    Web组件的使用非常简单,只需要在Page目录下的ArkTS文件中创建一个Web组件,传入两个参数就可以了。其中src指定引用的网页路径,controller为组件的控制器,通过controller绑定Web组件,用于实现对Web组件的控制。

    1. import webview from '@ohos.web.webview'
    2. @Entry
    3. @Component
    4. struct PageWeb {
    5. @State message: string = 'Hello World'
    6. webcontroller: WebviewController = new webview.WebviewController()
    7. build() {
    8. Row() {
    9. Column() {
    10. Button("前进").onClick(() => {
    11. this.webcontroller.forward()
    12. })
    13. Button("后退").onClick(() => {
    14. this.webcontroller.backward()
    15. })
    16. Button("刷新").onClick(() => {
    17. this.webcontroller.refresh()
    18. })
    19. Button("清除记录").onClick(() => {
    20. this.webcontroller.clearHistory()
    21. })
    22. Button("获取js中的方法").onClick(() => {
    23. this.webcontroller.runJavaScript('getUserInfo()', (err, info) => {
    24. if (!err) {
    25. console.log("mmmclock---", info)
    26. }
    27. })
    28. })
    29. Button("将数据注入到js中").onClick(() => {
    30. this.webcontroller.registerJavaScriptProxy({
    31. getName:()=>JSON.stringify({userName:"hju",passWord:"123"})
    32. }, "windowqq", ["getName"])
    33. this.webcontroller.refresh()
    34. })
    35. Web({
    36. // src:"www.baidu.com",
    37. src: $rawfile("clock.html"),
    38. controller: this.webcontroller
    39. }).onConsole(evt => {
    40. console.log(evt.message.getMessage())
    41. return false;
    42. })
    43. .textZoomRatio(150)
    44. .zoomAccess(true)
    45. .javaScriptAccess(true)
    46. }
    47. .width('100%')
    48. }
    49. .height('100%')
    50. }
    51. }
    1. //js代码里的方法
    2. var userName1=JSON.parse(windowqq.getName()).userName
    3. var password1=JSON.parse(windowqq.getName()).passWord
    4. const getUserInfo=()=>{
    5. return {
    6. userName:userName
    7. }
    8. }
    三、video视频组件

    在手机、平板或是智慧屏这些终端设备上,媒体功能可以算作是我们最常用的场景之一。无论是实现音频的播放录制、采集,还是视频的播放、切换、循环,亦或是相机的预览、拍照等功能,媒体组件都是必不可少的。以视频功能为例,在应用开发过程中,我们需要通过ArkU提供的Video组件为应用增加基础的视频播放功能。借助Video组件,我们可以实现视频的播放功能并控制其播放状态。常见的视频播放场景包括观看网络上的较为流行的短视频,也包括查看我们存储在本地的视频内容。

    因为没有真机。不知道video的使用效果如何。

    1. @Entry
    2. @Component
    3. struct PageVideo {
    4. @State message: string = 'Hello World'
    5. private controll :VideoController =new VideoController();
    6. build() {
    7. Row() {
    8. Column() {
    9. Video({
    10. src: $rawfile("2.mp4"),
    11. previewUri: "images/2.jpg",
    12. controller:this.controll
    13. }).width("100%").height(40).autoPlay(true)
    14. .controls(false)//进度条
    15. .onFinish(()=>{
    16. console.log("播放完毕")
    17. })
    18. }
    19. .width('100%')
    20. }
    21. .height('100%')
    22. }
    23. }
     四、动画

    ArkUI中,产生动画的方式是改变属性值且指定动画参数。动画参数包含了如动画时长、变化规律(即曲线)等参数。当属性值发生变化后,按照动画参数,从原来的状态过渡到新的状态,即形成一个动画。

    动画:1、页面内的动画(属性动画、显示动画、组件内转场动画)。2、页面间的动画(共享元素转场动画、页面转场动画)

    1、属性动画 .animation()
    1. //属性动画
    2. @Entry
    3. @Component
    4. struct PageAnim {
    5. @State message: string = 'Hello World'
    6. @State private widths:number=100
    7. @State list: string[] = ["子(鼠)", "丑(牛)", "寅(虎)", "卯(兔)"
    8. , "辰(龙)", "巳(蛇)", "午(马)", "未(羊)", "申(猴)", "酉(鸡)", "戌(狗)", "亥(猪)"]
    9. build() {
    10. Row() {
    11. Column() {
    12. Button("动画").onClick(()=>{
    13. // animateTo({duration:2000,curve:Curve.Ease},()=>{
    14. // this.widths=400;
    15. // })
    16. this.widths=300;
    17. })
    18. Text(this.message)
    19. .backgroundColor(Color.Gray)
    20. .fontSize(40)
    21. .width(this.widths)
    22. 属性动画。加在属性身上。属性改变的时候。动画展示
    23. .animation({duration:2000,curve:Curve.Linear})
    24. List(){
    25. ForEach(this.list,(item,index)=>{
    26. this.item(item,index)
    27. })
    28. }.margin(10)
    29. .padding(10)
    30. .borderRadius(3)
    31. .border({width:5,color:Color.Gray})
    32. }
    33. .width('100%').height('100%')
    34. }
    35. .height('100%')
    36. }
    37. @Builder
    38. item(item,index){
    39. Row(){
    40. Text(item).fontSize(35)
    41. Text("删除").fontSize(35).onClick(()=>{
    42. animateTo({duration:1000,curve:Curve.Linear},()=>{
    43. this.list.splice(index,1)
    44. })
    45. })
    46. }.backgroundColor(Color.Yellow)
    47. .width("100%")
    48. .height(60)
    49. .justifyContent(FlexAlign.Center)
    50. .margin({bottom:10})
    51. }
    52. }
    2、 转场动画 transition()  配合animateTo()
    1. // 转场动画
    2. import Animator from '@ohos.animator'
    3. @Entry
    4. @Component
    5. struct PageAnim1 {
    6. @State message: string = 'Hello World'
    7. @State isShow: boolean = false
    8. @State list: string[] = ["子(鼠)", "丑(牛)", "寅(虎)", "卯(兔)"
    9. , "辰(龙)", "巳(蛇)", "午(马)", "未(羊)", "申(猴)", "酉(鸡)", "戌(狗)", "亥(猪)"]
    10. build() {
    11. Row() {
    12. Column() {
    13. Button("显示/隐藏").onClick(() => {
    14. animateTo({
    15. duration:1000,
    16. curve: Curve.Linear
    17. },()=>{
    18. this.isShow = !this.isShow
    19. })
    20. })
    21. if (this.isShow) {
    22. Text(this.message)
    23. .fontSize(50).backgroundColor(Color.Yellow)
    24. .fontWeight(FontWeight.Bold)
    25. // .transition({
    26. // type: TransitionType.Insert,
    27. // translate:{x:-200,y:0},
    28. // opacity:0
    29. // })
    30. // .transition({
    31. // type:TransitionType.Delete,
    32. // translate:{x:-200,y:0},
    33. // opacity:0
    34. // })
    35. .transition({
    36. type:TransitionType.All,
    37. translate:{x:-200,y:0},
    38. opacity:0
    39. })
    40. }
    41. List(){
    42. ForEach(this.list,(item,index)=>{
    43. ListItem(){
    44. this.item(item,index)
    45. }.backgroundColor(Color.Yellow)
    46. .width("100%")
    47. .height(60)
    48. .margin({bottom:10})
    49. // .transition({
    50. // type:TransitionType.Insert
    51. // })
    52. .transition({
    53. type:TransitionType.Delete,
    54. translate:{x:-200},
    55. scale:{x:0,y:0}
    56. })
    57. // .transition({
    58. // type:TransitionType.Insert,
    59. // translate:{x:100}
    60. // })
    61. .height(100)
    62. },item=>item)
    63. }.margin(10)
    64. .padding(10)
    65. .borderRadius(3)
    66. .border({width:5,color:Color.Gray})
    67. }.height("100%")
    68. .width('100%')
    69. }
    70. .height('100%')
    71. }
    72. @Builder
    73. item(item,index){
    74. Row(){
    75. Text(item).fontSize(35)
    76. Text("删除").fontSize(35).onClick(()=>{
    77. animateTo({duration:1000,curve:Curve.Linear},()=>{
    78. this.list.splice(index,1)
    79. })
    80. })
    81. }
    82. }
    83. }
    3、页面间转场动画
    1. //页面间的跳转
    2. import router from '@ohos.router'
    3. @Entry
    4. @Component
    5. struct PageAnim_1 {
    6. @State message: string = 'Hello'
    7. build() {
    8. Row() {
    9. Column() {
    10. Button("走你").onClick(()=>{
    11. router.pushUrl({
    12. url:"pages/PageAnim_2"
    13. })
    14. })
    15. Text(this.message)
    16. .fontSize(50)
    17. .fontWeight(FontWeight.Bold)
    18. }
    19. .width('100%')
    20. }
    21. .height('100%')
    22. }
    23. pageTransition(){
    24. // 页面栈发生出栈操作,滑出
    25. PageTransitionExit({type:RouteType.Push,duration:2000})//入栈
    26. .slide(SlideEffect.Bottom)
    27. // 页面栈发生入栈操作,移入
    28. PageTransitionEnter({type:RouteType.Push,duration:2000})
    29. .slide(SlideEffect.Bottom)
    30. }
    31. }
    32. // 页面间的跳转
    33. import router from '@ohos.router'
    34. import Router from '@system.router'
    35. @Entry
    36. @Component
    37. struct PageAnim_2 {
    38. @State message: string = 'World'
    39. build() {
    40. Row() {
    41. Column() {
    42. Text(this.message)
    43. .fontSize(50)
    44. .fontWeight(FontWeight.Bold)
    45. Button("回来").onClick(()=>{
    46. router.pushUrl({
    47. url:"pages/PageAnim_1"
    48. })
    49. })
    50. }
    51. .width('100%')
    52. }
    53. .height('100%')
    54. }
    55. pageTransition(){
    56. // push出站
    57. PageTransitionExit({type:RouteType.Push,duration:2000})
    58. .slide(SlideEffect.Top)
    59. // pop入栈
    60. PageTransitionEnter({type:RouteType.Push,duration:2000})
    61. .slide(SlideEffect.Top)
    62. }
    63. }

  • 相关阅读:
    Mac 安装PHP swoole扩展
    2023.11.18 -自用hadoop高可用环境搭建命令
    Selenium 4 有哪些不一样?
    使用LangChain SQLChain连接LLM和SQL数据库
    Python和Tensorboard的下载和安装
    JSP 教学文档管理系统myeclipse开发mysql数据库bs框架java编程jdbc详细设计
    鸿鹄工程项目管理系统 Spring Cloud+Spring Boot+Mybatis+Vue+ElementUI+前后端分离构建工程项目管理系统项目背景
    Java项目硅谷课堂学习笔记-P6整合腾讯云对象存储和课程分类管理
    《QEMU/KVM源码分析与应用》读书笔记3 —— 第一章 QEMU与KVM概述
    数据结构 2.1 线性表的定义和基本操作
  • 原文地址:https://blog.csdn.net/huyawenz/article/details/137151565