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

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-12 12:04:40

鴻蒙HarmonyOS應用開發

2021-01-21 13:21:18

鴻蒙HarmonyOSPhotoview組件

2021-01-20 09:54:56

鴻蒙HarmonyOS開發

2021-06-28 14:48:03

鴻蒙HarmonyOS應用

2021-01-18 09:52:20

鴻蒙HarmonyOS開發

2021-02-04 09:45:19

鴻蒙HarmonyOS應用開發

2021-03-01 09:48:24

鴻蒙HarmonyOS應用開發

2021-02-24 15:22:47

鴻蒙HarmonyOS應用開發

2021-01-22 17:33:03

鴻蒙HarmonyOS應用開發

2021-03-19 17:42:01

鴻蒙HarmonyOS應用開發

2021-04-20 09:42:20

鴻蒙HarmonyOS應用開發

2021-02-04 13:06:38

鴻蒙HarmonyOS應用開發

2021-01-13 09:40:31

鴻蒙HarmonyOS開發

2021-04-16 09:28:18

鴻蒙HarmonyOS應用

2021-02-26 14:15:27

鴻蒙HarmonyOS應用開發

2021-03-01 14:01:41

鴻蒙HarmonyOS應用開發

2021-04-12 09:36:54

鴻蒙HarmonyOS應用

2021-03-31 09:50:25

鴻蒙HarmonyOS應用開發

2021-03-10 15:03:40

鴻蒙HarmonyOS應用

2021-08-02 14:54:50

鴻蒙HarmonyOS應用
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 在线播放一区二区三区 | 精品国产不卡一区二区三区 | 中文字幕在线免费观看 | 精品久久久久久亚洲精品 | 婷婷五月色综合香五月 | 久久99精品视频 | 国产精品揄拍一区二区 | 国产伦精品一区二区 | 国产精品国产精品国产专区不卡 | 成人免费高清 | av在线免费播放 | 男女免费在线观看视频 | 中文字幕在线看第二 | 久久精品国产一区老色匹 | se婷婷| 精品国产乱码久久久久久丨区2区 | 欧美色欧美亚洲另类七区 | 日韩中文字幕在线不卡 | 国产精品区一区二 | 一级aaaa毛片 | 日本欧美大片 | 综合久久av | 成人在线观看免费 | 久久免费国产视频 | 精品综合久久久 | 亚洲在线中文字幕 | 国产精品不卡一区二区三区 | 成人深夜小视频 | 亚洲人成在线播放 | 亚洲天堂日韩精品 | 久久精品国产亚洲一区二区 | 97精品一区二区 | 91福利网 | 欧美一级三级 | 欧美性受 | 国产乱码精品一品二品 | av一级在线观看 | 亚洲成人一区二区 | 久久久成人精品 | 99精品免费久久久久久日本 | 欧美激情精品久久久久久 |