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

HarmonyOS自定義UI之水波紋效果

開發 前端 OpenHarmony
任何東西都可以在生活中找到案例,我們要做水波紋效果,就想象一下,每個人應該都有把石頭扔進進水里的經歷,首先水波是從中心點的小圓慢慢放大為大圓,然后慢慢消失。

[[423871]]

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

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

https://harmonyos.51cto.com

概述

​ 當點擊一個view時,然后一個水波紋就會從點擊處擴散開來,模擬水波背景動效實現。

實現思路

​ 任何東西都可以在生活中找到案例,我們要做水波紋效果,就想象一下,每個人應該都有把石頭扔進進水里的經歷,首先水波是從中心點的小圓慢慢放大為大圓,然后慢慢消失,我們模擬的時候只需要畫圓,通過Canvas. drawCircle(float x, float y, float radius, Paint paint); 方法來實現,通過一點點放大半徑在根據變化速率設置透明度的方式實現。

效果如下:

HarmonyOS  自定義UI之水波紋效果-鴻蒙HarmonyOS技術社區

實現過程:

通過計算view的最大寬度 / 2為布局的最大半徑,最小半徑就是0然后給它一個插值動畫讓它動起來,每次點擊就讓它繪制圓,圓的半徑為 (this.radiusMax * 插值動畫比例 ),插值動畫比例的變化曲率為 0 - 1 根據比例計算出半徑從小到大的大小,代碼如下:

  1. //初始化畫筆 
  2. private void inint() { 
  3.      this.animationRunning = false
  4.      this.paint = new Paint(); 
  5.      this.paint.setAntiAlias(true); 
  6.      this.paint.setStyle(FILLANDSTROKE_STYLE); 
  7.      this.paint.setColor(new Color(Color.getIntColor("#0000FF")));//設置水波紋的顏色 
  8.  } 
  9.  
  10.  @Override 
  11.  public void onDraw(Component component, Canvas canvas) { 
  12.      float touchX = (float) this.getWidth() / 2; 
  13.      float touchY = (float) this.getHeight() / 2; 
  14.       this.paint.setAlpha(1 - 1* this.ripplePose);//透明都也是從0到1  因為ripplePose 是從0-1變換 
  15.      this.radiusMax = component.getWidth()/2; 
  16.         float radiusMax2 = component.getWidth()/4; 
  17.      //根據比例設置半徑大小 
  18.       canvas.drawCircle(touchX, touchY,  this.radiusMax * this.ripplePose +radiusMax2, this.paint); 
  19.  } 

this.ripplePose的變化曲率為 0 - 1 根據比例計算出半徑從小到大的大小,來繪制圓,這里我為什么加 radiusMax2是因為給他一個初始半徑,讓他不要從圓心開始,這樣看起來就比較舒服了, 在點擊的時候觸發動畫,代碼如下:

  1. @Override 
  2. public boolean onTouchEvent(Component component, TouchEvent touchEvent) { 
  3.  if (touchEvent.getPointerCount() == 1 && touchEvent.getAction() == PRIMARY_POINT_DOWN) { // 一個手指按下 
  4.      this.downX = this.getTouchX(touchEvent, 0); 
  5.      this.createAnimation(this.getTouchX(touchEvent, 0), this.getTouchY(touchEvent, 0)); 
  6.  } else if (touchEvent.getPointerCount() == 1 && touchEvent.getAction() == POINT_MOVE) { // 一個手指移動 
  7.  
  8.  
  9.  } else if (touchEvent.getPointerCount() == 1 && touchEvent.getAction() == PRIMARY_POINT_UP) { //一個手指抬起 
  10.      this.invalidate(); 
  11.  } 
  12.  return false

效果如下:

但是當我把水波紋應用到 PageSlider 中帶有ListContainer 的時候,滑動的時候也會觸發水波紋,所以需要解決事件沖突。

事件沖突解決

在添加了長按陰影效果后,在滑動PageSlider 頁面的時候listContainer的子item也會觸發點擊事件,導致各種事件沖突,解決方法就是 在Touch事件中計算定義出各種事件,各個擊破。

1.點擊事件:抬起時間-按下時間 沒有超過200ms

2.長按事件:按下超過200ms

3.長按滑動事件:移動監聽里面計算 滑動距離超過20,時間超過200 沒有抬起

4.短按滑動事件: 時間少于200ms 距離超過 200px 沒有抬起

5.然后處理各種事件

另外發現,手機點擊的時候移動事件會觸發,而模擬器不會觸發,這個也需要注意!

這樣就完成了整個繪制過程,代碼如下:

  1. /** 
  2.   * 觸摸監聽 
  3.   * 
  4.   * @param component view 
  5.   * @param touchEvent touchEvent 
  6.   * @return 是否消費 
  7.   */ 
  8. public boolean onTouchEvent(Component component, TouchEvent touchEvent) { 
  9.  if (touchEvent.getPointerCount() == NUM1 && touchEvent.getAction() == NUM1) { 
  10.      this.downX = this.getTouchX(touchEvent, 0); 
  11.      this.downY = this.getTouchY(touchEvent, 0); 
  12.      this.downTime = System.currentTimeMillis(); 
  13.      this.isDownPose = true
  14.      this.isSlide = false
  15.      isYinYing = false
  16.  } else if (touchEvent.getPointerCount() == NUM1 && touchEvent.getAction() == NUM3) { 
  17.      float upx = this.getTouchX(touchEvent, 0); 
  18.      long theTime = System.currentTimeMillis(); 
  19.      int xjuli = (int) Math.abs(upx - downX); 
  20.      int timejuli = (int) Math.abs(theTime - this.downTime); 
  21.      if (isDownPose && timejuli > NUM200 && xjuli < NUM2 && !isSlide) { 
  22.          this.createAnimation(this.getTouchX(touchEvent, 0), this.getTouchY(touchEvent, 0)); 
  23.          myevenHandler.sendEvent(1, NUM250); 
  24.          this.isSlide = true
  25.          return true
  26.      } 
  27.      return true
  28.  } else if (touchEvent.getPointerCount() == NUM1 && touchEvent.getAction() == NUM2) { 
  29.      float upx = this.getTouchX(touchEvent, 0); 
  30.      long upTimes = System.currentTimeMillis(); 
  31.      isDownPose = false
  32.      isYinYing = false
  33.      int xjuli = (int) Math.abs(upx - downX); 
  34.      if (!this.isDownPose && upTimes - this.downTime < NUM200 && xjuli < NUM5) { 
  35.          this.isDownPose = false
  36.          this.createAnimation(this.getTouchX(touchEvent, 0), this.getTouchY(touchEvent, 0)); 
  37.      } else if (!this.isDownPose && System.currentTimeMillis() - this.downTime > NUM250) { 
  38.          this.invalidate(); 
  39.      } else { 
  40.          this.isDownPose = false
  41.      } 
  42.      this.isDownPose = false
  43.  } 
  44.  return true
  45.   
  46. /** 
  47.   * 長按監聽 
  48.   * 
  49.   * @param component view 
  50.   */ 
  51. @Override 
  52.  public void onLongClicked(Component component) { 
  53.      this.createAnimation(this.downX, this.downY); 
  54.      myevenHandler.sendEvent(1, NUM250); 
  55.      this.isSlide = true
  56.  } 
  57.   
  58.   
  59.    /** 
  60.   * 繪制 
  61.   * 
  62.   * @param var1 Component 
  63.   * @param var2 Canvas 
  64.   */ 
  65.   public void onDraw(Component var1, Canvas var2) { 
  66.      if (isYinYing) { 
  67.          this.paint.setAlpha(NUM03F); 
  68.          var2.drawCircle(this.touchX, this.touchY, this.radiusMax * this.ripplePose - 0 / NUM2F, this.paint); 
  69.      } else { 
  70.          if (this.getWidth() != 0 && this.getHeight() != 0) { 
  71.              this.radiusMax = (float) Math.sqrt(this.getWidth() 
  72.                      * this.getWidth() + this.getHeight() * this.getHeight()); 
  73.              if (this.rippleType != NUM2) { 
  74.                  this.radiusMax /= NUM2F; 
  75.              } 
  76.              this.radiusMax -= (float) this.ripplePadding; 
  77.              if (this.isCentered || this.rippleType == 1) { 
  78.                  this.touchX = (float) this.getWidth() / NUM2F; 
  79.                  this.touchY = (float) this.getHeight() / NUM2F; 
  80.              } 
  81.              this.paint.setAlpha(this.rippleAlpha - this.rippleAlpha * this.ripplePose); 
  82.              float var3 = 0.0F; 
  83.              if (this.rippleType == NUM1 && this.ripplePose > NUM04) { 
  84.                  this.paint.setStyle(Paint.Style.STROKE_STYLE); 
  85.                  var3 = this.radiusMax * this.ripplePose - this.radiusMax * (this.ripplePose - NUM04) / NUM06; 
  86.                  this.paint.setStrokeWidth(this.radiusMax * this.ripplePose 
  87.                          - this.radiusMax * (this.ripplePose - NUM04) / NUM06); 
  88.              } else { 
  89.                  this.paint.setStyle(Paint.Style.FILL_STYLE); 
  90.              } 
  91.              var2.drawCircle(this.touchX, this.touchY, this.radiusMax * this.ripplePose - var3 / NUM2F, this.paint); 
  92.          } 
  93.      } 
  94.  } 

效果如下:

完整項目代碼

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

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

https://harmonyos.51cto.com

 

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

2023-10-30 08:35:50

水波紋效果vue

2023-12-20 17:28:48

水波紋ArkUI動畫開發

2024-05-31 08:43:31

2021-09-02 10:00:42

鴻蒙HarmonyOS應用

2021-09-15 10:19:15

鴻蒙HarmonyOS應用

2009-06-25 14:53:35

自定義UI組件JSF框架

2011-03-02 10:24:23

DashboardAndroid用戶界面設計模板

2021-01-21 07:35:40

JenkinsUICSS

2022-03-21 15:19:27

鴻蒙UI組件ets自定義

2022-05-18 16:17:31

設備開發鴻蒙

2021-03-09 15:23:45

鴻蒙HarmonyOS應用開發

2013-03-28 10:58:30

自定義Android界android

2013-01-06 10:43:54

Android開發View特效

2024-05-30 08:23:37

ViewPager滑動效果接口

2022-04-24 15:17:56

鴻蒙操作系統

2015-02-12 15:33:43

微信SDK

2022-04-07 14:17:15

Harmonytoast組件鴻蒙

2010-04-28 11:14:20

Windows 7桌面

2015-02-12 15:38:26

微信SDK

2023-02-20 15:20:43

啟動頁組件鴻蒙
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 天天影视色综合 | 日韩久久精品 | 亚洲高清久久 | 中文在线一区 | 成年网站在线观看 | 伊伊综合网 | 国产一级电影在线观看 | 91精品国产色综合久久不卡蜜臀 | 久久久久成人精品免费播放动漫 | 新91视频网 | 日韩在线一区二区 | 91在线精品秘密一区二区 | 国产一区二区三区免费观看视频 | aaaaaaa片毛片免费观看 | 美女久久视频 | 欧美一级做a爰片免费视频 国产美女特级嫩嫩嫩bbb片 | 亚州精品天堂中文字幕 | 国产精品一区二区在线观看 | 国产精品福利久久久 | 国内精品视频免费观看 | 一二区成人影院电影网 | 国产成人a亚洲精品 | 亚洲综合精品 | 国产乱码精品一区二区三区中文 | 日韩手机在线视频 | 午夜理伦三级理论三级在线观看 | 亚洲国产精品久久人人爱 | 欧美精品综合 | 久久久久久免费免费 | 国产日韩精品视频 | 天天曰天天曰 | 国产第一页在线观看 | 欧美日韩国产在线观看 | 国产精品日韩在线 | 日韩毛片在线免费观看 | 国产精品久久久久久久久免费桃花 | 中文在线一区二区 | 亚洲国产成人在线观看 | 午夜私人影院在线观看 | 亚卅毛片 | 日韩中文字幕网 |