【每日学点HarmonyOS Next知识】图片拖动、动画放大、列表高度、返回键监听、分割线颜色

devtools/2025/3/18 9:33:40/
1、HarmonyOS image 组件的图片可以随意拖动?

mage组件默认拖拽效果,draggable设置为true时,组件可拖拽,参考以下文档:https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/image-V5
在这里插入图片描述

HarmonyOS中Image组件默认支持长按触发拖拽事件。具体来说,Image组件的拖拽能力是通过设置组件的draggable属性来实现的。通常情况下,Text、TextInput、TextArea、Hyperlink、Image和RichEditor组件的draggable属性是默认为true的,这意味着这些组件可以被长按并拖拽。可以通过自定义image相关组件,实现部分把draggable关了

2、HarmonyOS 换成model后动画放大实现不了?

listitem内容为object,不能直接用 ==判断当前item,可以改用index确定当前点击的item,修改如下:(其他案例参考:https://gitee.com/harmonyos-cases/cases/tree/master/CommonAppDevelopment/feature/listexchange)

import curves from '@ohos.curves';
@Entry
@Component
struct model {@State private arr: titleStrModel[] = []@State dragItem: titleStrModel = new titleStrModel()@State scaleItem: titleStrModel = new titleStrModel()@State neighborItem: titleStrModel = new titleStrModel()@State selectedIndex:number = -1@State neighborScale: number = -1private dragRefOffset: number = 0@State offsetX: number = 0@State offsetY: number = 0private ITEM_INTV: number = 120aboutToAppear(): void {const d = new titleStrModel()d.name = 'wewewe'const b = new titleStrModel()b.name = 'adadad'const h = new titleStrModel()h.name = 'dfdfdf'this.arr.push(d)this.arr.push(b)this.arr.push(h)}//根据当前index确定item是否缩放scaleSelect(item: titleStrModel):void {this.selectedIndex = this.arr.indexOf(item)this.arr[this.selectedIndex].scale = 1.05}itemMove(index: number, newIndex: number): void {let tmp = this.arr.splice(index, 1)this.arr.splice(newIndex, 0, tmp[0])}build() {Stack() {List({ space: 20, initialIndex: 0 }) {ForEach(this.arr, (item: titleStrModel) => {ListItem() {Row() {Text('' + item.name).width('100%').height(100).fontSize(16).textAlign(TextAlign.Center).borderRadius(10).backgroundColor(0xFFFFFF)Image($r('app.media.app_icon')).width(100).height(100)}.shadow(this.scaleItem.name == item.name ? { radius: 70, color:Color.Blue, offsetX: 0, offsetY: 0 } :{ radius: 0, color: Color.Blue, offsetX: 0, offsetY: 0 }).animation({ curve: Curve.Sharp, duration: 300 })}.margin({ left: 12, right: 12 }).scale({ x: item.scale, y: item.scale }).zIndex(this.dragItem.name == item.name ? 1 : 0).translate(this.dragItem.name == item.name ? { y: this.offsetY } : { y: 0 }).gesture(//以下组合手势为顺序识别,当长按手势事件未正常触发时则不会触发拖动手势事件GestureGroup(GestureMode.Sequence,LongPressGesture({ repeat: true }).onAction((event?: GestureEvent) => {animateTo({ curve: Curve.Friction, duration: 300 }, () => {this.scaleItem = itemthis.scaleSelect(item)})}).onActionEnd(() => {animateTo({ curve: Curve.Friction, duration: 300 }, () => {this.scaleItem = new titleStrModel()})}),PanGesture({ fingers: 1, direction: null, distance: 0 }).onActionStart(() => {this.dragItem = itemthis.dragRefOffset = 0}).onActionUpdate((event: GestureEvent) => {this.offsetY = event.offsetY - this.dragRefOffset// console.log('Y:' + this.offsetY.toString())this.neighborItem = new titleStrModel()let index = this.arr.indexOf(item)let curveValue = curves.initCurve(Curve.Sharp)let value: number = 0//根据位移计算相邻项的缩放if (this.offsetY < 0) {value = curveValue.interpolate(-this.offsetY / this.ITEM_INTV)this.neighborItem = this.arr[index-1]this.neighborScale = 1 - value / 20;console.log('neighborScale:' + this.neighborScale.toString())} else if (this.offsetY > 0) {value = curveValue.interpolate(this.offsetY / this.ITEM_INTV)this.neighborItem = this.arr[index+1]this.neighborScale = 1 - value / 20;}//根据位移交换排序if (this.offsetY > this.ITEM_INTV / 2) {animateTo({ curve: curves.interpolatingSpring(0, 1, 400, 38) }, () => {this.offsetY -= this.ITEM_INTVthis.dragRefOffset += this.ITEM_INTVthis.itemMove(index, index + 1)})} else if (this.offsetY < -this.ITEM_INTV / 2) {animateTo({ curve: curves.interpolatingSpring(0, 1, 400, 38) }, () => {this.offsetY += this.ITEM_INTVthis.dragRefOffset -= this.ITEM_INTVthis.itemMove(index, index - 1)})}}).onActionEnd((event: GestureEvent) => {animateTo({ curve: curves.interpolatingSpring(0, 1, 400, 38) }, () => {this.dragItem = new titleStrModel()this.neighborItem = new titleStrModel()//动画结束将缩放还原this.arr[this.arr.indexOf(this.scaleItem)].scale = 1})animateTo({curve: curves.interpolatingSpring(14, 1, 170, 17), delay: 150}, () => {this.scaleItem = new titleStrModel()})})).onCancel(() => {animateTo({ curve: curves.interpolatingSpring(0, 1, 400, 38) }, () => {this.dragItem = new titleStrModel()this.neighborItem = new titleStrModel()})animateTo({curve: curves.interpolatingSpring(14, 1, 170, 17), delay: 150}, () => {this.scaleItem = new titleStrModel()})}))})}}.width('100%').height('100%').backgroundColor(0xDCDCDC).padding({ top: 5 })}
}
class titleStrModel {name:string = ''scale:number = 1 //增加scale属性, 每个item都有对应的scale值
}
3、HarmonyOS 为什么List的高度设置100% 还是显示不全?

问题是解决了(方法:子控件Column,List都加上layoutWeight(1)),有点疑惑,想请教一下:

  1. 为什么外部用了layoutWeight(1) 内部所有的Column与List也要用layoutWeight(1) 想知道原理
  2. 外部我已经计算高度了(TNAppUIData.getPageHeight())为什么Tabs我不用layoutWeight(1)还是占满屏幕的剩余高度,我计算给的高度不起作用?

问题1:父容器尺寸确定时,设置了layoutWeight属性的子元素与兄弟元素占主轴尺寸按照权重进行分配,前提是父容器尺寸确定。比如共有三个元素且都用了layoutWeight(1),它们按照“1:1:1”的比例均分父容器主轴方向的空间。与.width(‘33%’)、.width(‘34%’)、.width(‘33%’)效果相同。

问题2:tabs是默认占满屏幕的

4、HarmonyOS 物理返回键监听?

customdialog弹窗打开时,按物理返回键,希望不关闭弹窗
可以使用onBackPress当用户点击返回按钮时触发,仅@Entry装饰的自定义组件生效。返回true表示页面自己处理返回逻辑,不进行页面路由;返回false表示使用默认的路由返回逻辑,不设置返回值按照false处理。参考文档:https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/ts-custom-component-lifecycle-V5#onbackpress

onBackPress?(): void | boolean

当用户点击返回按钮时触发,仅@Entry装饰的自定义组件生效。返回true表示页面自己处理返回逻辑,不进行页面路由;返回false表示使用默认的路由返回逻辑,不设置返回值按照false处理。

// xxx.ets
@Entry
@Component
struct IndexComponent {@State textColor: Color = Color.Black;onPageShow() {this.textColor = Color.Blue;console.info('IndexComponent onPageShow');}onPageHide() {this.textColor = Color.Transparent;console.info('IndexComponent onPageHide');}onBackPress() {this.textColor = Color.Red;console.info('IndexComponent onBackPress');}build() {Column() {Text('Hello World').fontColor(this.textColor).fontSize(30).margin(30)}.width('100%')}
}
5、HarmonyOS list divider中startmargin前面的颜色怎么和listitem保持一致?

目前使用list的divider属性并不能满足你想实现的效果,可以自定义实现分隔线效果,参考如下代码:

build() {Row(){List(){ListItem(){Flex({direction:FlexDirection.Column,justifyContent:FlexAlign.SpaceBetween}){Text('test')Divider().color(Color.Red)// .height(15).strokeWidth(5).margin({left:50})}}.backgroundColor(Color.Blue).height(100)ListItem(){Flex({direction:FlexDirection.Column,justifyContent:FlexAlign.SpaceBetween}){Text('test')Divider().color(Color.Red).strokeWidth(5).margin({left:50,bottom:0})}}.backgroundColor(Color.Orange).height(100)ListItem(){Flex({direction:FlexDirection.Column,justifyContent:FlexAlign.SpaceBetween}){Text('test')Divider().color(Color.Red).strokeWidth(5).margin({left:50})}}.backgroundColor(Color.Yellow).height(100)}// .divider({strokeWidth:5,color:Color.Red,startMargin:50}).width('100%').height('100%').backgroundColor(Color.Gray)}
}

http://www.ppmy.cn/devtools/168033.html

相关文章

Java高频面试之集合-13

hello啊&#xff0c;各位观众姥爷们&#xff01;&#xff01;&#xff01;本baby今天来报道了&#xff01;哈哈哈哈哈嗝&#x1f436; 面试官&#xff1a;为什么 hash 函数能降哈希碰撞&#xff1f; 哈希函数通过以下核心机制有效降低碰撞概率&#xff0c;确保不同输入尽可能映…

搭建主从服务器

任务需求 客户端通过访问 www.nihao.com 后&#xff0c;能够通过 dns 域名解析&#xff0c;访问到 nginx 服务中由 nfs 共享的首页文件&#xff0c;内容为&#xff1a;Very good, you have successfully set up the system. 各个主机能够实现时间同步&#xff0c;并且都开启防…

JVM常用概念之FPU溢出

问题 当自己的代码根本没有浮点或矢量运算&#xff0c;JVM在x86生成的机器代码为什么会用到XMM 寄存器? 基础知识 FPU 和矢量单元在现代 CPU 中随处可见&#xff0c;在许多情况下&#xff0c;它们为 FPU 特定的操作提供了一组备用寄存器。例如&#xff0c;Intel x86_64 中的…

Android Room 框架测试模块源码深度剖析(五)

Android Room 框架测试模块源码深度剖析 一、引言 在 Android 开发中&#xff0c;数据持久化是一项重要的功能&#xff0c;而 Android Room 框架为开发者提供了一个强大且便捷的方式来实现本地数据库操作。然而&#xff0c;为了确保 Room 数据库操作的正确性和稳定性&#xf…

K8S学习之基础三十四:K8S之监控Prometheus部署pod版

使用 Kubernetes Pod 的方式部署 Prometheus 是一种常见的方法&#xff0c;尤其是在容器化和微服务架构中。以下是详细的步骤&#xff1a; 1. 创建命名空间&#xff08;可选&#xff09; 为了方便管理&#xff0c;可以为 Prometheus 创建一个单独的命名空间。 yaml 复制 a…

Spark 优化作业性能以及处理数据倾斜问题

1. 如何优化Spark作业的性能&#xff1f; 优化Spark作业性能可以从多个方面入手&#xff0c;以下是一些关键的优化策略&#xff1a; &#xff08;1&#xff09;资源调优 增加Executor数量&#xff1a;更多的Executor可以并行处理更多任务。 增加Executor内存&#xff1a;通过…

Zabbix安装(保姆级教程)

Zabbix 是一款开源的企业级监控解决方案,能够监控网络的多个参数以及服务器、虚拟机、应用程序、服务、数据库、网站和云的健康状况和完整性。它提供了灵活的通知机制,允许用户为几乎任何事件配置基于电子邮件的告警,从而能够快速响应服务器问题。Zabbix 基于存储的数据提供…

provide/inject源码实现

在 Vue 3 中&#xff0c;provide 和 inject 是通过 Vue 的响应式系统和组件实例机制实现的&#xff0c;底层是依赖 Vue 3 中的 Proxy 和 Reactive 来实现跨层级的数据传递和响应式绑定。以下是一个简化版的实现逻辑&#xff0c;帮助理解 Vue 3 中 provide 和 inject 是如何实现…