HarmonyOS 自定义组件之上拉抽屉

开发 OpenHarmony
这里给大家提供了一个BottomSheet上拉抽屉的组件,同时通过这个组件示例讲解一下HarmonyOS中的几个自定义控件用到的知识,分享一下自己自定义组件的思路。

[[441431]]

想了解更多内容,请访问:

51CTO和华为官方合作共建的鸿蒙技术社区

https://harmonyos.51cto.com

简介

HarmonyOS 开发自定义组件目前还不是很丰富,在开发过程中常常会有一些特殊效果的组件,这就需要我们额外花一些时间实现,这里给大家提供了一个BottomSheet上拉抽屉的组件,同时通过这个组件示例讲解一下HarmonyOS中的几个自定义控件用到的知识,分享一下自己自定义组件的思路。

效果演示

#星光计划2.0# HarmonyOS 自定义组件之上拉抽屉-鸿蒙HarmonyOS技术社区

实现思路

1.布局设计

选择的是相对布局,蒙层区来改变内容区随着抽屉的位置调节透明度。

图1:

#星光计划2.0# HarmonyOS 自定义组件之上拉抽屉-鸿蒙HarmonyOS技术社区
[[441433]]

 

2.手势判断

先得出Component在屏幕的上下左右的坐标,然后手指的坐标是否在Component内。

  1. /** 
  2.  * (x,y)是否在view的区域内 
  3.  * 
  4.  * @param component 
  5.  * @param x 
  6.  * @param y 
  7.  * @return 
  8.  */ 
  9. private boolean isTouchPointInComponent(Component component, float x, float y) { 
  10.     int[] locationOnScreen = component.getLocationOnScreen(); 
  11.     int left = locationOnScreen[0]; 
  12.     int top = locationOnScreen[1]; 
  13.     int right = left + component.getEstimatedWidth(); 
  14.     int bottom = top + component.getEstimatedHeight(); 
  15.     boolean inY = y >= top && y <= bottom; 
  16.     boolean inX = x >= left && x <= right
  17.     return inY && inX; 

3.抽屉偏移

  • 这里采用的是整个component对Touch事件的监听;
  • 手指按下的判断是否在抽屉上,然后记录当前触摸y坐标;
  • 移动是算出偏移量offY;
  1. setTouchEventListener(new TouchEventListener() { 
  2.     @Override 
  3.     public boolean onTouchEvent(Component component, TouchEvent touchEvent) { 
  4.         HiLog.info(logLabel, "onTouchEvent action:" + touchEvent.getAction()); 
  5.         switch (touchEvent.getAction()) { 
  6.             case TouchEvent.PRIMARY_POINT_DOWN: 
  7.                 marginBottom = directionalLayout.getMarginBottom(); 
  8.                 MmiPoint position = touchEvent.getPointerScreenPosition(0); 
  9.                 if (isTouchPointInComponent(directionalLayout, position.getX(), position.getY())) { 
  10.                     dragStartPointY = touchEvent.getPointerPosition(0).getY(); 
  11.                     return true
  12.                 } 
  13.                 break; 
  14.             case TouchEvent.PRIMARY_POINT_UP: 
  15.                 onTouchUp(); 
  16.                 break; 
  17.             case TouchEvent.POINT_MOVE: 
  18.                 float y = touchEvent.getPointerPosition(0).getY(); 
  19.                 float offY = dragStartPointY - y; 
  20.                 setDrawerMarginBottom((int) offY); 
  21.                 break; 
  22.         } 
  23.         return false
  24.     } 
  25. }); 

根据偏移量改变抽屉的位置;

  1. private void setDrawerMarginBottom(int offY) { 
  2.     int bottom = marginBottom + offY; 
  3.     if (bottom > 0) { 
  4.         bottom = 0; 
  5.         listContainer.setEnabled(true); 
  6.     } 
  7.  
  8.     if (bottom < -H / 2) { 
  9.         bottom = -H / 2; 
  10.     } 
  11.     HiLog.info(logLabel, "setDrawerMarginBottom bottom:" + bottom); 
  12.  
  13.     float alpha = (0.5f - Math.abs((float) bottom / (float) H)) * 0.5f; 
  14.     HiLog.info(logLabel, "setDrawerMarginBottom alpha:" + alpha); 
  15.     bgComponent.setAlpha(alpha); 
  16.     directionalLayout.setMarginBottom(bottom); 

4.事件冲突解决

首先发现不能按安卓的思想去处理:

  • HarmonyOS中是没有事件分发这概念的,只有事件消费,ListContainer先拿到事件,然后是抽屉布局;
  • 根据抽屉在完全展开的位置,在ListContainer收到触摸事件时,把ListContainer事件静止掉,不让其消费;
  • 待抽屉完全展开时,解开ListContainer的事件;
  1. listContainer.setTouchEventListener(new TouchEventListener() { 
  2.     @Override 
  3.     public boolean onTouchEvent(Component component, TouchEvent touchEvent) { 
  4.         marginBottom = directionalLayout.getMarginBottom(); 
  5.         boolean drag_down = listContainer.canScroll(DRAG_DOWN); 
  6.         boolean drag_UP = listContainer.canScroll(DRAG_UP); 
  7.         if (marginBottom == 0 && drag_down) { 
  8.             component.setEnabled(true); 
  9.             return true
  10.         } 
  11.         component.setEnabled(false); 
  12.         return false
  13.     } 
  14. }); 

这里是抽屉容器定位抽屉时,判断是否打开ListContainer事件。

  1. private void setDrawerMarginBottom(int offY) { 
  2.     int bottom = marginBottom + offY; 
  3.     if (bottom > 0) { 
  4.         bottom = 0; 
  5.         listContainer.setEnabled(true); 
  6.     } 
  7.     ....... 

5.背景亮暗变化

  • 首先我们XML布局参照上述布局设计—图1;
  • 背景亮暗的改变根据抽屉位置按比例设置蒙层的透明度;
  1. float alpha = (0.5f - Math.abs((float) bottom / (float) H)) * 0.5f; 
  2. bgComponent.setAlpha(alpha); 

6.回弹效果

运用到了数值动画,在手势抬起时,判断上下临界点决定动画的上下。

  1. private void onTouchUp() { 
  2.     HiLog.info(logLabel, "onTouchUp"); 
  3.     createAnimator(); 
  1. private void createAnimator() { 
  2.     marginBottom = directionalLayout.getMarginBottom(); 
  3.     HiLog.info(logLabel, "createAnimator marginBottom:" + marginBottom); 
  4.     //创建数值动画对象 
  5.     AnimatorValue animatorValue = new AnimatorValue(); 
  6.     //动画时长 
  7.     animatorValue.setDuration(300); 
  8.     //播放前的延迟时间 
  9.     animatorValue.setDelay(0); 
  10.     //循环次数 
  11.     animatorValue.setLoopedCount(0); 
  12.     //动画的播放类型 
  13.     animatorValue.setCurveType(Animator.CurveType.ACCELERATE_DECELERATE); 
  14.     //设置动画过程 
  15.     animatorValue.setValueUpdateListener(new AnimatorValue.ValueUpdateListener() { 
  16.         @Override 
  17.         public void onUpdate(AnimatorValue animatorValue, float value) { 
  18.             HiLog.info(logLabel, "createAnimator value:" + value); 
  19.             if (marginBottom > -H / 4) { // top 
  20.                 HiLog.info(logLabel, "createAnimator top:" + value); 
  21.                 setDrawerBottomOrToP((int) (marginBottom - value * marginBottom)); 
  22.             } else { // bottom 
  23.                 HiLog.info(logLabel, "createAnimator bottom:" + value); 
  24.                 int top = H / 2 + marginBottom; 
  25.                 setDrawerBottomOrToP((int) (marginBottom - value *top)); 
  26.             } 
  27.         } 
  28.     }); 
  29.     //开始启动动画 
  30.     animatorValue.start(); 
  1. private void setDrawerBottomOrToP(int bottom) { 
  2.     if (bottom > 0) { 
  3.         bottom = 0; 
  4.         listContainer.setEnabled(true); 
  5.     } 
  6.  
  7.     if (bottom < -H / 2) { 
  8.         bottom = -H / 2; 
  9.     } 
  10.   
  11.     float alpha = (0.5f - Math.abs((float) bottom / (float) H)) * 0.5f; 
  12.  
  13.     bgComponent.setAlpha(alpha); 
  14.     directionalLayout.setMarginBottom(bottom); 

总结

自定义组件步骤及思考方向:

明确父容器和子view的关系;

如何绘制一般采用以下三个方向:

  1. 已有控件组合;
  2. 采用画布绘制等;
  3. 继承控件扩展功能;

若涉及到触摸事件,需要考虑如何处理事件分发与消费;

动画选择,可根据需求选择合适动画(本文采用属性动画);

计算问题,复杂的需要丰富的数学知识;

性能问题(过度计算,重复绘制,对象重复创建)。

想了解更多内容,请访问:

51CTO和华为官方合作共建的鸿蒙技术社区

https://harmonyos.51cto.com

 

责任编辑:jianghua 来源: 鸿蒙社区
相关推荐

2022-04-24 15:17:56

鸿蒙操作系统

2023-02-20 15:20:43

启动页组件鸿蒙

2021-11-01 10:21:36

鸿蒙HarmonyOS应用

2022-02-21 15:16:30

HarmonyOS鸿蒙操作系统

2022-06-20 15:43:45

switch开关鸿蒙

2022-06-30 14:02:07

鸿蒙开发消息弹窗组件

2021-09-15 10:19:15

鸿蒙HarmonyOS应用

2022-07-15 16:45:35

slider滑块组件鸿蒙

2022-07-06 20:24:08

ArkUI计时组件

2022-07-12 16:56:48

自定义组件鸿蒙

2022-02-16 15:25:31

JS代码Canvas鸿蒙

2022-06-23 07:23:34

自定义组件计时器

2022-02-16 16:09:12

鸿蒙游戏操作系统

2021-12-24 15:46:23

鸿蒙HarmonyOS应用

2021-11-22 10:00:33

鸿蒙HarmonyOS应用

2022-05-20 14:34:20

list组件鸿蒙操作系统

2021-11-24 10:02:53

鸿蒙HarmonyOS应用

2021-12-30 16:10:52

鸿蒙HarmonyOS应用

2021-01-11 11:36:23

鸿蒙HarmonyOSApp开发

2021-08-24 15:25:59

鸿蒙HarmonyOS应用
点赞
收藏

51CTO技术栈公众号