【每日学点HarmonyOS Next知识】上下拉动作、图片预览、组件边距、this获取、svg旋转

ops/2025/3/16 23:28:20/
1、HarmonyOS 怎么实现上拉刷新,并可以调接口和实现动画,下拉刷新同理?

怎么实现上拉刷新,并可以调接口和实现动画,下拉刷新同理

参考:https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/ts-container-refresh-V5

Refresh 可以进行页面下拉操作并显示刷新动效的容器组件。

2、HarmonyOS 如何实现图片预览?

建议使用Image组件替代,Image组件,可用于本地图片和网络图片的渲染展示,可以参考:https://gitee.com/harmonyos-cases/cases/tree/master/CommonAppDevelopment/feature/imageviewer

图片预览在应用开发中是一种常见场景,在诸如QQ、微信、微博等应用中均被广泛使用。本模块基于Image组件实现了简单的图片预览功能。

使用说明:

  1. 双指捏合对图片进行缩放
  2. 双击图片进行图片的大小切换,在放大状态下,双击可恢复默认状态
  3. 图片在放大模式下,滑动图片查看图片的对应位置

实现思路:

  1. 使用matrix实现图片的缩放。
@State matrix: matrix4.Matrix4Transit = matrix4.identity().copy();
Image(this.imagePixelMap).transform(this.matrix)
  1. 使用offset属性对图片进行偏移。
@State imageOffsetInfo: OffsetModel = new OffsetModel(0, 0);
Image(this.imagePixelMap).offset({x: this.imageOffsetInfo.currentX,y: this.imageOffsetInfo.currentY})
  1. Image的objectFit属性设置为Cover,锁定图片宽高比,并使其能够超出父组件边界显示。
    Image(this.imagePixelMap).objectFit(ImageFit.Cover)
  1. 提前计算图片信息,并通过Image的宽或高配合aspectRatio设置默认尺寸。
    initCurrentImageInfo(): void {this.matrix = matrix4.identity().copy();const imageSource: image.ImageSource = image.createImageSource(this.imageUri);imageSource.getImageInfo(0).then((data: image.ImageInfo) => {this.imageWHRatio = data.size.width / data.size.height;this.imageDefaultSize = this.calcImageDefaultSize(this.imageWHRatio, windowSizeManager.get());if (this.imageDefaultSize.width === windowSizeManager.get().width) {this.fitWH = "width";} else {this.fitWH = "height";}this.imageScaleInfo.maxScaleValue += this.fitWH === "width" ? (windowSizeManager.get().height / this.imageDefaultSize.height) : (windowSizeManager.get().width / this.imageDefaultSize.width);}).catch((err: BusinessError) => {console.error(`[error][getImageInfo]${err.message}`);});imageSource.createPixelMap().then((data: image.PixelMap) => {this.imagePixelMap = data;}).catch((err: BusinessError) => {console.error(`[error][createPixelMap]${err.message}`);});}Image(this.imagePixelMap).width(this.fitWH === "width" ? $r("app.string.image_default_width") : undefined).height(this.fitWH === "height" ? $r("app.string.image_default_height") : undefined).aspectRatio(this.imageWHRatio)
3、HarmonyOS 如何获取组件的padding值?

如何获取组件的padding值?

参考demo:

const TEST_TAG: string = "FrameNode"
@Entry
@Component
struct Index {build() {Column() {Text('我的测试').key('text').padding({top: '10vp',right: '20vp',bottom: '30vp',left: '40vp'})Button('获取padding').onClick(() => {let frameNode = this.getUIContext().getFrameNodeById('text')let userConfigPadding = frameNode?.getUserConfigPadding();console.log(TEST_TAG + JSON.stringify(userConfigPadding));})}.width('100%').height('100%').justifyContent(FlexAlign.Center)}
}
4、HarmonyOS LiveEventBus 无法获取this?

可以参考以下demo:

import { LiveEventBus } from '@ohos/liveeventbus';
import { MState } from '@ohos/liveeventbus';
import { Lifecycle } from '@ohos/liveeventbus';const KEY_TEST_CLOSE_ALL_PAGE = "key_test_close_all_page";@Entry({ routeName: "EditorPage" })
@Component
export struct EditorPage {@State message: string = 'Hello World';private mLifecycle?: Lifecycle;getLifecycle(): Lifecycle {if (this.mLifecycle) {return this.mLifecycle}return new Lifecycle(MState.STARTED)}aboutToAppear() {//创建生命周期感知对象this.mLifecycle = new Lifecycle(MState.STARTED)//订阅消息LiveEventBus.get<boolean>(KEY_TEST_CLOSE_ALL_PAGE).observe(this, {onChanged: (b: boolean) => {this.message}});}build() {Column() {}.width('100%').height('100%')}
}
5、HarmonyOS svg图标?

svg图标如何旋转角度

参考以下demo:

Image($rawfile('svgLevel.svg')).width(100).height(100).rotate({x: 0,y: 0,z: 1,angle: 180})

http://www.ppmy.cn/ops/166343.html

相关文章

Django REST Framework 中 ModelViewSet 的接口方法及参数详解,继承的方法和核心类方法,常用查询方法接口

第一部分&#xff08;ModelViewSet&#xff09; 一、ModelViewSet 的继承结构 ModelViewSet 继承自以下类&#xff1a; ModelViewSet (CreateModelMixin # 创建RetrieveModelMixin # 检索单个UpdateModelMixin # 更新DestroyModelMixin # 删除ListModelMixin …

【python】OpenCV—Hough Circle Transform

文章目录 1、功能描述2、代码实现3、效果展示4、完整代码5、涉及到的库函数6、参考 更多有趣的代码示例&#xff0c;可参考【Programming】 1、功能描述 2、代码实现 载入必要的库 import sys import cv2 as cv import numpy as np函数入口 if __name__ "__main__&qu…

Ubuntu 服务器安装 Python 环境 的详细指南

以下是 在 Ubuntu 上安装 Python 3.10 的详细步骤&#xff08;兼容 Ubuntu 20.04/22.04&#xff09;&#xff1a; 方法一&#xff1a;通过 PPA 仓库安装&#xff08;推荐&#xff09; 1. 添加 deadsnakes PPA sudo apt update sudo apt install software-properties-common s…

数据结构篇——线性表

一、引入 线性结构是数据结构中最为重要的一种数据存储结构之一&#xff0c;同时也是其他数据结构的基础。今天我们就来学学数据结构中的一种基础类型——线性表。 二、线性表的定义和特点 线性表的定义&#xff1a;用数据元素的有限序列表示。具体情况如下图所示&#xff1a;…

HarmonyOS学习第20天:让应用“找准方向”的地图与定位秘籍

引言&#xff1a;开启 HarmonyOS 应用的位置感知之旅 在这个信息爆炸的时代&#xff0c;我们的生活与地理位置信息紧密相连。无论是寻找一家心仪的餐厅&#xff0c;规划一次完美的旅行&#xff0c;还是追踪快递的实时位置&#xff0c;地图与定位服务都扮演着至关重要的角色。对…

idea maven 编译报错Java heap space解决方法

1.增加 Maven 编译的堆内存 我是用这个方法修改成功的 Maven 编译时使用的 JVM 堆内存可以通过设置 MAVEN_OPTS 环境变量来调整。 在 IntelliJ IDEA 中设置&#xff1a; 打开 IntelliJ IDEA 的设置&#xff08;File -> Settings 或 Ctrl Alt S&#xff09;。 导航到 Bui…

【DevOps】Backstage介绍及如何在Azure Kubernetes Service上进行部署

【DevOps】Backstage介绍及如何在Azure Kubernetes Service上进行部署 推荐超级课程: 本地离线DeepSeek AI方案部署实战教程【完全版】Docker快速入门到精通Kubernetes入门到大师通关课AWS云服务快速入门实战目录 【DevOps】Backstage介绍及如何在Azure Kubernetes Service上…

MySQL小练习

要求&#xff1a; 新建产品库mydb6_product&#xff0c;新建4张表如下&#xff1a; oemployees表 列1&#xff1a;id&#xff0c;整型&#xff0c;主键 列2&#xff1a;name&#xff0c;字符串&#xff0c;最大长度50&#xff0c;不能为空 列3&#xff1a;age&#xff0c;整型 …