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

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)

  2. 使用offset属性对图片进行偏移。

    @State imageOffsetInfo: OffsetModel = new OffsetModel(0, 0);
    Image(this.imagePixelMap)
    .offset({
    x: this.imageOffsetInfo.currentX,
    y: this.imageOffsetInfo.currentY
    })

  3. Image的objectFit属性设置为Cover,锁定图片宽高比,并使其能够超出父组件边界显示。

    复制代码
     Image(this.imagePixelMap)
         .objectFit(ImageFit.Cover)
  4. 提前计算图片信息,并通过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
  })
相关推荐
御承扬6 分钟前
HarmonyOS NEXT系列之定制化构建制品
华为·harmonyos
大土豆的bug记录1 小时前
鸿蒙拉起系统定位和app授权定位
前端·javascript·harmonyos
weixin_307779132 小时前
设计Mock华为昇腾GPU的MindSpore和CANN的库的流程与实现
c++·算法·华为·系统架构·gpu算力
挨踢攻城10 小时前
思科华为基础命令对比
华为·网络工程师·ccna·hcia·hcip·思科·厦门微思网络
熬了夜的程序员15 小时前
【华为机试】34. 在排序数组中查找元素的第一个和最后一个位置
数据结构·算法·华为od·华为·面试·golang
zhanshuo15 小时前
用鸿蒙做多人协作,真的可以跨屏秒同步!
harmonyos
zhanshuo15 小时前
HarmonyOS 开发:基于 ArkUI 实现复杂表单验证的最佳实践
harmonyos
嵌入之梦20 小时前
鸿蒙智能居家养老系统构思(续二)—— 适老化烹饪中心详细构思
智能家居·harmonyos·居家养老
鸿蒙开发工程师—阿辉21 小时前
HarmonyOS 应用拉起系列(一):应用与元服务互通方式
华为·harmonyos·arkts·鸿蒙
用户5951433221771 天前
HarmonyOS开发:ArkUI相对布局RelativeContainer解决层级嵌套较多问题
harmonyos