HarmonyOS NEXT应用开发案例——滑动页面信息隐藏与组件位移效果

Row() {…}
.height( r ( ′ a p p . i n t e g e r . h e i g h t o n e h u n d r e d ′ ) ) . w i d t h ( r('app.integer.height_one_hundred')) .width( r(app.integer.heightonehundred)).width(r(‘app.string.size_full’))
Scroll() {…}
}


2. 在第一和第二部分中,使用[@State装饰器]( )装饰相关的组件属性,使之与自定义组件的渲染绑定起来,状态变量改变时,UI会发生对应的渲染改变。用户头像的缩放通过改变其width和height属性值的大小来刷新,用户头像的位移通过改变其margin属性中的top和left的大小来刷新。其他一些组件的显隐变化通过改变其[opacity]( )属性值的大小来刷新。源码参考[SlideToHideAndDisplace](/harmonyos-cases/cases/blob/master/CommonAppDevelopment/feature/slidetohideanddisplace/src/main/ets/SlideToHideAndDisplace.ets)



@State userRowOpacity: number = 1;
@State userImageHeight: number = 50;

build() {
Column() {
Row() {

Text($r(‘app.string.phone_number’))
.opacity(this.userNameOpacity) // userNameOpacity控制顶部用户名的透明度
Blank()
Text(“设置”)
.opacity(this.userNameOpacity) // 设置的文字透明度与顶部用户名相同
Text(“客服”)
.opacity(this.userNameOpacity) // 客服的文字透明度与顶部用户名相同
}

  Row() {
    Image($r('app.media.batman'))
      .width(this.userImageHeight)
      .height(this.userImageHeight) // userImageHeight控制头像尺寸
      // userImageMarginTop和userImageMarginLeft控制头像在父容器内的位置
      .margin({ top: this.userImageMarginTop, left: this.userImageMarginLeft })

    Column() {...}
    .alignItems(HorizontalAlign.Start)
    .opacity(this.userRowOpacity) // 控制Row组件的透明度
  }
  ...
}

}


3. 第三部分页面滚动通过[Scroll]( )组件实现,其中第二栏和第三栏相似,使用[@Builder装饰器]( )封装了两个自定义构建函数IconAndDescription和CustomRow,增加代码复用。源码参考[SlideToHideAndDisplace](/harmonyos-cases/cases/blob/master/CommonAppDevelopment/feature/slidetohideanddisplace/src/main/ets/SlideToHideAndDisplace.ets)



// 自定义构建函数,将重复使用的UI元素抽象成一个方法。此处样式为:上方图标下方文字
@Builder
IconAndDescription(icon: Resource, description: string | Resource, iconSize?: Size, radius?: number) {
Column() {
Image(icon)
.size(iconSize === undefined ? { height: $r(‘app.integer.icon_default_height’),
width: $r(‘app.integer.icon_default_height’) } : iconSize)
.borderRadius(radius)
Text(description)
.margin({ top: $r(‘app.integer.margin_between_icon_and_description’) })
}
}

// 自定义构建函数。此处样式为:在Row组件中横向排列IconAndDescription
@Builder
CustomRow(iconsAndDescriptions: IconAndDescription[]) {
Row() {
ForEach(iconsAndDescriptions, (item: IconAndDescription) => {
this.IconAndDescription(item.icon, item.description)
})
}
.width( r ( ′ a p p . s t r i n g . s i z e f u l l ′ ) ) . j u s t i f y C o n t e n t ( F l e x A l i g n . S p a c e A r o u n d ) . p a d d i n g ( r('app.string.size_full')) .justifyContent(FlexAlign.SpaceAround) .padding( r(app.string.sizefull)).justifyContent(FlexAlign.SpaceAround).padding(r(‘app.integer.padding_small’))
.margin({ top: KaTeX parse error: Expected 'EOF', got '}' at position 31: …margin_small') }̲) .backgrou…r(‘app.color.color_transparent_aa’))
.borderRadius($r(‘app.integer.border_radius’))
}


#### 高性能知识点


本例中Scroll组件绑定onScroll滚动事件回调,onScroll属于频繁回调,在回调中需要尽量减少耗时和冗余操作,例如减少不必要的日志打印。


#### 工程结构&模块类型



slidetohideanddisplace // har包
|—model
| |—Util.ets // 提供测试数据类
|—SlideToHideAndDisplace.ets // 滑动变化效果实现页面


#### 模块依赖


不涉及。


#### 参考资料


[@State装饰器]( )


[@Builder装饰器]( )


[透明度设置]( )


[Scroll]( )


**为了能让大家更好的学习鸿蒙(HarmonyOS NEXT)开发技术,这边特意整理了《鸿蒙开发学习手册》(共计890页),希望对大家有所帮助:[`https://qr21.cn/FV7h05`]( )**


#### 《鸿蒙开发学习手册》:[`https://qr21.cn/FV7h05`]( )


**入门必看:[`https://qr21.cn/FV7h05`]( )**  
 1.  应用开发导读(ArkTS)  
 2.  ……


![](https://img-blog.csdnimg.cn/img_convert/14b990e58b198c48cab2c3c2f794e499.webp?x-oss-process=image/format,png)


**HarmonyOS 概念:[`https://qr21.cn/FV7h05`]( )**


1. 系统定义
2. 技术架构
3. 技术特性
4. 系统安全


![](https://img-blog.csdnimg.cn/img_convert/948e6364f67f7185fe65cba01024a24e.webp?x-oss-process=image/format,png)


**如何快速入门:[`https://qr21.cn/FV7h05`]( )**  
 1.  基本概念  
 2.  构建第一个ArkTS应用  
 3.  ……


![](https://img-blog.csdnimg.cn/img_convert/ca7dfba2d3a7e98ee60f2f3eac772a60.webp?x-oss-process=image/format,png)


**开发基础知识:[`https://qr21.cn/FV7h05`]( )**  
 1.  应用基础知识  
 2.  配置文件  
 3.  应用数据管理  
 4.  应用安全管理  
 5.  应用隐私保护  
 6.  三方应用调用管控机制  
 7.  资源分类与访问  
 8.  学习ArkTS语言  
 9.  ……


![](https://img-blog.csdnimg.cn/img_convert/e520760d6bbd0671faf9515867d9c98a.webp?x-oss-process=image/format,png)




**自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。**

**深知大多数HarmonyOS鸿蒙开发工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但对于培训机构动则几千的学费,着实压力不小。自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!**

**因此收集整理了一份《2024年HarmonyOS鸿蒙开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。**
![img](https://img-blog.csdnimg.cn/img_convert/b906e8680b8133594a503c088313f18d.png)
![img](https://img-blog.csdnimg.cn/img_convert/42cbed5949a099238a4b4f48a7f36abe.png)
![img](https://img-blog.csdnimg.cn/img_convert/21e45e7ecfd50b21d3ce85e88f553c85.png)

**既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上HarmonyOS鸿蒙开发知识点,真正体系化!**

**由于文件比较大,这里只是将部分目录大纲截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新**

**如果你觉得这些内容对你有帮助,可以添加VX:vip204888 (备注鸿蒙获取)**
![img](https://img-blog.csdnimg.cn/img_convert/951c8884143d38de10b2f6cc5d48d479.png)

**一个人可以走的很快,但一群人才能走的更远。不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎扫码加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!**

你觉得这些内容对你有帮助,可以添加VX:vip204888 (备注鸿蒙获取)**
[外链图片转存中...(img-erRkTfko-1712842904862)]

**一个人可以走的很快,但一群人才能走的更远。不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎扫码加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!**

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值