自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

HarmonyOS自定義組件之仿微信朋友圈主頁

開發(fā) OpenHarmony
自定義組件是由開發(fā)者定義的具有一定特性的組件,通過擴(kuò)展 Component 或其子類實(shí)現(xiàn),可以精確控制屏幕元素的外觀,實(shí)現(xiàn)開發(fā)者想要達(dá)到的效果,也可響應(yīng)用戶的點(diǎn)擊、觸摸、長按等操作。

[[433138]]

想了解更多內(nèi)容,請訪問:

51CTO和華為官方合作共建的鴻蒙技術(shù)社區(qū)

https://harmonyos.51cto.com

前言

在實(shí)際開發(fā)過程中,我們經(jīng)常會遇到一些系統(tǒng)原有組件無法滿足的情況,而HarmonyOS提供了自定義組件的方式,我們使用自定義組件來滿足項(xiàng)目需求。自定義組件是由開發(fā)者定義的具有一定特性的組件,通過擴(kuò)展 Component 或其子類實(shí)現(xiàn),可以精確控制屏幕元素的外觀,實(shí)現(xiàn)開發(fā)者想要達(dá)到的效果,也可響應(yīng)用戶的點(diǎn)擊、觸摸、長按等操作。下面通過自定義一個仿微信朋友圈主頁的組件來了解一下自定義組件的過程。

簡述

首先關(guān)于自定義組件,一般遵循以下幾個方式

首先,創(chuàng)建一個繼承 Component 或其子類的自定義組件類,并添加構(gòu)造方法,如 MyComponent 。

實(shí)現(xiàn) Component.EstimateSizeListener 接口,在 onEstimateSize 方法中進(jìn)行組件測量,并通過 setEstimatedSize 方法通知組件。

自定義組件測量出的大小需通過 setEstimatedSize 通知組件,并且必須返回true使測量值生效。setEstimatedSize 方法的入?yún)y帶模式信息,可使用 Component.EstimateSpec.getChildSizeWithMode 方法進(jìn)行拼接。

測量模式

自定義xml屬性,通過構(gòu)造方法中攜帶的參數(shù) attrSet,可以獲取到在 xml 中配置的屬性值,并應(yīng)用在該自定義組件中。

實(shí)現(xiàn) Component.DrawTask 接口,在 onDraw 方法中執(zhí)行繪制任務(wù),該方法提供的畫布 Canvas,可以精確控制屏幕元素的外觀。在執(zhí)行繪制任務(wù)之前,需要定義畫筆 Paint。

實(shí)現(xiàn) Component.TouchEventListener 或其他事件的接口,使組件可響應(yīng)用戶輸入。

在 xml 文件中創(chuàng)建并配置自定義組件。

在 HarmonyOS 中 Component 是視圖的父類,既然組件都是繼承 Component 來實(shí)現(xiàn)的,那么我們也可以繼承它來實(shí)現(xiàn)我們想要的視圖了,根據(jù)具體流程,我們按照示例代碼來了解一下大致流程:

創(chuàng)建自定義布局

  1. ... 
  2. public class MyComponent extends Component implements Component.DrawTask,Component.EstimateSizeListener { 
  3.     private Paint paint; 
  4.     private Paint paintText; 
  5.  
  6.     private PixelMap bigImage; 
  7.  
  8.     private PixelMap smallImage; 
  9.  
  10.     public MyComponent(Context context) { 
  11.         this(context, null); 
  12.     } 
  13.  
  14.     public MyComponent(Context context, AttrSet attrSet) { 
  15.         this(context, attrSet,""); 
  16.     } 
  17.  
  18.     public MyComponent(Context context, AttrSet attrSet, String styleName) { 
  19.         super(context, attrSet, styleName); 
  20.         init(context); 
  21.     } 
  22.  
  23.     public void init(Context context) { 
  24.         // 設(shè)置測量組件的偵聽器 
  25.         setEstimateSizeListener(this); 
  26.         // 初始化畫筆 
  27.         initPaint(); 
  28.         // 添加繪制任務(wù) 
  29.         addDrawTask(this); 
  30.     } 
  31.  
  32.     private void initPaint() { 
  33.         paint = new Paint(); 
  34.         paint.setAntiAlias(true); 
  35.         paint.setStrokeCap(Paint.StrokeCap.ROUND_CAP); 
  36.         paint.setStyle(Paint.Style.STROKE_STYLE); 
  37.  
  38.         paintText = new Paint(); 
  39.         paintText.setStrokeCap(Paint.StrokeCap.ROUND_CAP); 
  40.         paintText.setStyle(Paint.Style.FILL_STYLE); 
  41.         paintText.setColor(Color.WHITE); 
  42.         paintText.setTextSize(50); 
  43.         paintText.setAntiAlias(true); 
  44.  
  45.         bigImage = PixelMapUtils.createPixelMapByResId(ResourceTable.Media_imageDev, getContext()).get(); 
  46.         smallImage = PixelMapUtils.createPixelMapByResId(ResourceTable.Media_icon, getContext()).get(); 
  47.  
  48.     } 
  49.  
  50.     @Override 
  51.     public void addDrawTask(Component.DrawTask task) { 
  52.         super.addDrawTask(task); 
  53.         task.onDraw(this, mCanvasForTaskOverContent); 
  54.     } 
  55.  
  56.     @Override 
  57.     public boolean onEstimateSize(int widthEstimateConfig, int heightEstimateConfig) { 
  58.         int width = Component.EstimateSpec.getSize(widthEstimateConfig); 
  59.         int height = Component.EstimateSpec.getSize(heightEstimateConfig); 
  60.         setEstimatedSize( 
  61.                 Component.EstimateSpec.getChildSizeWithMode(width, width, Component.EstimateSpec.NOT_EXCEED), 
  62.                 Component.EstimateSpec.getChildSizeWithMode(height, height, Component.EstimateSpec.NOT_EXCEED)); 
  63.         return true
  64.     } 
  65.  
  66.     @Override 
  67.     public void onDraw(Component component, Canvas canvas) { 
  68.         int width = getWidth(); 
  69.         int center = width / 2; 
  70.  
  71.         float length = (float) (center - Math.sqrt(2) * 1.0f / 2 * center); 
  72.  
  73.         // 獲取大圖片的大小 
  74.  
  75.         Size bigImageSize = bigImage.getImageInfo().size
  76.         RectFloat bigImageRect = new RectFloat(0, 0, width,  bigImageSize.height); 
  77.  
  78.         // 獲取小圖片的大小 
  79.         Size smallImageSize = smallImage.getImageInfo().size
  80.         RectFloat smallImageRect = new RectFloat(length * 5, length * 5 - 50, 1100, 1030); 
  81.  
  82.         canvas.drawPixelMapHolderRect(new PixelMapHolder(bigImage), bigImageRect, paint); 
  83.         canvas.drawPixelMapHolderRect(new PixelMapHolder(smallImage), smallImageRect, paint); 
  84.         canvas.drawText(paintText,"ABCDEFG",width - length * 3.3f, bigImageSize.height - length * 0.2f); 
  85.  
  86.     } 
  87.  

如上代碼,我們創(chuàng)建一個 MyComponent 繼承 Component ,并且在構(gòu)造方法中,初始化一些畫筆屬性,傳入默認(rèn)的圖片,當(dāng)然也可以通過調(diào)用接口的方式在引用的地方傳入圖片。然后在 ondraw 方法中做具體的畫筆操作。通過 canvas.drawPixelMapHolderRect 方法畫出一大一小兩張可堆疊的圖片,并調(diào)整好位置參數(shù)。

在 Ability 中引用

實(shí)現(xiàn)組件之后,我們就可以在我們需要展示的 Ability 去調(diào)用這個自定義組件了。

  1. ... 
  2. public class ImageAbilitySlice extends AbilitySlice { 
  3.  
  4.     @Override 
  5.     protected void onStart(Intent intent) { 
  6.         super.onStart(intent); 
  7. //        super.setUIContent(ResourceTable.Layout_ability_image_main); 
  8.         drawMyComponent(); 
  9.     } 
  10.  
  11.     private void drawMyComponent() { 
  12.         // 聲明布局 
  13.         ScrollView myLayout = new ScrollView(this); 
  14.         DirectionalLayout.LayoutConfig config = new DirectionalLayout.LayoutConfig( 
  15.                 DirectionalLayout.LayoutConfig.MATCH_PARENT, DirectionalLayout.LayoutConfig.MATCH_PARENT); 
  16.         myLayout.setLayoutConfig(config); 
  17.         myLayout.setReboundEffect(true); 
  18.         MyComponent customComponent = new MyComponent(this); 
  19.         myLayout.addComponent(customComponent); 
  20.         super.setUIContent(myLayout); 
  21.     } 
  22.  

在XML文件中引用

  1. <?xml version="1.0" encoding="utf-8"?> 
  2. <ScrollView 
  3.     ohos:id="$+id:dl" 
  4.     xmlns:ohos="http://schemas.huawei.com/res/ohos" 
  5.     ohos:height="match_parent" 
  6.     ohos:width="match_parent" 
  7.     ohos:alignment="center" 
  8.     ohos:rebound_effect="true" 
  9.     ohos:orientation="vertical"
  10.  
  11.     <com.example.harmonyosdeveloperchenpan.MyComponent 
  12.         ohos:id="$+id:myComponent" 
  13.         ohos:height="match_parent" 
  14.         ohos:width="match_parent"/> 
  15.  
  16. </ScrollView> 

需要注意的是,微信朋友圈主頁的滑動有下拉回彈效果,所以我們需要設(shè)置 ScrollView 的布局屬性。通過在代碼中調(diào)用 setReboundEffect(true) 或者 xml 中設(shè)置 ohos:rebound_effect=“true” 來實(shí)現(xiàn)。

效果展示

#星光計劃1.0# HarmonyOS 自定義組件之仿微信朋友圈主頁-鴻蒙HarmonyOS技術(shù)社區(qū)

想了解更多內(nèi)容,請訪問:

51CTO和華為官方合作共建的鴻蒙技術(shù)社區(qū)

https://harmonyos.51cto.com

 

責(zé)任編輯:jianghua 來源: 鴻蒙社區(qū)
相關(guān)推薦

2021-11-19 09:48:33

鴻蒙HarmonyOS應(yīng)用

2013-11-06 14:25:30

微信朋友圈

2015-09-01 16:55:42

微信朋友圈圖片

2021-03-30 08:36:53

iOS微信朋友圏

2021-05-31 08:23:47

應(yīng)用開發(fā)前端

2023-03-09 07:29:28

微信朋友圈架構(gòu)

2013-04-12 03:40:53

微信開放平臺朋友圈

2021-06-23 10:24:06

微信macOS移動應(yīng)用

2013-12-06 16:39:56

2020-03-13 13:19:28

微信廣告隱私

2013-11-29 11:46:49

微信朋友圈朋友圈生意淘寶

2021-08-14 23:23:49

ios微信朋友圈

2022-01-27 07:40:27

iOS微信朋友圈

2019-12-24 13:00:03

微信朋友圈移動應(yīng)用

2021-11-01 10:21:36

鴻蒙HarmonyOS應(yīng)用

2021-03-11 22:23:46

微信Mac版朋友圈

2022-01-12 21:00:08

微信安卓騰訊

2021-03-31 06:05:08

微信朋友圈騰訊

2020-11-05 14:26:43

微信朋友圏7.0.18

2022-06-30 14:02:07

鴻蒙開發(fā)消息彈窗組件
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號