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

鴻蒙HarmonyOS三方件開(kāi)發(fā)指南(8)-RoundedImage

開(kāi)發(fā) OpenHarmony
文章由鴻蒙社區(qū)產(chǎn)出,想要了解更多內(nèi)容請(qǐng)前往:51CTO和華為官方戰(zhàn)略合作共建的鴻蒙技術(shù)社區(qū)https://harmonyos.51cto.com/#zz

[[380586]]

想了解更多內(nèi)容,請(qǐng)?jiān)L問(wèn):

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

https://harmonyos.51cto.com/#zz

1. RoundedImage組件功能介紹

1.1. 功能介紹:

RoundedImage組件可以將圖片顯示成圓形,橢圓形,圓角矩形,目前僅支持上述三種樣式顯示。

1.2. 模擬器上運(yùn)行效果:

2. RoundedImage使用方法

2.1. 新建工程,增加組件Har包依賴

在應(yīng)用模塊中添加HAR,只需要將library-debug.har復(fù)制到entry\libs目錄下即可(由于build.gradle中已經(jīng)依賴的libs目錄下的*.har,因此不需要再做修改)。

2.2. 修改主頁(yè)面的布局文件

修改主頁(yè)面的布局文件ability_main.xml,增加com.custom.library.RoundedImage組件,組件的寬和高自定義。

  1. <?xml version="1.0" encoding="utf-8"?> 
  2. <DirectionalLayout 
  3.     xmlns:ohos="http://schemas.huawei.com/res/ohos" 
  4.     ohos:height="match_parent" 
  5.     ohos:width="match_parent" 
  6.     ohos:orientation="vertical"
  7.     <com.custom.library.RoundedImage 
  8.         ohos:id="$+id:image1" 
  9.         ohos:height="300" 
  10.         ohos:width="300" 
  11.         ohos:top_margin="20vp" 
  12.         ohos:layout_alignment="center"/> 
  13.     <com.custom.library.RoundedImage 
  14.         ohos:id="$+id:image2" 
  15.         ohos:height="400" 
  16.         ohos:width="400" 
  17.         ohos:layout_alignment="center" 
  18.         ohos:top_margin="20vp"/> 
  19.     <com.custom.library.RoundedImage 
  20.         ohos:id="$+id:image3" 
  21.         ohos:height="500" 
  22.         ohos:width="500" 
  23.         ohos:layout_alignment="center" 
  24.         ohos:top_margin="20vp"/> 
  25. </DirectionalLayout> 

 2.3. 修改MainAbilitySlince的UI加載代碼

在MainAbilitySlince類的onStart函數(shù)中。

增加如下代碼可顯示圓角矩形:

  1. @Override 
  2. public void onStart(Intent intent) { 
  3.     super.onStart(intent); 
  4.     super.setUIContent(ResourceTable.Layout_ability_main); 
  5.     RoundedImage roundedImage1 = (RoundedImage) findComponentById(ResourceTable.Id_image1); 
  6.     roundedImage1.setPixelMapToRoundedRect(ResourceTable.Media_photo, 100, 50, 100, 50); 
  7.     RoundedImage roundedImage2 = (RoundedImage) findComponentById(ResourceTable.Id_image2); 
  8.     roundedImage2.setPixelMapToRoundedRect(ResourceTable.Media_photo1, 100, 100, 100, 100); 
  9.     RoundedImage roundedImage3 = (RoundedImage) findComponentById(ResourceTable.Id_image3); 
  10.     roundedImage3.setPixelMapToRoundedRect(ResourceTable.Media_photo2, 50, 100, 50, 100); 
  11.     } 

 增加如下代碼可顯示圓形:

  1. @Override 
  2. public void onStart(Intent intent) { 
  3.     super.onStart(intent); 
  4.     super.setUIContent(ResourceTable.Layout_ability_main); 
  5.     RoundedImage roundedImage1 = (RoundedImage) findComponentById(ResourceTable.Id_image1); 
  6.     roundedImage1.setPixelMapToCircleImage(ResourceTable.Media_photo); 
  7.     RoundedImage roundedImage2 = (RoundedImage) findComponentById(ResourceTable.Id_image2); 
  8.     roundedImage2.setPixelMapToCircleImage(ResourceTable.Media_photo1); 
  9.     RoundedImage roundedImage3 = (RoundedImage) findComponentById(ResourceTable.Id_image3); 
  10.     roundedImage3.setPixelMapToCircleImage(ResourceTable.Media_photo2); 

 增加如下代碼可顯示橢圓形:

  1. @Override 
  2. public void onStart(Intent intent) { 
  3.     super.onStart(intent); 
  4.     super.setUIContent(ResourceTable.Layout_ability_main); 
  5.     RoundedImage roundedImage1 = (RoundedImage) findComponentById(ResourceTable.Id_image1); 
  6.     roundedImage1.setPixelMapToOvalImage(ResourceTable.Media_photo3); 
  7.     RoundedImage roundedImage2 = (RoundedImage) findComponentById(ResourceTable.Id_image2); 
  8.     roundedImage2.setPixelMapToOvalImage(ResourceTable.Media_photo4); 
  9.     RoundedImage roundedImage3 = (RoundedImage) findComponentById(ResourceTable.Id_image3); 
  10.     roundedImage3.setPixelMapToOvalImage(ResourceTable.Media_photo5); 

 3. RoundedImage開(kāi)發(fā)實(shí)現(xiàn)

3.1. 新建一個(gè)Module

新建一個(gè)Module,類型選擇HarmonyOS Library,模塊名為library。

3.2. 新建一個(gè)RoundedImage類

新建一個(gè)RoundedImage類,繼承自Image類,實(shí)現(xiàn)DrawTask.onDraw接口,代碼如下:

用于繪制圓形:

  1. @Override 
  2. public void onDraw(Component component, Canvas canvas) { 
  3.     float centerX = getWidth() / 2f; 
  4.     float centerY = getHeight() / 2f; 
  5.     float radius = Math.min(centerX, centerY); 
  6.     Paint paint = new Paint(); 
  7.     Shader shader = new PixelMapShader(holder, Shader.TileMode.CLAMP_TILEMODE, Shader.TileMode.CLAMP_TILEMODE); 
  8.     paint.setShader(shader, Paint.ShaderType.SWEEP_SHADER); 
  9.     canvas.drawCircle(centerX, centerY, radius, paint); 

 用于繪制橢圓形:

  1. @Override 
  2. public void onDraw(Component component, Canvas canvas) { 
  3.     Paint paint = new Paint(); 
  4.     Shader shader = new PixelMapShader(holder, Shader.TileMode.CLAMP_TILEMODE, Shader.TileMode.CLAMP_TILEMODE); 
  5.     paint.setShader(shader, Paint.ShaderType.SWEEP_SHADER); 
  6.     PixelMap pixelMap = holder.getPixelMap(); 
  7.     int min = Math.min(pixelMap.getImageInfo().size.width, pixelMap.getImageInfo().size.height); 
  8.     int radiusX = Math.min(min, minImageLength); 
  9.     float halfRadiusX = radiusX / 2f; 
  10.     float quarterRadiusX = radiusX / 4f; 
  11.     float left = getWidth() / 2f - halfRadiusX; 
  12.     float right = getWidth() / 2f + halfRadiusX; 
  13.     float top = getHeight() / 2f - quarterRadiusX; 
  14.     float bottom = getHeight() / 2f + quarterRadiusX; 
  15.     RectFloat rect = new RectFloat(lefttopright, bottom); 
  16.     canvas.drawOval(rect, paint); 

 用于設(shè)置圓角矩形,調(diào)用Image方法進(jìn)行設(shè)置:

  1. setCornerRadii(new float[]{topLeft, topLeft, topRigth, topRigth, bottomRight, bottomRight, bottomLeft, bottomLeft}); 

3.3. 編譯HAR包

利用Gradle可以將HarmonyOS Library庫(kù)模塊構(gòu)建為HAR包,構(gòu)建HAR包的方法如下:

在Gradle構(gòu)建任務(wù)中,雙擊PackageDebugHar或PackageReleaseHar任務(wù),構(gòu)建Debug類型或Release類型的HAR。

待構(gòu)建任務(wù)完成后,可以在loadingview> bulid > outputs > har目錄中,獲取生成的HAR包。

項(xiàng)目源代碼地址:https://github.com/isoftstone-dev/RoundedImage_HarmonyOS

歡迎交流:HWIS-HOS@isoftstone.com

©著作權(quán)歸作者和HarmonyOS技術(shù)社區(qū)共同所有,如需轉(zhuǎn)載,請(qǐng)注明出處,否則將追究法律責(zé)任。

想了解更多內(nèi)容,請(qǐng)?jiān)L問(wèn):

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

https://harmonyos.51cto.com/#zz

 

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

2021-03-01 09:48:24

鴻蒙HarmonyOS應(yīng)用開(kāi)發(fā)

2021-02-24 15:22:47

鴻蒙HarmonyOS應(yīng)用開(kāi)發(fā)

2021-01-13 09:40:31

鴻蒙HarmonyOS開(kāi)發(fā)

2021-04-16 09:28:18

鴻蒙HarmonyOS應(yīng)用

2021-06-28 14:48:03

鴻蒙HarmonyOS應(yīng)用

2021-01-18 09:52:20

鴻蒙HarmonyOS開(kāi)發(fā)

2021-02-04 09:45:19

鴻蒙HarmonyOS應(yīng)用開(kāi)發(fā)

2021-02-26 14:15:27

鴻蒙HarmonyOS應(yīng)用開(kāi)發(fā)

2021-01-12 12:04:40

鴻蒙HarmonyOS應(yīng)用開(kāi)發(fā)

2021-01-21 13:21:18

鴻蒙HarmonyOSPhotoview組件

2021-01-20 09:54:56

鴻蒙HarmonyOS開(kāi)發(fā)

2021-03-01 14:01:41

鴻蒙HarmonyOS應(yīng)用開(kāi)發(fā)

2021-05-12 15:17:39

鴻蒙HarmonyOS應(yīng)用

2021-03-31 09:50:25

鴻蒙HarmonyOS應(yīng)用開(kāi)發(fā)

2021-01-22 17:33:03

鴻蒙HarmonyOS應(yīng)用開(kāi)發(fā)

2021-04-12 09:36:54

鴻蒙HarmonyOS應(yīng)用

2021-03-19 17:42:01

鴻蒙HarmonyOS應(yīng)用開(kāi)發(fā)

2021-04-20 09:42:20

鴻蒙HarmonyOS應(yīng)用開(kāi)發(fā)

2021-03-10 15:03:40

鴻蒙HarmonyOS應(yīng)用

2023-02-07 15:43:13

三方庫(kù)適配鴻蒙
點(diǎn)贊
收藏

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