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

相关文章

Python实例题之pygame开发打飞机游戏实例代码

《Python实例题之pygame开发打飞机游戏实例代码》对于python的学习者,能够写出一个飞机大战的程序代码,是不是感觉到非常的开心,:本文主要介绍Python实例题之pygame开发打飞机... 目录题目pygame-aircraft-game使用 Pygame 开发的打飞机游戏脚本代码解释初始化部

使用Python开发一个现代化屏幕取色器

《使用Python开发一个现代化屏幕取色器》在UI设计、网页开发等场景中,颜色拾取是高频需求,:本文主要介绍如何使用Python开发一个现代化屏幕取色器,有需要的小伙伴可以参考一下... 目录一、项目概述二、核心功能解析2.1 实时颜色追踪2.2 智能颜色显示三、效果展示四、实现步骤详解4.1 环境配置4.

华为鸿蒙HarmonyOS 5.1官宣7月开启升级! 首批支持名单公布

《华为鸿蒙HarmonyOS5.1官宣7月开启升级!首批支持名单公布》在刚刚结束的华为Pura80系列及全场景新品发布会上,除了众多新品的发布,还有一个消息也点燃了所有鸿蒙用户的期待,那就是Ha... 在今日的华为 Pura 80 系列及全场景新品发布会上,华为宣布鸿蒙 HarmonyOS 5.1 将于 7

Python使用smtplib库开发一个邮件自动发送工具

《Python使用smtplib库开发一个邮件自动发送工具》在现代软件开发中,自动化邮件发送是一个非常实用的功能,无论是系统通知、营销邮件、还是日常工作报告,Python的smtplib库都能帮助我们... 目录代码实现与知识点解析1. 导入必要的库2. 配置邮件服务器参数3. 创建邮件发送类4. 实现邮件

基于Python开发一个有趣的工作时长计算器

《基于Python开发一个有趣的工作时长计算器》随着远程办公和弹性工作制的兴起,个人及团队对于工作时长的准确统计需求日益增长,本文将使用Python和PyQt5打造一个工作时长计算器,感兴趣的小伙伴可... 目录概述功能介绍界面展示php软件使用步骤说明代码详解1.窗口初始化与布局2.工作时长计算核心逻辑3

python web 开发之Flask中间件与请求处理钩子的最佳实践

《pythonweb开发之Flask中间件与请求处理钩子的最佳实践》Flask作为轻量级Web框架,提供了灵活的请求处理机制,中间件和请求钩子允许开发者在请求处理的不同阶段插入自定义逻辑,实现诸如... 目录Flask中间件与请求处理钩子完全指南1. 引言2. 请求处理生命周期概述3. 请求钩子详解3.1

如何基于Python开发一个微信自动化工具

《如何基于Python开发一个微信自动化工具》在当今数字化办公场景中,自动化工具已成为提升工作效率的利器,本文将深入剖析一个基于Python的微信自动化工具开发全过程,有需要的小伙伴可以了解下... 目录概述功能全景1. 核心功能模块2. 特色功能效果展示1. 主界面概览2. 定时任务配置3. 操作日志演示

JavaScript实战:智能密码生成器开发指南

本文通过JavaScript实战开发智能密码生成器,详解如何运用crypto.getRandomValues实现加密级随机密码生成,包含多字符组合、安全强度可视化、易混淆字符排除等企业级功能。学习密码强度检测算法与信息熵计算原理,获取可直接嵌入项目的完整代码,提升Web应用的安全开发能力 目录

一文教你如何解决Python开发总是import出错的问题

《一文教你如何解决Python开发总是import出错的问题》经常朋友碰到Python开发的过程中import包报错的问题,所以本文将和大家介绍一下可编辑安装(EditableInstall)模式,可... 目录摘要1. 可编辑安装(Editable Install)模式到底在解决什么问题?2. 原理3.

Python+PyQt5开发一个Windows电脑启动项管理神器

《Python+PyQt5开发一个Windows电脑启动项管理神器》:本文主要介绍如何使用PyQt5开发一款颜值与功能并存的Windows启动项管理工具,不仅能查看/删除现有启动项,还能智能添加新... 目录开篇:为什么我们需要启动项管理工具功能全景图核心技术解析1. Windows注册表操作2. 启动文件