HarmonyOS三方件开发指南(19)-BGABadgeView徽章组件

开发 OpenHarmony
文章由鸿蒙社区产出,想要了解更多内容请前往:51CTO和华为官方战略合作共建的鸿蒙技术社区https://harmonyos.51cto.com

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

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

https://harmonyos.51cto.com

引言

现在很多的APP会有新消息/未接来电/未读消息/新通知圆球红点提示,典型的以微信、QQ新消息提示为例,当微信朋友圈有新的朋友更新/发布朋友圈消息后,在微信的底部切换卡上会有一个红色的小圆球红点,表示有新消息,提示用户查看。在消息通讯类的app中十分实用。

功能介绍

鸿蒙BGABadgeView 徽章组件,主要功能包括:传入图片生成徽章,设置文本生成文本徽章,并且每个徽章都具有拖拽超范围即可消除,范围内即可回到原位置。模拟机效果图如下

1.图片徽章:

【软通动力】HarmonyOS三方件开发指南(19)-BGABadgeView徽章组件

2.文字徽章:

3.拖动徽章爆炸:

使用时候,直接将其下载,作为一个har包导入到自己的项目中即可。下面则详细介绍BGABadgeView 的使用以及开发指南。

BGABadgeView 使用指南

Ø 新建工程, 添加组件Har包依赖

在应用模块中添加HAR,只需要将verificationcodeview-debug.har复制到entry\libs目录下即可

Ø 修改配置文件

1. 修改主页面的布局文件:

  1. <?xml version="1.0" encoding="utf-8"?> 
  2. <DependentLayout 
  3.     xmlns:ohos="http://schemas.huawei.com/res/ohos" 
  4.     ohos:height="match_parent" 
  5.     ohos:width="match_parent" 
  6.     ohos:id="$+id:layout1" 
  7.     ohos:orientation="vertical"
  8.     <DependentLayout 
  9.         ohos:id="$+id:dependent1" 
  10.         ohos:height="200vp" 
  11.         ohos:width="match_parent"
  12.  
  13.         <com.example.bgabadgecomp_library.BAGDragBadgeImage 
  14.             ohos:top_margin="15vp" 
  15.             ohos:right_margin="10vp" 
  16.             ohos:bottom_margin="10vp" 
  17.             ohos:height="80vp" 
  18.             ohos:width="80vp" 
  19.             ohos:scale_mode="zoom_center" 
  20.             ohos:image_src="$media:avator" 
  21.             ohos:id="$+id:image1" 
  22.             ohos:below="$id:title" 
  23.             ohos:left_margin="30vp"/> 
  24.         <Image 
  25.             ohos:top_margin="15vp" 
  26.             ohos:right_margin="10vp" 
  27.             ohos:bottom_margin="10vp" 
  28.             ohos:height="80vp" 
  29.             ohos:width="80vp" 
  30.             ohos:scale_mode="zoom_center" 
  31.             ohos:image_src="$media:avator" 
  32.             ohos:id="$+id:image2" 
  33.             ohos:end_of="$id:image1" 
  34.             ohos:below="$id:title" 
  35.             ohos:left_margin="10vp"/> 
  36.            </DependentLayout> 
  37.         <Text 
  38.             ohos:left_margin="30vp" 
  39.             ohos:id="$+id:text1" 
  40.             ohos:top_margin="10vp" 
  41.             ohos:right_margin="15vp" 
  42.             ohos:bottom_margin="10vp" 
  43.             ohos:height="40vp" 
  44.             ohos:width="match_parent" 
  45.             ohos:text="测试1" 
  46.             ohos:below="$id:dependent1" 
  47.             ohos:text_size="20vp"/> 
  48. </DependentLayout> 

 2.修改MainAbilitySlice中的UI加载代码

在MainAbilitySlince类的onStart函数中,增加如下代码:

  1. @Override 
  2. public void onStart(Intent intent) { 
  3.     super.onStart(intent); 
  4.     super.setUIContent(ResourceTable.Layout_ability_main); 
  5.  
  6.     BAGDragBadgeImage bagDragBadgeImage = (BAGDragBadgeImage) findComponentById(ResourceTable.Id_image1); 
  7.     bagDragBadgeImage.setCornerRadius(bagDragBadgeImage.getWidth() / 2); // 圆形边框 
  8.     DependentLayout stackLayout = (DependentLayout) findComponentById(ResourceTable.Id_layout1); 
  9.  
  10.     Image image2 = (Image) findComponentById(ResourceTable.Id_image2); 
  11.     image2.setCornerRadius(20); 
  12.  
  13.     DependentLayout.LayoutConfig config = new DependentLayout.LayoutConfig(DependentLayout.LayoutConfig.MATCH_PARENT, DependentLayout.LayoutConfig.MATCH_PARENT); 
  14.  
  15.     RoundRectImage roundRectImage = RoundRectImage.attach2Window(this, stackLayout, image2, config, BGABadgeViewHelper.getPixelMap(this, ResourceTable.Media_avatar_vip)); 
  16.  
  17.     RoundRectText roundRectText = RoundRectText.attach2Window(this, stackLayout, image3, config); 
  18.  
  19.     Text text1  = (Text) findComponentById(ResourceTable.Id_text1); 
  20.     RoundRectText roundText1 = RoundRectText.attach2Window(this, stackLayout, text1, config); 
  21.     roundText1.setBadgeText("qqqqqqqq"); 
  22.     List<Component> componentList = new ArrayList<>(); 
  23.     componentList.add(roundRectText); 
  24.     componentList.add(roundText1); 
  25.  
  26.     stackLayout.setTouchEventListener(new Component.TouchEventListener() { 
  27.         @Override 
  28.         public boolean onTouchEvent(Component component, TouchEvent event) { 
  29.  
  30.             switch (event.getAction()) { 
  31.                 case TouchEvent.PRIMARY_POINT_DOWN:// 手指第一次触摸到屏幕 
  32.                     int startX = (int) event.getPointerPosition(event.getIndex()).getX(); 
  33.                     int startY = (int) event.getPointerPosition(event.getIndex()).getY(); 
  34.                    if (startX < roundRectImage.getCircleLeft() + 2 * roundRectImage.getCircleRadius() 
  35.                            && startX > roundRectImage.getCircleLeft() 
  36.                            && startY < roundRectImage.getCircleTop() + 2 * roundRectImage.getCircleRadius() 
  37.                            && startY > roundRectImage.getCircleTop()) { 
  38.                        roundRectImage.setDraggedListener(DRAG_HORIZONTAL_VERTICAL, roundRectImage ); 
  39.  
  40.                        for (Component component1 : componentList) { 
  41.                            component1.setDraggedListener(DRAG_HORIZONTAL_VERTICAL,null); 
  42.                        } 
  43.                    } else { 
  44.                        roundRectImage.setDraggedListener(DRAG_HORIZONTAL_VERTICAL, null ); 
  45.  
  46.                        for (Component component1 : componentList) { 
  47.                            RoundRectText rectText = (RoundRectText) component1; 
  48.                            if (startX < rectText.getCircleLeft() + 2 * rectText.getRadius() 
  49.                                    && startX > rectText.getCircleLeft() 
  50.                                    && startY < rectText.getCircleTop() + 2 * rectText.getRadius() 
  51.                                    && startY > rectText.getCircleTop()) { 
  52.                                component1.setDraggedListener(DRAG_HORIZONTAL_VERTICAL, (Component.DraggedListener) component1); 
  53.                            } else { 
  54.                                component1.setDraggedListener(DRAG_HORIZONTAL_VERTICAL,null); 
  55.                            } 
  56.                        } 
  57.                    } 
  58.  
  59.                     break; 
  60.                 case TouchEvent.PRIMARY_POINT_UP: 
  61.                 case TouchEvent.POINT_MOVE: 
  62.                 default
  63.                     break; 
  64.             } 
  65.             return true
  66.         } 
  67.     }); 

 复制通过以上两个步骤,就实现了简单的徽章组件,接下来在一起看下徽章组件是如何实现的。

BGABadgeView 开发指南

新建一个Module

新建一个Module,类型选择HarmonyOS Library,模块名为VerificationCodeView,如图:

新建一个RoundRectText类

1.实现自定义RoundRectText绘制

  1. @Override 
  2. public void onDraw(Component component, Canvas canvas){ 
  3.     length = mBadgeText.length(); 
  4.     Paint mTextPain = new Paint(); 
  5.     mTextPain.setColor(Color.WHITE); 
  6.     mTextPain.setStyle(Paint.Style.FILL_STYLE); 
  7.     mTextPain.setTextSize(30); 
  8.     mTextPain.setFont(Font.DEFAULT); 
  9.     Rect textBounds = mTextPain.getTextBounds(mBadgeText); 
  10.  
  11.     Paint mBadgePaint = new Paint(); 
  12.     mBadgePaint.setColor(Color.RED); 
  13.     mBadgePaint.setStyle(Paint.Style.FILL_STYLE); 
  14.     mBadgePaint.setStrokeWidth(5); 
  15.     if (mBadgeRectF == null) { 
  16.         switch (mBadgeGravity) { 
  17.             case RightTop: 
  18.                 int left = mComponent.getLeft(); 
  19.                 int top = mComponent.getTop(); 
  20.                 circleLeft = mComponent.getWidth() + left - 2 * radius - 15 * (length - 2); 
  21.                 circleTop = top
  22.                 mBadgeRectF = new RectFloat( circleLeft, circleTop, circleLeft + 2 * radius + 15 * (length - 2)  , circleTop + 2 * radius); 
  23.                 break; 
  24.             case RightCenter: 
  25.                 left = mComponent.getLeft(); 
  26.                 top = mComponent.getTop(); 
  27.                 circleLeft = mComponent.getWidth() + left - 2 * radius - 15 * (length - 2); 
  28.                 circleTop = top + (float)mComponent.getHeight() / 2 - radius; 
  29.                 mBadgeRectF = new RectFloat( circleLeft, circleTop, circleLeft + 2 * radius + 15 * (length - 2) , circleTop + 2 * radius); 
  30.  
  31.                 break; 
  32.             case RightBottom: 
  33.                 mBadgeRectF = new RectFloat(); 
  34.                 left = mComponent.getLeft(); 
  35.                 top = mComponent.getTop(); 
  36.                 circleLeft = mComponent.getWidth() + left - 2 * radius - 15 * (length - 2) ; 
  37.                 circleTop = top + mComponent.getHeight() - 2 * radius; 
  38.                 mBadgeRectF = new RectFloat( circleLeft, circleTop, circleLeft + 2 * radius + 15 * (length - 2) , circleTop + 2 * radius); 
  39.  
  40.                 break; 
  41.             default
  42.                 break; 
  43.         } 
  44.     } 
  45.     path = (float) Math.sqrt((mBadgeRectF.left - circleLeft) * (mBadgeRectF.left - circleLeft) + (mBadgeRectF.top - circleTop) * (mBadgeRectF.top - circleTop)); 
  46.     isOverPath = path > overPath; 
  47.     float offSet = (float) (textBounds.top + textBounds.bottom) / 2; 
  48.     float boundsX = 0 ; 
  49.     if( 15 * length < (mBadgeRectF.right - mBadgeRectF.left)){ 
  50.        float temp =  mBadgeRectF.right - mBadgeRectF.left - 15 * length; 
  51.         boundsX = temp / 2; 
  52.     } 
  53.    float roundNum = 2 * radius / (mBadgeRectF.right - mBadgeRectF.left) ; 
  54.     canvas.drawRoundRect(mBadgeRectF,roundNum * radius ,roundNum * radius, mBadgePaint); 
  55.     canvas.drawText(mTextPain, mBadgeText, mBadgeRectF.left + boundsX, mBadgeRectF.top + radius - offSet); 

 2.生成拖拽事件

  1. @Override 
  2. public void onDragDown(Component component, DragInfo dragInfo) { 
  3.      pointX = dragInfo.downPoint.getPointX(); 
  4.      pointY = dragInfo.downPoint.getPointY(); 
  5.     if (pointX <= circleLeft || pointX >= circleLeft + 2 * radius + 15 * (length - 2) 
  6.             || pointY <= circleTop || pointY >= circleTop + 2 * radius) { 
  7.         onDragCancel(component, dragInfo); 
  8.     } 
  9.  
  10. @Override 
  11. public void onDragStart(Component component, DragInfo dragInfo) { 
  12. @Override 
  13. public void onDragUpdate(Component component, DragInfo dragInfo) { 
  14.  
  15.     float left = mBadgeRectF.left
  16.     float right = mBadgeRectF.right
  17.     float top = mBadgeRectF.top
  18.     float bottom = mBadgeRectF.bottom; 
  19.  
  20.     if (pointX <= circleLeft || pointX >= circleLeft + 2 * radius + 15 * (length - 2) 
  21.             || pointY <= circleTop || pointY >= circleTop + 2 * radius) { 
  22.         onDragCancel(component, dragInfo); 
  23.     } else { 
  24.         mBadgeRectF.left = (float) (left + dragInfo.xOffset); 
  25.         mBadgeRectF.right = (float) (right + dragInfo.xOffset); 
  26.         mBadgeRectF.top = (float) (top + dragInfo.yOffset); 
  27.         mBadgeRectF.bottom = (float) (bottom + dragInfo.yOffset); 
  28.         invalidate(); 
  29.     } 
  30.  
  31. @Override 
  32. public void onDragEnd(Component component, DragInfo dragInfo) { 
  33.     if (isOverPath) { 
  34.         explosionField.explode(component, mBadgeRectF, explosionFieldColor); 
  35.     } else { 
  36.         mBadgeRectF = new RectFloat(circleLeft, circleTop , circleLeft + 2 * radius + 15 * (length - 2), circleTop + 2 * radius); 
  37.         invalidate(); 
  38.     } 
  39. @Override 
  40. public void onDragCancel(Component component, DragInfo dragInfo) { 
  41.     mBadgeRectF = new RectFloat(circleLeft, circleTop , circleLeft + 2 * radius + 15 * (length - 2), circleTop + 2 * radius); 
  42.     invalidate(); 

 具体代码请下载项目查看。

编译HAR包

利用Gradle可以将HarmonyOS Library库模块构建为HAR包,构建HAR包的方法如下:

在Gradle构建任务中,双击PackageDebugHar或PackageReleaseHar任务,构建Debug类型或Release类型的HAR。

待构建任务完成后,可以在工程目录中的VerificationCodeView> bulid > outputs > har目录中,获取生成的HAR包。

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

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

https://harmonyos.51cto.com

 

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

2021-01-18 09:52:20

鸿蒙HarmonyOS开发

2021-06-28 14:48:03

鸿蒙HarmonyOS应用

2021-02-04 09:45:19

鸿蒙HarmonyOS应用开发

2021-01-12 12:04:40

鸿蒙HarmonyOS应用开发

2021-01-20 09:54:56

鸿蒙HarmonyOS开发

2021-01-21 13:21:18

鸿蒙HarmonyOSPhotoview组件

2021-02-24 15:22:47

鸿蒙HarmonyOS应用开发

2021-03-01 09:48:24

鸿蒙HarmonyOS应用开发

2021-03-19 17:42:01

鸿蒙HarmonyOS应用开发

2021-04-20 09:42:20

鸿蒙HarmonyOS应用开发

2021-01-13 09:40:31

鸿蒙HarmonyOS开发

2021-02-04 13:06:38

鸿蒙HarmonyOS应用开发

2021-04-16 09:28:18

鸿蒙HarmonyOS应用

2021-01-22 17:33:03

鸿蒙HarmonyOS应用开发

2021-03-31 09:50:25

鸿蒙HarmonyOS应用开发

2021-02-26 14:15:27

鸿蒙HarmonyOS应用开发

2021-04-12 09:36:54

鸿蒙HarmonyOS应用

2021-03-01 14:01:41

鸿蒙HarmonyOS应用开发

2021-03-10 15:03:40

鸿蒙HarmonyOS应用

2021-06-21 15:21:52

鸿蒙HarmonyOS应用开发
点赞
收藏

51CTO技术栈公众号