91欧美超碰AV自拍|国产成年人性爱视频免费看|亚洲 日韩 欧美一厂二区入|人人看人人爽人人操aV|丝袜美腿视频一区二区在线看|人人操人人爽人人爱|婷婷五月天超碰|97色色欧美亚州A√|另类A√无码精品一级av|欧美特级日韩特级

0
  • 聊天消息
  • 系統(tǒng)消息
  • 評(píng)論與回復(fù)
登錄后你可以
  • 下載海量資料
  • 學(xué)習(xí)在線課程
  • 觀看技術(shù)視頻
  • 寫文章/發(fā)帖/加入社區(qū)
會(huì)員中心
創(chuàng)作中心

完善資料讓更多小伙伴認(rèn)識(shí)你,還能領(lǐng)取20積分哦,立即完善>

3天內(nèi)不再提示

玩嗨OpenHarmony:基于OpenHarmony的ArkUI翻頁(yè)時(shí)鐘

共熵服務(wù)中心 ? 來(lái)源:未知 ? 2022-12-05 20:15 ? 次閱讀
加入交流群
微信小助手二維碼

掃碼添加小助手

加入工程師交流群

原文引自:51CTO 開(kāi)源基礎(chǔ)軟件社區(qū) #夏日挑戰(zhàn)賽# OpenHarmony - 《ArkUI(TS)開(kāi)發(fā)翻頁(yè)時(shí)鐘

63824d32-7496-11ed-8abf-dac502259ad0.png

1. 項(xiàng)目背景

翻頁(yè)時(shí)鐘(Flip Clock)是一種有趣的機(jī)電數(shù)字計(jì)時(shí)設(shè)備,用電腦動(dòng)畫的方式實(shí)現(xiàn)翻頁(yè)時(shí)鐘,也是一種特別的復(fù)古UI交互體驗(yàn)。

本項(xiàng)目豈在通過(guò)OpenHarmony的ArkUI框架,用TS擴(kuò)展的聲明式開(kāi)發(fā)范式eTS,來(lái)實(shí)現(xiàn)翻頁(yè)時(shí)鐘的體驗(yàn)。

本項(xiàng)目的開(kāi)發(fā)環(huán)境如下:

  • 工具版本:DevEco Studio 3.0 Beta4

  • SDK版本:3.1.6.6(API Version 8 Release)

具體實(shí)現(xiàn)的效果是這樣的:

63a47eca-7496-11ed-8abf-dac502259ad0.gif

本項(xiàng)目的主要知識(shí)點(diǎn)如下:

  • UI狀態(tài):@Prop、@Link、@Watch

  • 形狀裁剪屬性:clip

  • 顯式動(dòng)畫:animateTo

2. eTS開(kāi)發(fā)范式基于eTS的聲明式開(kāi)發(fā)范式的方舟開(kāi)發(fā)框架是一套開(kāi)發(fā)極簡(jiǎn)、高性能、跨設(shè)備應(yīng)用的UI開(kāi)發(fā)框架,支持開(kāi)發(fā)者高效的構(gòu)建跨設(shè)備應(yīng)用UI界面。

使用基于eTS的聲明式開(kāi)發(fā)范式的方舟開(kāi)發(fā)框架,采用更接近自然語(yǔ)義的編程方式,讓開(kāi)發(fā)者可以直觀地描述UI界面,不必關(guān)心框架如何實(shí)現(xiàn)UI繪制和渲染,實(shí)現(xiàn)極簡(jiǎn)高效開(kāi)發(fā)。開(kāi)發(fā)框架不僅從組件、動(dòng)效和狀態(tài)管理三個(gè)維度來(lái)提供UI能力,還提供了系統(tǒng)能力接口,實(shí)現(xiàn)系統(tǒng)能力的極簡(jiǎn)調(diào)用。

63e5af44-7496-11ed-8abf-dac502259ad0.png

關(guān)于語(yǔ)法和概念詳細(xì)請(qǐng)直接看官網(wǎng)官方文檔地址:

https://docs.openharmony.cn/pages/v3.1/zh-cn/application-dev/ui/ui-ts-overview.md/

3. 實(shí)現(xiàn)思路時(shí)鐘翻頁(yè)效果,用到四個(gè)Text組件,使用堆疊容器Stack。底層:用到兩個(gè)裁剪過(guò)后的Text上下顯示;頂層:也是用兩個(gè)裁剪后的Text做動(dòng)畫效果,進(jìn)行X軸角度旋轉(zhuǎn)。

3.1 裁剪Text

裁剪前:

63fc5e4c-7496-11ed-8abf-dac502259ad0.png

裁剪后:

64170030-7496-11ed-8abf-dac502259ad0.png

使用形狀裁剪屬性clip

裁剪Text上半部:從坐標(biāo)(0,0)往下裁剪,clip(new Rect({ width: this.width, height: this.height / 2 }))

裁剪Text下半部:從坐標(biāo)(0,height / 2)往下裁剪,clip(new Path().commands(this.bottomPath))

@Entry
@Component
struct Test {
  private width = 90
  private height = 110
  private fontSize = 70
  private defaultBgColor = '#ffe6e6e6'
  private borderRadius = 10


  // 下半部裁剪路徑
  private bottomPath = `M0 ${vp2px(this.height / 2)}
  L${vp2px(this.width)} ${vp2px(this.height / 2)}
  L${vp2px(this.width)} ${vp2px(this.height)}
  L0 ${vp2px(this.height)} Z`


  build() {
    Row() {


      Text('24')
        .width(this.width)
        .height(this.height)
        .fontColor(Color.Black)
        .fontSize(this.fontSize)
        .textAlign(TextAlign.Center)
        .borderRadius(this.borderRadius)
        .backgroundColor(this.defaultBgColor)
        .clip(new Rect({ width: this.width, height: this.height / 2 }))


      Text('25')
        .margin({left:20})
        .width(this.width)
        .height(this.height)
        .fontColor(Color.Black)
        .fontSize(this.fontSize)
        .textAlign(TextAlign.Center)
        .borderRadius(this.borderRadius)
        .backgroundColor(this.defaultBgColor)
        .clip(new Path().commands(this.bottomPath))


    }.width('100%')
    .height('100%')
    .justifyContent(FlexAlign.Center)
  }
}
3.2放入堆疊容器

四個(gè)裁剪后的Text放入到堆疊容器中(代碼片段):

    Stack() {
      // 底層文字上部
      Text(this.newValue)
        ......
        .clip(new Rect({ width: this.width, height: this.height / 2 }))


      // 底層文字下部
      Text(this.oldValue)
        ......
        .clip(new Path().commands(this.bottomPath))


      // 頂層文字上部動(dòng)畫
      Text(this.oldValue)
        ......
        .clip(new Rect({ width: this.width, height: this.height / 2 }))
        .rotate({ x: 1, centerY: '50%', angle: this.angleTop })


      // 頂層文字下部動(dòng)畫
      Text(this.newValue)
        ......
        .margin({ top: 3 })
        .clip(new Path().commands(this.bottomPath))
        .rotate({ x: 1, centerY: '50%', angle: this.angleBottom })
    }
3.3使用顯式動(dòng)畫

先頂層上部的動(dòng)畫,上部旋轉(zhuǎn)角度從0到90停止,接下來(lái)執(zhí)行頂層下部的動(dòng)畫,下部旋轉(zhuǎn)角度從-90到0停止,停止完后重置初始狀態(tài),上部旋轉(zhuǎn)角度 = 0、下部旋轉(zhuǎn)角度 = -90(代碼片段)

  /**
   * 啟動(dòng)頂層文字上部動(dòng)畫
   */
  startTopAnimate() {
    animateTo({
      duration: 400,
      onFinish: () => {
        this.startBottomAnimate()
        this.animateBgColor = '#ffededed'
      }
    }, () => {
      this.angleTop = 90
      this.animateBgColor = '#ffc5c5c5'
    })
  }


  /**
   * 啟動(dòng)頂層文字下部動(dòng)畫
   */
  startBottomAnimate() {
    animateTo({
      duration: 400,
      onFinish: () => {
        this.angleTop = 0
        this.angleBottom = -90
        this.animateBgColor = this.defaultBgColor
        this.oldValue = this.newValue
      }
    }, () => {
      this.angleBottom = 0
      this.animateBgColor = this.defaultBgColor
    })
  }
3.4組件封裝

翻頁(yè)邏輯封裝成組件,提供給外部調(diào)用,根據(jù)外部傳入的雙向數(shù)據(jù)綁定:newValue,監(jiān)聽(tīng)數(shù)據(jù)變化,有變化則啟動(dòng)翻頁(yè)動(dòng)畫(代碼片段):

@Component
export struct FlipPage {
  // 頂層上部動(dòng)畫角度
  @State angleTop: number = 0
  // 頂層下部動(dòng)畫角度
  @State angleBottom: number = -90
  // 舊值
  @Prop oldValue: string
  // 新值,加入監(jiān)聽(tīng)
  @Link @Watch('valueChange') newValue: string


  /**
   * 監(jiān)聽(tīng)新值變化
   */
  valueChange() {
    if (this.oldValue === this.newValue) return
    this.startTopAnimate()
  }


  build() {
    Stack() {
      // 底層文字上部
      Text(this.newValue)
        ......
        .clip(new Rect({ width: this.width, height: this.height / 2 }))


      // 底層文字下部
      Text(this.oldValue)
        ......
        .clip(new Path().commands(this.bottomPath))


      // 頂層文字上部動(dòng)畫
      Text(this.oldValue)
        ......
        .clip(new Rect({ width: this.width, height: this.height / 2 }))
        .rotate({ x: 1, centerY: '50%', angle: this.angleTop })


      // 頂層文字下部動(dòng)畫
      Text(this.newValue)
        ......
        .margin({ top: 3 })
        .clip(new Path().commands(this.bottomPath))
        .rotate({ x: 1, centerY: '50%', angle: this.angleBottom })
    }
  }
  /**
  * 啟動(dòng)頂層文字上部動(dòng)畫
  */
  startTopAnimate() {
    ......
  }
3.5外部調(diào)用

界面加載成功后,開(kāi)啟循環(huán)定時(shí)器setInterval、間隔1秒更新時(shí)間。更改newValue的值,翻頁(yè)組件內(nèi)部進(jìn)行動(dòng)畫翻頁(yè)。

import { FlipPage } from '../componet/FlipPage'


@Entry
@Component
struct Index {
  // 小時(shí)-舊值
  @State oldHours: string = ''
  // 小時(shí)-新值
  @State newHours: string = ''
  // 分鐘-舊值
  @State oldMinutes: string = ''
  // 分鐘-新值
  @State newMinutes: string = ''
  // 秒數(shù)-舊值
  @State oldSeconds: string = ''
  // 秒數(shù)-新值
  @State newSeconds: string = ''


  @Builder Colon() {
    Column() {
      Circle().width(8).height(8).fill(Color.Black)
      Circle().width(8).height(8).fill(Color.Black).margin({ top: 10 })
    }.padding(10)
  }


  build() {
    Row() {
      // 翻頁(yè)組件-顯示小時(shí)
      FlipPage({ oldValue: this.oldHours, newValue: $newHours })
      // 冒號(hào)
      this.Colon()
      // 翻頁(yè)組件-顯示分鐘
      FlipPage({ oldValue: this.oldMinutes, newValue: $newMinutes })
      // 冒號(hào)
      this.Colon()
      // 翻頁(yè)組件-顯示秒數(shù)
      FlipPage({ oldValue: this.oldSeconds, newValue: $newSeconds })
    }
    .justifyContent(FlexAlign.Center)
    .width('100%')
    .height('100%')
    .onAppear(() => {
      // 開(kāi)啟定時(shí)器
      this.initDate()
      setInterval(() => {
        this.updateDate()
      }, 1000)
    })
  }


  /**
   * 初始化時(shí)間
   */
  initDate() {
    let date = new Date()
    // 設(shè)置小時(shí)
    this.oldHours = this.format(date.getHours())
    // 設(shè)置分鐘
    this.oldMinutes = this.format(date.getMinutes())
    // 設(shè)置秒數(shù)
    this.oldSeconds = this.format(date.getSeconds())
    // 設(shè)置新的秒數(shù)
    this.newSeconds = date.getSeconds() + 1 === 60 ? '00' : this.format(date.getSeconds() + 1)
  }


  /**
   * 更新時(shí)間
   */
  updateDate() {
    let date = new Date()
    console.log(`${date.getHours()}時(shí)${date.getMinutes()}${date.getSeconds()}秒`)
    // 當(dāng)新值改變,才有動(dòng)畫
    if (date.getSeconds() === 59) {
      this.newSeconds = '00'
      this.newMinutes = date.getMinutes() + 1 === 60 ? '00' : this.format(date.getMinutes() + 1)
      if (date.getMinutes() === 59) {
        this.newHours = date.getHours() + 1 === 24 ? '00' : this.format(date.getHours() + 1)
      }
    } else {
      this.newSeconds = this.format(date.getSeconds() + 1)
    }
  }


  /**
   * 不足十位前面補(bǔ)零
   */
  format(param) {
    let value = '' + param
    if (param < 10) {
      value = '0' + param
    }
    return value
  }
}
4.總結(jié)根據(jù)上面的實(shí)現(xiàn)思路和5個(gè)步驟流程,相信你也掌握了翻頁(yè)時(shí)鐘原理,拆分成一步一步還是很簡(jiǎn)單的,最主要還是對(duì)API的熟悉和聲明式語(yǔ)法的掌握。HarmonyOS的API是根據(jù)OpenHarmony去更新的,兩者區(qū)別語(yǔ)法都一樣,只是OpenHarmony的API比較新,功能比較完善和成熟的,所以本項(xiàng)目直接使用OpenHarmony SDK開(kāi)發(fā)。645d457c-7496-11ed-8abf-dac502259ad0.gif 本文完寫在最后我們最近正帶著大家玩嗨OpenHarmony。如果你有好玩的東東,歡迎投稿,讓我們一起嗨起來(lái)!有點(diǎn)子,有想法,有Demo,立刻聯(lián)系我們:合作郵箱:zzliang@atomsource.org
646c344c-7496-11ed-8abf-dac502259ad0.gif

6497e1fa-7496-11ed-8abf-dac502259ad0.png

64cde110-7496-11ed-8abf-dac502259ad0.png64de3970-7496-11ed-8abf-dac502259ad0.png6501ab08-7496-11ed-8abf-dac502259ad0.png

650cc628-7496-11ed-8abf-dac502259ad0.png

652c6190-7496-11ed-8abf-dac502259ad0.png

654d7074-7496-11ed-8abf-dac502259ad0.png

65721000-7496-11ed-8abf-dac502259ad0.png

65a1d57e-7496-11ed-8abf-dac502259ad0.png

65ca526a-7496-11ed-8abf-dac502259ad0.png

663c7336-7496-11ed-8abf-dac502259ad0.png

66afe622-7496-11ed-8abf-dac502259ad0.png

66c4d104-7496-11ed-8abf-dac502259ad0.png

66d3bbba-7496-11ed-8abf-dac502259ad0.png

66fd970a-7496-11ed-8abf-dac502259ad0.png

671687ba-7496-11ed-8abf-dac502259ad0.png

672c55fe-7496-11ed-8abf-dac502259ad0.png

674d70ea-7496-11ed-8abf-dac502259ad0.png

676f6902-7496-11ed-8abf-dac502259ad0.png

6781e2b2-7496-11ed-8abf-dac502259ad0.png


原文標(biāo)題:玩嗨OpenHarmony:基于OpenHarmony的ArkUI翻頁(yè)時(shí)鐘

文章出處:【微信公眾號(hào):開(kāi)源技術(shù)服務(wù)中心】歡迎添加關(guān)注!文章轉(zhuǎn)載請(qǐng)注明出處。


聲明:本文內(nèi)容及配圖由入駐作者撰寫或者入駐合作網(wǎng)站授權(quán)轉(zhuǎn)載。文章觀點(diǎn)僅代表作者本人,不代表電子發(fā)燒友網(wǎng)立場(chǎng)。文章及其配圖僅供工程師學(xué)習(xí)之用,如有內(nèi)容侵權(quán)或者其他違規(guī)問(wèn)題,請(qǐng)聯(lián)系本站處理。 舉報(bào)投訴
  • 開(kāi)源技術(shù)
    +關(guān)注

    關(guān)注

    0

    文章

    389

    瀏覽量

    8745
  • OpenHarmony
    +關(guān)注

    關(guān)注

    33

    文章

    3952

    瀏覽量

    21096

原文標(biāo)題:玩嗨OpenHarmony:基于OpenHarmony的ArkUI翻頁(yè)時(shí)鐘

文章出處:【微信號(hào):開(kāi)源技術(shù)服務(wù)中心,微信公眾號(hào):共熵服務(wù)中心】歡迎添加關(guān)注!文章轉(zhuǎn)載請(qǐng)注明出處。

收藏 人收藏
加入交流群
微信小助手二維碼

掃碼添加小助手

加入工程師交流群

    評(píng)論

    相關(guān)推薦
    熱點(diǎn)推薦

    【原創(chuàng)】OpenHarmony系統(tǒng)投屏工具軟件 - OpenHarmony_OHScrcpy使用推薦

    OpenHarmony_OHScrcpy - OpenHarmony投屏工具軟件 OpenHarmony_OHScrcpy是一款為OpenHarmony系統(tǒng)設(shè)計(jì)的投屏工具軟件,功能類似
    發(fā)表于 01-22 18:34

    #OpenHarmony HDC調(diào)試

    OpenHarmony
    視美泰
    發(fā)布于 :2026年01月16日 09:46:28

    #OpenHarmony 開(kāi)發(fā)環(huán)境準(zhǔn)備

    OpenHarmony
    視美泰
    發(fā)布于 :2026年01月16日 09:44:31

    #OpenHarmony 鏡像燒錄

    OpenHarmony
    視美泰
    發(fā)布于 :2026年01月16日 09:42:36

    #OpenHarmony 系統(tǒng)概述

    OpenHarmony
    視美泰
    發(fā)布于 :2026年01月16日 09:39:20

    #OpenHarmony Hello World應(yīng)用以及部署

    OpenHarmony
    視美泰
    發(fā)布于 :2026年01月16日 09:32:52

    OpenHarmony系統(tǒng)】本期視頻將為您介紹怎樣將鏡像燒錄到板卡中。

    OpenHarmony
    視美泰
    發(fā)布于 :2026年01月15日 14:29:55

    OpenHarmony年度課題管理辦法

    OpenHarmony年度課題管理辦法V1.0 第一章 總則 第一條 宗旨 圍繞終端操作系統(tǒng)所面臨的技術(shù)挑戰(zhàn),OpenHarmony項(xiàng)目群技術(shù)指導(dǎo)委員會(huì)(TSC)聯(lián)合產(chǎn)學(xué)研各界,以
    的頭像 發(fā)表于 11-12 16:55 ?653次閱讀

    2025 OpenHarmony TSC年中技術(shù)與生態(tài)研討會(huì)圓滿舉辦

    2025 OpenHarmony TSC 年中技術(shù)與生態(tài)研討會(huì) 圓滿舉辦 8月1日,2025 OpenHarmony TSC年中技術(shù)與生態(tài)研討會(huì)于中國(guó)上海順利召開(kāi)。 本次會(huì)議由OpenHarmony
    的頭像 發(fā)表于 08-07 12:24 ?2035次閱讀
    2025 <b class='flag-5'>OpenHarmony</b> TSC年中技術(shù)與生態(tài)研討會(huì)圓滿舉辦

    ArkUI-X通過(guò)Stage模型開(kāi)發(fā)Android端應(yīng)用指南(一)

    簡(jiǎn)介 本文介紹將ArkUI框架擴(kuò)展到Android平臺(tái)所需要的必要的類及其使用說(shuō)明,開(kāi)發(fā)者基于OpenHarmony,可復(fù)用大部分的應(yīng)用代碼(生命周期等)并可以部署到Android平臺(tái),降低跨平臺(tái)
    發(fā)表于 06-24 22:16

    ArkUI-X平臺(tái)橋接Bridge說(shuō)明

    。 使用場(chǎng)景及能力 使用場(chǎng)景 平臺(tái)橋接主要用于這樣的場(chǎng)景:應(yīng)用需要復(fù)用平臺(tái)的代碼,而在OpenHarmony中沒(méi)有對(duì)應(yīng)的跨平臺(tái)API(不包括UI相關(guān))實(shí)現(xiàn)。 具體可用于以下場(chǎng)景: 1、ArkUI與平臺(tái)雙向
    發(fā)表于 06-19 23:12

    ArkUI-X應(yīng)用工程結(jié)構(gòu)說(shuō)明

    簡(jiǎn)介 本文檔配套ArkUI-X,將OpenHarmony ArkUI開(kāi)發(fā)框架擴(kuò)展到不同的OS平臺(tái),比如Android和iOS平臺(tái),讓開(kāi)發(fā)者基于ArkUI,可復(fù)用大部分的應(yīng)用代碼(UI以
    發(fā)表于 06-19 23:11

    請(qǐng)問(wèn)下,openharmony支持哪一款龍芯的開(kāi)發(fā)板?有沒(méi)有開(kāi)源的龍芯的openharmony源碼?

    想買個(gè)2k0300的開(kāi)發(fā)板學(xué)習(xí)龍芯和openharmony,愣是沒(méi)有看到提供openharmony源碼的,也沒(méi)與看到開(kāi)源的代碼。gitee上,openharmony的龍芯sig倉(cāng)庫(kù)也是關(guān)閉的,有沒(méi)有人知道現(xiàn)在是什么情況?
    發(fā)表于 04-26 13:06