成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

HarmonyOS自定義控件之JS進度條

開發 前端 OpenHarmony
在我們日常開發的里面,很多場景經常會用到進度條,而系統提供的進度條樣式又無法滿足我們的使用,這時候我們就需要自定義一個進度條。

[[422087]]

想了解更多內容,請訪問:

51CTO和華為官方合作共建的鴻蒙技術社區

https://harmonyos.51cto.com

前言

在我們日常開發的里面,很多場景經常會用到進度條,而系統提供的進度條樣式又無法滿足我們的使用,這時候我們就需要自定義一個進度條,自定義JS進度條主要涉及以下知識點:

  • 如何自定義組件及引用
  • 如何自定義繪制圖形(draw)
  • 如何創建并執行動畫(animation)
  • 如何設置自定義組件的參數(setter)
  • 如何監聽自定義組件的參數(getter)

效果演示

代碼實現

如何自定義組件及引用

1.Js自定義組件,只需要新創建一個包,直接在里面編寫界面,樣式,邏輯代碼即可。如果需要使用該組件,將其完整拷貝到自己的項目結構下進行引用即可。我們自定義一個Progress進度條控件,項目結構如下圖:

【中軟國際】HarmonyOS 自定義控件之JS進度條-鴻蒙HarmonyOS技術社區

2.使用的時候,我們需要給自定義組件進行標簽聲明,然后就可以使用該標簽了:

<index.hml>

  1. // src表示我們引用的自定義組件的文件,name表示我們給該自定義組件聲明一個標簽名 
  2. <element src="../progress/progress.hml" name="progress-bar"></element> 
  3. <div class="container"
  4.     // 聲明之后,就可以使用這個自定義的標簽了 
  5.     <progress-bar></progress-bar> 
  6.  
  7.     ... 
  8. </div> 

如何自定義繪制圖形

說到自定義繪制,自然離不開canvas,首先我們給自定義組件增加一個<canvas>標簽,并在JS文件中描述繪制:

<progress.hml>

  1. <stack class="frame-layout"
  2.     <canvas id="progress-bar" class="progress-bar" ontouchmove="onTouchEvent"></canvas> 
  3.     <text if="{{ display }}" class="progress-bar">{{ progressText }}</text> 
  4. </stack> 

在JS中定義一個draw方法,并且傳入一個CanvasRenderingContext2D參數,這個參數我們可以理解為canvas + paint,所有繪制都是通過它進行調用:

<progress.js>

  1. draw(ctx) { 
  2.      this.display = true 
  3.      ctx.lineWidth = this.circleWidth 
  4.      ctx.lineCap = 'round' 
  5.      // ctx可以理解為canvas + paint 
  6.      ctx.clearRect(0, 0, this.width, this.height) // 會閃屏,系統渲染問題 
  7.      ctx.save() // save1 
  8.      ctx.translate(this.width / 2, this.height / 2) 
  9.      // draw background 
  10.      ctx.beginPath() 
  11.      ctx.strokeStyle = this.backgroundColor 
  12.      ctx.arc(0, 0, 100, 0, 2 * Math.PI) // r = 100, 參數是弧度,角度 = 弧度 / PI * 180 
  13.      ctx.stroke() // 繪制 
  14.      ctx.closePath() 
  15.      // draw progress 
  16.      ctx.save() 
  17.      ctx.rotate(-90 / 180 * Math.PI) 
  18.      ctx.beginPath() 
  19.      ctx.strokeStyle = this.progressColor 
  20.      ctx.arc(0, 0, 100, 0, this.angle / 180 * Math.PI) // r = 100, 參數是弧度,角度 = 弧度 / PI * 180 
  21.      ctx.stroke() // 繪制 
  22.      ctx.closePath() 
  23.      ctx.restore() 
  24.      ctx.restore() // save1 
  25.      this.notifyChanged() 
  26.  } 

這部分邏輯并不復雜,都有注釋,就是繪制一個圓環背景,然后根據進度參數在圓環上繪制一個圓弧進度,相信做過自定義控件的同學都能夠非常熟悉。

如何創建并執行動畫

1.首先我們需要在init的時候創建一個動畫對象,并且設置好初始的動畫參數:

<progress.js>

  1. onInit() { 
  2.       // 動畫參數(具體參數類型和參數說明參考官方文檔) 
  3.       var options = { 
  4.           duration: this.animDuration, // 動畫時長 
  5.           direction: 'normal', // 播放模式 
  6.           easing: 'linear', // 差值器 
  7.           fill: 'forwards', // 動畫結束后狀態 
  8.           iterations: 1, // 執行次數 
  9.           begin: 0, // 起始值 
  10.           end: 360.0 // 終止值 
  11.       }; 
  12.       var _this = this 
  13.       this.animator = Animator.createAnimator(options) 
  14.       this.animator.onframe = function (value) { 
  15.           // 動畫每一幀回調,類似我們熟悉的onAnimateUpdate回調 
  16.           _this.angle = value 
  17.           // 刷新繪制 
  18.           _this.draw(_this.ctx) 
  19.       } 
  20.       ... 
  21.   },  

2.接著我們需要在特定的時候開啟動畫,例如我們在接收到外部傳進來的進度參數后,我們需要更新動畫的起始值和終止值,并且開始執行動畫:

<progress.js>

  1. onProgressChanged(oldV, newV) {  
  2.      console.log("onProgressChanged from:" + oldV + " to: " + newV) 
  3.      this.initWidget() 
  4.      // 進度值范圍限定為[0, 1] 
  5.      if (oldV >= 1) { 
  6.          oldV = 1 
  7.      } 
  8.      if (newV >= 1) { 
  9.          newV = 1 
  10.      } 
  11.      // 更新動畫的起始和終止參數 
  12.      var options = { 
  13.          duration: this.animDuration, 
  14.          direction: 'alternate-reverse'
  15.          easing: 'linear'
  16.          fill: 'forwards'
  17.          iterations: 1, 
  18.          begin: oldV * 360, 
  19.          end: newV * 360 
  20.      }; 
  21.      this.animator.update(options) 
  22.      // 開始執行動畫 
  23.      this.animator.play() 
  24.  }, 

如何設置自定義組件的參數

1.我們自定義組件,并不能像之前一樣簡單的暴露個公開方法給外部調用。由于其數據驅動的設計,我們可以定義一些自定義屬性參數,當外部修改參數時我們就可以接收到信息進行主動動作(setter):

<progress.js>

  1. props: [ 
  2.       'progress', // 進度 
  3.       'backgroundColor', // 圓環背景顏色 
  4.       'progressColor' // 進度前景顏色 
  5.   ], 
  6.   ... 

2.監聽這些對外暴露的屬性值變化(listener):

<progress.js>

  1. onInit() { 
  2.      ... 
  3.      // 監聽自定義屬性值變化 
  4.      this.$watch('progress''onProgressChanged'
  5.      this.$watch('backgroundColor''onBackgroundChanged'
  6.      this.$watch('progressColor''onForegroundChanged'
  7.      ... 
  8.  },    
  9.  // backgroundColor變化時會觸發該回調 
  10.  onBackgroundChanged(oldV, newV) { 
  11.      this.backgroundColor = newV 
  12.  }, 
  13.  // progressColor變化時會觸發該回調 
  14.  onForegroundChanged(oldV, newV) { 
  15.      this.progressColor = newV 
  16.  }, 
  17.  // progress變化時會觸發該回調 
  18.  onProgressChanged(oldV, newV) { 
  19.      console.log("onProgressChanged from:" + oldV + " to: " + newV) 
  20.      this.initWidget() 
  21.      if (oldV >= 1) { 
  22.          oldV = 1 
  23.      } 
  24.      if (newV >= 1) { 
  25.          newV = 1 
  26.      } 
  27.      var options = { 
  28.          duration: this.animDuration, 
  29.          direction: 'alternate-reverse'
  30.          easing: 'linear'
  31.          fill: 'forwards'
  32.          iterations: 1, 
  33.          begin: oldV * 360, 
  34.          end: newV * 360 
  35.      }; 
  36.      this.animator.update(options) 
  37.      this.animator.play() 
  38.  },  

3..外部設置參數,當外部改變這些參數時,我們自定義組件內部的回調方法就會觸發,并執行刷新邏輯:

<index.hml>

  1. <element src="../progress/progress.hml" name="progress-bar"></element> 
  2. <div class="container"
  3.     <progress-bar background-color="#c2f135" 
  4.                   progress-color="#6bfc33" 
  5.                   progress="{{ progress }}"
  6.     </progress-bar> 
  7.     ... 
  8. </div> 

如何監聽自定義組件的參數

上面我們說到了外部如何改變自定義組件內部的屬性,本質上就是一個典型觀察者模式。同理,外部調用者需要監聽我們自定義組件的參數變化,也是通過這種方式:

1.首先我們在自定義組件中需要定義一個被觀察者對象(key),并且在該對象值變化時對外發送消息:

<progress.js>

  1. notifyChanged() { 
  2.        // currentAngle, currentProgress就是被觀察者對象,key-value結構,value就是我們對外發送的值 
  3.        // 注意:駝峰命名 
  4.        this.$emit("currentAngle", this.angle) 
  5.        this.$emit("currentProgress", Math.ceil(this.angle / 3.6) / 100) 
  6.        this.progressText = Math.ceil(this.angle / 3.6) + "%" 
  7.    }, 

2.外部使用者需要注冊監聽回調方法,對被觀察者對象(key)進行監聽:

 <index.hml>

  1. <element src="../progress/progress.hml" name="progress-bar"></element> 
  2. <div class="container"
  3.     // 通過@current-angle和@current-progress進行該參數的監聽,注意參數前加"@",并且參數根據駝峰命名方式拆分單詞,每個詞語用"-"隔開 
  4.     <progress-bar background-color="#c2f135" 
  5.                   progress-color="#6bfc33" 
  6.                   progress="{{ progress }}" 
  7.                   @current-angle="onAngleChanged" 
  8.                   @current-progress="onProgressChanged"
  9.     </progress-bar> 
  10.     ...  
  11. </div>

<index.js>

  1. // 當自定義組件內部的 currentAngle, currentProgress變化時,會觸發下面的回調方法通知外部使用者 
  2.    onAngleChanged(angle) { 
  3.        console.log("onAngleChanged: " + angle.detail) 
  4.    }, 
  5.    onProgressChanged(progress) { 
  6.        console.log("onProgressChanged: " + progress.detail) 
  7.    } 

其他關鍵點

1.<canvas>標簽的繪制內容默認是不顯示的,我們可以在初始化的時候監聽首幀回調,主動進行刷新一次:

<progress.js>

  1. onInit() { 
  2.     ... 
  3.     // 監聽首幀,觸發首次繪制,類似attachToWindow的觸發時機 
  4.     requestAnimationFrame(function () { 
  5.         _this.initWidget() 
  6.         _this.draw(_this.ctx) 
  7.     }) 
  8. }, 

2.自定義組件如何獲取寬高信息,在API6+系統已經提供相關的方法可以進行獲取,類似onSizeChanged中讀取寬高信息:

<progress.js>

  1. initWidget() { 
  2.      console.log("init widget"
  3.      if (this.ctx === null) { 
  4.          // 獲取標簽元素 
  5.          let widget = this.$element('progress-bar'); 
  6.          this.ctx = widget.getContext('2d', { 
  7.              antialias: true 
  8.          }) 
  9.          // 獲取寬高,并計算出繪制圓環的寬高,中心點,半徑信息 
  10.          this.width = widget.getBoundingClientRect().width 
  11.          this.height = widget.getBoundingClientRect().height 
  12.          this.centerX = widget.getBoundingClientRect().left + this.width / 2 
  13.          this.centerY = widget.getBoundingClientRect().top + this.height / 2 
  14.          console.log("canvas size = " + this.width + ", " + this.height) 
  15.          console.log("canvas center = " + this.centerX + ", " + this.centerY) 
  16.      } 
  17.  }, 

3.canvas畫布和我們通常理解的是不同的,它是存在繪制緩存的,所以每一幀刷新時,我們需要在繪制前先清空之前的繪制內容。目前鴻蒙清空畫布時會概率出現閃屏問題。

以上就是實現一個自定義JS進度條的核心代碼了,源代碼:JsProgress

想了解更多內容,請訪問:

51CTO和華為官方合作共建的鴻蒙技術社區

https://harmonyos.51cto.com

 

責任編輯:jianghua 來源: 鴻蒙社區
相關推薦

2022-09-09 14:47:50

CircleArkUI

2021-12-30 16:10:52

鴻蒙HarmonyOS應用

2017-03-14 15:09:18

AndroidView圓形進度條

2021-12-07 18:23:50

自定義進度條分段式

2021-08-16 14:45:38

鴻蒙HarmonyOS應用

2021-08-25 10:14:51

鴻蒙HarmonyOS應用

2009-12-25 17:58:12

WPF進度條

2015-08-03 11:39:20

擬物化進度條

2021-11-01 10:21:36

鴻蒙HarmonyOS應用

2015-07-31 11:19:43

數字進度條源碼

2015-02-11 17:49:35

Android源碼自定義控件

2021-09-02 10:00:42

鴻蒙HarmonyOS應用

2022-02-17 14:51:39

HarmonyOSJSPAI開發canvas畫布

2021-08-11 14:29:20

鴻蒙HarmonyOS應用

2022-05-20 14:34:20

list組件鴻蒙操作系統

2009-06-08 20:13:36

Eclipse自定義控

2013-04-19 10:14:24

2009-08-06 09:18:01

ASP.NET自定義控ASP.NET控件開發

2022-04-11 11:07:37

HarmonyUI小型系統textarea

2021-11-24 10:02:53

鴻蒙HarmonyOS應用
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 黑人精品 | 在线看片网站 | 日韩精品一区二区三区四区视频 | 九九热九九 | 欧美区在线 | 亚洲成人中文字幕 | 精品日韩 | 伊人久久综合 | 久久久久久免费观看 | 久久只有精品 | 亚洲欧美在线免费观看 | 欧美一区二区在线观看 | 国产精品一区二区三区四区 | 天堂在线www | 国产精品久久久久久久午夜 | 台湾a级理论片在线观看 | 久久久成人一区二区免费影院 | 欧美在线a | 亚洲激情第一页 | 欧美日韩精品久久久免费观看 | 成人免费福利视频 | 国产一级免费在线观看 | 精品福利av导航 | 中文字幕第十页 | 成人精品视频 | 涩涩视频在线观看免费 | 欧美中文字幕一区二区三区亚洲 | 国产色片 | 欧美在线成人影院 | 色狠狠一区 | 天天艹 | 国产高清性xxxxxxxx | 伊人超碰在线 | 中文字幕在线观看 | 超碰导航| 国产农村妇女精品一区 | 欧美美乳| 日韩中文字幕在线观看视频 | 日操操夜操操 | 天天操操 | 中文字幕亚洲一区 |