WanAndroid(鸿蒙版)开发的第六篇

2024-03-19 21:28

本文主要是介绍WanAndroid(鸿蒙版)开发的第六篇,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

前言

DevEco Studio版本:4.0.0.600

WanAndroid的API链接:玩Android 开放API-玩Android - wanandroid.com

其他篇文章参考:

1、WanAndroid(鸿蒙版)开发的第一篇

2、WanAndroid(鸿蒙版)开发的第二篇

3、WanAndroid(鸿蒙版)开发的第三篇

4、WanAndroid(鸿蒙版)开发的第四篇

5、WanAndroid(鸿蒙版)开发的第五篇

6、WanAndroid(鸿蒙版)开发的第六篇

效果

​ 

我的页面实现

从UI效果上可以看出是头部的背景+头像 和下面的业务item,因此整体可以使用RelativeContainer布局,业务item可以用column实现。

1、UI界面实现

build() {RelativeContainer() {Image($r('app.media.ic_background_mine')).width('100%').height(240).id('imageBg').alignRules({top: { anchor: '__container__', align: VerticalAlign.Top },middle: { anchor: '__container__', align: HorizontalAlign.Center }})CircleImageView({src: $r("app.media.ic_icon"), //本地图片资源radius: 60,border_Width: 1,border_Color: Color.Pink,}).id('imageIcon').margin({ top: 50 }).alignRules({top: { anchor: '__container__', align: VerticalAlign.Top },middle: { anchor: '__container__', align: HorizontalAlign.Center }})Text(decodeURIComponent(this.userName)).fontSize(20).fontColor(Color.Black).fontWeight(FontWeight.Bold).id('textUsername').margin({ top: 20 }).alignRules({top: { anchor: 'imageIcon', align: VerticalAlign.Bottom },middle: { anchor: '__container__', align: HorizontalAlign.Center }})Column() {Row() {Text('我的收藏').fontColor(Color.Black).margin({ left: 15 })Image($r("app.media.ic_mine_next")).width(25).height(22).margin({ right: 15 })}.justifyContent(FlexAlign.SpaceBetween).width('100%').height(50).margin({ top: 10 }).borderRadius(10).backgroundColor('#66CCCCCC').onClick(() => {if (this.isLogin) {router.pushUrl({ url: 'pages/mine/MineCollectPage' })} else {router.pushUrl({url: 'pages/LoginPage',params: {hideJump: true}})}})Row() {Text('个人积分').fontColor(Color.Black).margin({ left: 15 })Image($r("app.media.ic_mine_next")).width(25).height(22).margin({ right: 15 })}.justifyContent(FlexAlign.SpaceBetween).width('100%').height(50).margin({ top: 20 }).borderRadius(10).backgroundColor('#66CCCCCC').onClick(() => {if (this.isLogin) {router.pushUrl({ url: 'pages/mine/MineIntegralPage' })} else {router.pushUrl({url: 'pages/LoginPage',params: {hideJump: true}})}})Row() {Text('关于我们').fontColor(Color.Black).margin({ left: 15 })Image($r("app.media.ic_mine_next")).width(25).height(22).margin({ right: 15 })}.justifyContent(FlexAlign.SpaceBetween).width('100%').height(50).margin({ top: 20 }).borderRadius(10).backgroundColor('#66CCCCCC').onClick(() => {router.pushUrl({ url: 'pages/mine/MineAboutPage' })})Row() {Text('退出登录').fontColor(Color.Black).margin({ left: 15 })Image($r("app.media.ic_mine_next")).width(25).height(22).margin({ right: 15 })}.justifyContent(FlexAlign.SpaceBetween).width('100%').height(50).margin({ top: 20 }).borderRadius(10).backgroundColor('#66CCCCCC').visibility(this.isLogin ? Visibility.Visible : Visibility.None).onClick(() => {this.dialogController.open()})}.id('columnSetting').padding({ left: 10, right: 10 }).alignRules({top: { anchor: 'imageBg', align: VerticalAlign.Bottom },middle: { anchor: '__container__', align: HorizontalAlign.Center }}).width('100%')}.backgroundColor(Color.White).width('100%').height('100%')
}

2、退出登录弹窗

private dialogController = new CustomDialogController({builder: LoginOutDialog({ onLoginOut: () => {this.onLoginOut()} }),customStyle: true,alignment: DialogAlignment.Center,
})

3、执行退出登录请求

private onLoginOut() {HttpManager.getInstance().request<LoginOutBean>({method: RequestMethod.GET,url: 'https://www.wanandroid.com/user/logout/json' //wanAndroid的API:Banner}).then((result: LoginOutBean) => {LogUtils.info(TAG, "result: " + JSON.stringify(result))if (result.errorCode == 0) {this.clearUserData()//跳转到登录界面router.replaceUrl({ url: 'pages/LoginPage' })}}).catch((error) => {LogUtils.info(TAG, "error: " + JSON.stringify(error))})
}

4、清除用户数据

private clearUserData() {AppStorage.Set(Constants.APPSTORAGE_USERNAME, '')AppStorage.Set(Constants.APPSTORAGE_PASSWORD, '')AppStorage.Set(Constants.APPSTORAGE_ISLOGIN, false)
}

我的收藏

1、获取收藏数据

private getCollectData() {HttpManager.getInstance().request<CollectBean>({method: RequestMethod.GET,header: { "Cookie": `loginUserName=${this.userName}; token_pass=${this.token_pass}` },url: `https://www.wanandroid.com/lg/collect/list/${this.pageNum}/json` //wanAndroid的API:积分排行}).then((result: CollectBean) => {LogUtils.info(TAG, "getCollectData  result: " + JSON.stringify(result))if (this.isRefresh) {this.controller.finishRefresh()} else {this.controller.finishLoadMore()}if (result.errorCode == 0) {if (this.isRefresh) {this.collectListData = result.data.datas} else {if (result.data.datas.length > 0) {this.collectListData = this.collectListData.concat(result.data.datas)} else {promptAction.showToast({ message: '没有更多数据啦!' })}}}this.dialogController.close()}).catch((error) => {LogUtils.info(TAG, "error: " + JSON.stringify(error))if (this.isRefresh) {this.controller.finishRefresh()} else {this.controller.finishLoadMore()}this.dialogController.close()})
}

2、UI界面实现

build() {Column() {AppTitleBar({ title: "我的收藏" })RefreshListView({list: this.collectListData,controller: this.controller,isEnableLog: true,paddingRefresh: { left: 10, right: 10, top: 5, bottom: 5 },refreshLayout: (item: CollectItemBean, index: number): void => this.itemLayout(item, index),onItemClick: (item: CollectItemBean, index: number) => {LogUtils.info(TAG, "点击了:index: " + index + " item: " + item)router.pushUrl({url: 'pages/WebPage',params: {title: item.title,uriLink: this.isValidUrl(item.link) ? item.link : 'https://www.wanandroid.com/' + item.link  //规避部分情况下偶现link链接不完整}}, router.RouterMode.Single)},onRefresh: () => {//下拉刷新this.isRefresh = truethis.pageNum = 0this.getCollectData()},onLoadMore: () => {//上拉加载this.isRefresh = falsethis.pageNum++this.getCollectData()}}).flexShrink(1)}.width('100%').height('100%').backgroundColor('#F1F3F5')}@BuilderitemLayout(item: CollectItemBean, index: number) {RelativeContainer() {//标题Text(HtmlUtils.formatStr(item.title)).fontColor('#333333').fontWeight(FontWeight.Bold).maxLines(2).textOverflow({overflow: TextOverflow.Ellipsis}).fontSize(20).id("textTitle").alignRules({top: { anchor: 'textAuthor', align: VerticalAlign.Bottom },left: { anchor: '__container__', align: HorizontalAlign.Start }})//更新时间Text("作者:" + item.author /*+ "\t分类:" + item.chapterName*/ + "\t\t收藏时间:" + item.niceDate).fontColor('#666666').fontSize(14).id("textNiceDate").alignRules({bottom: { anchor: '__container__', align: VerticalAlign.Bottom },left: { anchor: '__container__', align: HorizontalAlign.Start }})}.width('100%').height(90).padding(10).borderRadius(10).backgroundColor(Color.White)}

个人积分

1、获取积分数据

private getIntegralData() {HttpManager.getInstance().request<IntegralBean>({method: RequestMethod.GET,header: { "Cookie": `loginUserName=${this.userName}; token_pass=${this.token_pass}` },url: `https://www.wanandroid.com/lg/coin/userinfo/json` //wanAndroid的API:积分排行}).then((result: IntegralBean) => {this.IntegralDataState = trueLogUtils.info(TAG, "getIntegralData  result: " + JSON.stringify(result))if (result.errorCode == 0) {this.userIntegralData = result.data}LogUtils.info(TAG, "IntegralDataState: " + this.IntegralDataState + "  LeaderboardDataState: " + this.LeaderboardDataState)if (this.IntegralDataState && this.LeaderboardDataState) {this.dialogController.close()}}).catch((error) => {this.IntegralDataState = trueLogUtils.info(TAG, "error: " + JSON.stringify(error))LogUtils.info(TAG, "IntegralDataState: " + this.IntegralDataState + "  LeaderboardDataState: " + this.LeaderboardDataState)if (this.IntegralDataState && this.LeaderboardDataState) {this.dialogController.close()}})
}//积分排行
private getLeaderboardData() {HttpManager.getInstance().request<LeaderboardBean>({method: RequestMethod.GET,header: {"Content-Type": "application/json"},url: `https://www.wanandroid.com/coin/rank/1/json` //wanAndroid的API:积分排行}).then((result: LeaderboardBean) => {this.LeaderboardDataState = trueLogUtils.info(TAG, "getLeaderboardData  result: " + JSON.stringify(result))if (result.errorCode == 0) {this.leaderboardListData = result.data.datas}LogUtils.info(TAG, "IntegralDataState: " + this.IntegralDataState + "  LeaderboardDataState: " + this.LeaderboardDataState)if (this.IntegralDataState && this.LeaderboardDataState) {this.dialogController.close()}}).catch((error) => {this.LeaderboardDataState = trueLogUtils.info(TAG, "error: " + JSON.stringify(error))LogUtils.info(TAG, "IntegralDataState: " + this.IntegralDataState + "  LeaderboardDataState: " + this.LeaderboardDataState)if (this.IntegralDataState && this.LeaderboardDataState) {this.dialogController.close()}})
}

2、UI界面实现

build() {Column() {AppTitleBar({ title: '个人积分' })Row() {Text('个人积分信息').fontSize(20).fontWeight(FontWeight.Bold)Text('积分获取列表 >>').fontSize(14).fontColor('#66666666').fontWeight(FontWeight.Bold).onClick(() => {router.pushUrl({ url: 'pages/mine/PointAcquisitionPage' })})}.width('100%').justifyContent(FlexAlign.SpaceBetween).height(50).padding({ left: 15, right: 15 }).backgroundColor('#66CCCCCC')Stack() {Column() {Text(decodeURIComponent(this.userName)).fontColor(Color.Black).fontSize(28).margin({ top: 5, bottom: 5 }).fontWeight(FontWeight.Bold)Text('积分:' + this.userIntegralData.coinCount).fontColor(Color.Red).margin({ top: 10, bottom: 10 }).fontSize(20)Text('等级:' + this.userIntegralData.level).fontColor(Color.Green).margin({ top: 10, bottom: 10 }).fontSize(20)Text('排名:' + this.userIntegralData.rank).fontColor('#1296db').margin({ top: 10, bottom: 10 }).fontSize(20)}.alignItems(HorizontalAlign.Start).padding(10).borderRadius(10).backgroundColor('#F1F3F5').width('100%')}.width('100%').padding({ left: 10, right: 10, top: 15, bottom: 15 })//积分排行榜Row() {Text('积分排行榜').fontSize(20).fontWeight(FontWeight.Bold)Text('更多信息 >>').fontSize(14).fontColor('#66666666').fontWeight(FontWeight.Bold).onClick(() => {router.pushUrl({ url: 'pages/mine/LeaderboardPage' })})}.width('100%').justifyContent(FlexAlign.SpaceBetween).height(50).padding({ left: 15, right: 15 }).backgroundColor('#66CCCCCC')if (this.leaderboardListData.length > 3) {LeaderboardItemLayout({ item: this.leaderboardListData[0] }).padding(10)LeaderboardItemLayout({ item: this.leaderboardListData[1] }).padding(10)LeaderboardItemLayout({ item: this.leaderboardListData[2] }).padding(10)}}.width('100%').height('100%')
}

3、效果

关于我们界面实现

1、效果

2、原理分析

要实现‘关于我们’文字和背景图片随着手指滑动而变化的效果。监听最外层布局的滑动事件,然后动态设置绝对偏移量(通过position方法实现)。

图片设置绝对偏移量

Image($r('app.media.ic_background_mine')).width('100%').height(this.imageHeight).position({ x: 0, y: this.offsetY - this.pullMaxHeight })

‘关于我们’文字根据滑动距离动态设置大小

Text('关于我们').fontColor(Color.White).fontSize(this.fontSize).position({ x: this.textInitialX, y: this.textInitialY + this.textPositionY })

底部内容设置绝对偏移量

Column() {//......}.padding(10).position({ y: this.imageHeight + this.offsetY - this.pullMaxHeight })

3、详细代码

import LogUtils from '@app/BaseLibrary/src/main/ets/utils/LogUtils'
import router from '@ohos.router';const TAG = 'MineAboutPage--- ';/*** 关于我们*/
@Entry
@Component
struct MineAboutPage {// 列表y坐标偏移量@State offsetY: number = 0// 按下的y坐标private downY = 0// 上一次手指抬起后的背景偏移量private lastUpOffsetY = 0//上一次手指抬起后的文字偏移量private lastUpTextPositionY = 0// 下拉的最大高度private pullMaxHeight: number = 100//收缩后title的高度private pushMaxHeight: number = 56//图片高度private imageHeight: number = 300//文字初始偏移量,xprivate textInitialX: number = 60//文字初始偏移量,yprivate textInitialY: number = 60//文字初始大小private textFontSize: number = 40@State fontSize: number = this.textFontSize@State textPositionY: number = 0@State pushStatus: boolean = falsebuild() {Column() {Stack() {Image($r('app.media.ic_background_mine')).width('100%').height(this.imageHeight).position({ x: 0, y: this.offsetY - this.pullMaxHeight })Image($r('app.media.ic_back_white')).width(26).height(26).margin({ left: 20, right: 20, top: 15 }).onClick(() => {router.back()})Text('关于我们').fontColor(Color.White).fontSize(this.fontSize).position({ x: this.textInitialX, y: this.textInitialY + this.textPositionY })}.alignContent(Alignment.TopStart).width('100%').height(this.imageHeight - this.pullMaxHeight).id('stackHeader').alignRules({left: { anchor: '__container__', align: HorizontalAlign.Start },top: { anchor: '__container__', align: VerticalAlign.Top }})Column() {Text("APP介绍:").fontColor(Color.Black).fontSize(25).fontWeight(FontWeight.Bold).alignSelf(ItemAlign.Start)Text("\t这是一款鸿蒙应用,基于鸿洋大佬WanAndroid的API进行开发,目前主要功能包括:登录、注册,搜索,首页,导航,项目,个人信息等模块。").lineHeight(25).fontSize(20).fontColor(Color.Black).margin({ top: 10 })Text() {Span('\t可以在').fontSize(20)Span('WanAndroid(鸿蒙)').decoration({ type: TextDecorationType.Underline, color: Color.Blue }).fontSize(18).onClick(() => {router.pushUrl({url: 'pages/WebPage',params: {title: 'WanAndroid(鸿蒙)',uriLink: 'https://blog.csdn.net/abner_crazy/category_12565384.html',isShowCollect: false,}}, router.RouterMode.Single)}).fontColor(Color.Blue)Span('专栏中关注项目相关介绍,后续会在该专栏中更新项目开发相关知识点,欢迎大家在专栏下方留言进行沟通交流。').fontSize(20)}.lineHeight(25).margin({ top: 20 })}.padding(10).position({ y: this.imageHeight + this.offsetY - this.pullMaxHeight })}.onTouch((event) => this.touchEvent(event)).width('100%').height('100%')}touchEvent(event: TouchEvent) {switch (event.type) {case TouchType.Down: // 手指按下// 记录按下的y坐标this.downY = event.touches[0].ybreakcase TouchType.Move: // 手指移动this.touchMovePull(event.touches[0].y)breakcase TouchType.Up: // 手指抬起case TouchType.Cancel: // 触摸意外中断if (this.offsetY > 0) {this.pushStatus = falsethis.setOriginalStatus()} else { //手指抬起后在初始态上面,即上划this.pushStatus = truethis.lastUpOffsetY = this.offsetYthis.lastUpTextPositionY = this.textPositionY}break}}// 手指移动,处理下拉touchMovePull(touchesY: number) {//最大差值let maxHeight = this.imageHeight - this.pullMaxHeight - this.pushMaxHeight// 滑动的偏移量let offset = touchesY - this.downY// 偏移量的值缓慢增加let height = offset * 0.50if (this.pushStatus) {this.offsetY = height + this.lastUpOffsetY > this.pullMaxHeight ? this.pullMaxHeight : height + this.lastUpOffsetY} else {this.offsetY = height > this.pullMaxHeight ? this.pullMaxHeight : height}LogUtils.info(TAG, " offsetY: " + this.offsetY + " height: " + height + "  lastUpOffsetY: " + this.lastUpOffsetY)//滑到title高度停止if (this.offsetY < -maxHeight) {this.offsetY = -maxHeight}//文字Y轴偏移量if (this.pushStatus) {this.textPositionY = this.lastUpTextPositionY + offset * 0.16 /*> 104 ? 104 : this.lastUpTextPositionY + offset * 0.16*/} else {this.textPositionY = offset * 0.16}if (this.textPositionY < -42) {this.textPositionY = -42}if (this.textFontSize + this.textPositionY * 0.4762 <= this.textFontSize) {this.fontSize = this.textFontSize + this.textPositionY * 0.4762} else {this.fontSize = this.textFontSize}LogUtils.info(TAG, "touchMovePull   offsetY: " + this.offsetY + " textPositionY: "+ this.textPositionY + "  pushStatus: " + this.pushStatus + "    fontSize: " + this.fontSize)}setOriginalStatus() {animateTo({duration: 300,}, () => {this.offsetY = 0this.textPositionY = 0})}
}

总结:

到这篇文章为止整个WanAndroid(鸿蒙)版本的功能开发基本完成了,后续会继续更新一些优化点,大家可以关注后面更新内容~~

源代码地址:WanHarmony: WanAndroid的鸿蒙版本

这篇关于WanAndroid(鸿蒙版)开发的第六篇的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/827285

相关文章

Android开发中gradle下载缓慢的问题级解决方法

《Android开发中gradle下载缓慢的问题级解决方法》本文介绍了解决Android开发中Gradle下载缓慢问题的几种方法,本文给大家介绍的非常详细,感兴趣的朋友跟随小编一起看看吧... 目录一、网络环境优化二、Gradle版本与配置优化三、其他优化措施针对android开发中Gradle下载缓慢的问

使用Go语言开发一个命令行文件管理工具

《使用Go语言开发一个命令行文件管理工具》这篇文章主要为大家详细介绍了如何使用Go语言开发一款命令行文件管理工具,支持批量重命名,删除,创建,移动文件,需要的小伙伴可以了解下... 目录一、工具功能一览二、核心代码解析1. 主程序结构2. 批量重命名3. 批量删除4. 创建文件/目录5. 批量移动三、如何安

Android 悬浮窗开发示例((动态权限请求 | 前台服务和通知 | 悬浮窗创建 )

《Android悬浮窗开发示例((动态权限请求|前台服务和通知|悬浮窗创建)》本文介绍了Android悬浮窗的实现效果,包括动态权限请求、前台服务和通知的使用,悬浮窗权限需要动态申请并引导... 目录一、悬浮窗 动态权限请求1、动态请求权限2、悬浮窗权限说明3、检查动态权限4、申请动态权限5、权限设置完毕后

基于Python开发PPTX压缩工具

《基于Python开发PPTX压缩工具》在日常办公中,PPT文件往往因为图片过大而导致文件体积过大,不便于传输和存储,所以本文将使用Python开发一个PPTX压缩工具,需要的可以了解下... 目录引言全部代码环境准备代码结构代码实现运行结果引言在日常办公中,PPT文件往往因为图片过大而导致文件体积过大,

使用DeepSeek API 结合VSCode提升开发效率

《使用DeepSeekAPI结合VSCode提升开发效率》:本文主要介绍DeepSeekAPI与VisualStudioCode(VSCode)结合使用,以提升软件开发效率,具有一定的参考价值... 目录引言准备工作安装必要的 VSCode 扩展配置 DeepSeek API1. 创建 API 请求文件2.

基于Python开发电脑定时关机工具

《基于Python开发电脑定时关机工具》这篇文章主要为大家详细介绍了如何基于Python开发一个电脑定时关机工具,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录1. 简介2. 运行效果3. 相关源码1. 简介这个程序就像一个“忠实的管家”,帮你按时关掉电脑,而且全程不需要你多做

Java中的Opencv简介与开发环境部署方法

《Java中的Opencv简介与开发环境部署方法》OpenCV是一个开源的计算机视觉和图像处理库,提供了丰富的图像处理算法和工具,它支持多种图像处理和计算机视觉算法,可以用于物体识别与跟踪、图像分割与... 目录1.Opencv简介Opencv的应用2.Java使用OpenCV进行图像操作opencv安装j

基于Qt开发一个简单的OFD阅读器

《基于Qt开发一个简单的OFD阅读器》这篇文章主要为大家详细介绍了如何使用Qt框架开发一个功能强大且性能优异的OFD阅读器,文中的示例代码讲解详细,有需要的小伙伴可以参考一下... 目录摘要引言一、OFD文件格式解析二、文档结构解析三、页面渲染四、用户交互五、性能优化六、示例代码七、未来发展方向八、结论摘要

你的华为手机升级了吗? 鸿蒙NEXT多连推5.0.123版本变化颇多

《你的华为手机升级了吗?鸿蒙NEXT多连推5.0.123版本变化颇多》现在的手机系统更新可不仅仅是修修补补那么简单了,华为手机的鸿蒙系统最近可是动作频频,给用户们带来了不少惊喜... 为了让用户的使用体验变得很好,华为手机不仅发布了一系列给力的新机,还在操作系统方面进行了疯狂的发力。尤其是近期,不仅鸿蒙O

在 VSCode 中配置 C++ 开发环境的详细教程

《在VSCode中配置C++开发环境的详细教程》本文详细介绍了如何在VisualStudioCode(VSCode)中配置C++开发环境,包括安装必要的工具、配置编译器、设置调试环境等步骤,通... 目录如何在 VSCode 中配置 C++ 开发环境:详细教程1. 什么是 VSCode?2. 安装 VSCo