harmonyos 开发自定义组件目前还不是很丰富,在开发过程中常常会有一些特殊效果的组件,这就需要我们额外花一些时间实现。
这里给大家提供了一个 bottomsheet 上拉抽屉的组件,同时通过这个组件示例讲解一下 harmonyos 中的几个自定义控件用到的知识,分享一下自己自定义组件的思路。
效果演示如下图:
实现思路
①布局设计
选择的是相对布局,蒙层区来改变内容区随着抽屉的位置调节透明度。如图 1:
②手势判断
先得出 component 在屏幕的上下左右的坐标,然后手指的坐标是否在 component 内。
/** * (x,y)是否在view的区域内 * * @param component * @param x * @param y * @return */private boolean istouchpointincomponent(component component, float x, float y) { int[] locationonscreen = component.getlocationonscreen(); int left = locationonscreen[0]; int top = locationonscreen[1]; int right = left + component.getestimatedwidth(); int bottom = top + component.getestimatedheight(); boolean iny = y >= top && y = left && x 0) { bottom = 0; listcontainer.setenabled(true); } if (bottom 0) { bottom = 0; listcontainer.setenabled(true); } .......}
⑤背景亮暗变化
首先我们 xml 布局参照上述布局设计—如图 1。背景亮暗的改变根据抽屉位置按比例设置蒙层的透明度。
float alpha = (0.5f - math.abs((float) bottom / (float) h)) * 0.5f;bgcomponent.setalpha(alpha);
⑥回弹效果
运用到了数值动画,在手势抬起时,判断上下临界点决定动画的上下。
private void ontouchup() { hilog.info(loglabel, ontouchup); createanimator();}
private void createanimator() { marginbottom = directionallayout.getmarginbottom(); hilog.info(loglabel, createanimator marginbottom: + marginbottom); //创建数值动画对象 animatorvalue animatorvalue = new animatorvalue(); //动画时长 animatorvalue.setduration(300); //播放前的延迟时间 animatorvalue.setdelay(0); //循环次数 animatorvalue.setloopedcount(0); //动画的播放类型 animatorvalue.setcurvetype(animator.curvetype.accelerate_decelerate); //设置动画过程 animatorvalue.setvalueupdatelistener(new animatorvalue.valueupdatelistener() { @override public void onupdate(animatorvalue animatorvalue, float value) { hilog.info(loglabel, createanimator value: + value); if (marginbottom > -h / 4) { // top hilog.info(loglabel, createanimator top: + value); setdrawerbottomortop((int) (marginbottom - value * marginbottom)); } else { // bottom hilog.info(loglabel, createanimator bottom: + value); int top = h / 2 + marginbottom; setdrawerbottomortop((int) (marginbottom - value *top)); } } }); //开始启动动画 animatorvalue.start();}
private void setdrawerbottomortop(int bottom) { if (bottom > 0) { bottom = 0; listcontainer.setenabled(true); } if (bottom < -h / 2) { bottom = -h / 2; } float alpha = (0.5f - math.abs((float) bottom / (float) h)) * 0.5f; bgcomponent.setalpha(alpha); directionallayout.setmarginbottom(bottom);}
总结
自定义组件步骤及思考方向:
明确父容器和子 view 的关系。
如何绘制一般采用以下三个方向:已有控件组合;采用画布绘制等;继承控件扩展功能。
若涉及到触摸事件,需要考虑如何处理事件分发与消费。
动画选择,可根据需求选择合适动画(本文采用属性动画)。
计算问题,复杂的需要丰富的数学知识。
性能问题(过度计算,重复绘制,对象重复创建)。
代码地址:
https://gitee.com/guangdong-wangduoyu/touch-event-demo
原文标题:harmonyos“上拉抽屉”效果实现!
文章出处:【微信公众号:harmonyos技术社区】欢迎添加关注!文章转载请注明出处。
X-Ray检测设备在LED生产中的应用
闪迪至尊极速固态移动硬盘评测 到底怎么样
Qorvo新型PAC5556电源应用控制器的特性分析
长城WEY-VV5:20万的产品售价13万,CRV和途观都闻之胆寒的SUV!
日本可编程控制器的发展动向
HarmonyOS 中的几个自定义控件介绍
自古红蓝出CP!华为荣耀V9和华为荣耀8青春版这对就很强势!
2021年所有千万级机场将实现RFID行李跟踪系统
为何没有BAT?是扎在魔都上海心头上的一根刺
为什么大多数人都不喜欢写代码文档
为何TWS耳机采用磁吸pogopin充电方案
盐雾试验箱的安装方法及试验方法
植物病虫害诊断仪的检测原理是怎样的
XMG推出蓝天模具笔记本电脑,搭载AMD锐龙 R9 3900处理器
重庆万州首颗拥有自主知识产权的3D传感VCSEL芯片在威科赛乐公司下线
如何使用MAX17201/MAX17211评估板完成m5 EZ的配置
大力建设5G后,我国4G网络变快了
晨星超联发科成全球最大电视芯片供应商
电瓶车充电桩的功能都有哪些
RS232转PROFIBUS协议转换器使用方法