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

2020征文-TV「續(xù)3.1.1 文本組件」不需要背景圖,自定義繪制會(huì)更好

開(kāi)發(fā) 前端
本小節(jié)我們對(duì)ShapeElement類做具體介紹,在后面的組件中將不在詳細(xì)介紹,可以根據(jù)本節(jié)內(nèi)容自定義不同樣式的組件。對(duì)于ShapeElement類中的另外兩個(gè)繪制幾何圖形的方法,我將在后面的小節(jié)中單獨(dú)介紹。

 [[356867]]

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

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

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

上節(jié)我們對(duì)Text組件有了詳細(xì)的認(rèn)知,我們可以通過(guò)設(shè)置Text組件的各類屬性來(lái)讓Text組件美觀起來(lái),但是我們僅僅能設(shè)置背景、字體大小、字體大小自適應(yīng)、內(nèi)容折行、外邊距、內(nèi)邊距等。很多時(shí)候我們想給字體設(shè)置邊框、圓角、陰影等等特殊的效果,但是在屬性中沒(méi)有特定的屬性可以完成這件事。當(dāng)然我們可以給Text組件設(shè)置背景圖片,這樣也可以達(dá)到我們的預(yù)期效果,但是如果整個(gè)APP中圖片比較多會(huì)增加我們安裝包的體積,還會(huì)在各種手機(jī)的適配上出現(xiàn)不可預(yù)知的問(wèn)題。

官方團(tuán)隊(duì)提供了一個(gè)可以繪制帶有顏色、漸變、邊框,用于視圖背景的,及設(shè)置各種形狀的類—ShapeElement。

ShapeElement類源碼

ShapeElement類是Element的子類,Element類有多個(gè)擴(kuò)展類,這里就不詳細(xì)說(shuō)明了,后面我們?cè)谛枰臅r(shí)候再擴(kuò)展講解。目前Java API中提供了多個(gè)常量值,兩個(gè)構(gòu)造函數(shù),以及多個(gè)繪圖方法。

我們先來(lái)了解一下能夠設(shè)置哪些形狀?

ShapeElement類提供了設(shè)置矩形、橢圓形、直線、圓弧和路徑(以直線段、二次曲線和三次曲線的幾何組合表示多輪廓路徑)。本節(jié)僅對(duì)前三個(gè)幾何圖形做介紹,后兩個(gè)后面小節(jié)會(huì)詳細(xì)介紹。

  1. /** 
  2.   * 繼承自Element,提供了帶有顏色漸變的可繪制實(shí)例,通常用于視圖背景 
  3.   */ 
  4. public class ShapeElement extends Element { 
  5.     // 可繪制的幾何圖形 
  6.     // 繪制形狀為矩形 
  7.     public static final int RECTANGLE = 0; 
  8.     // 繪制形狀為橢圓 
  9.     public static final int OVAL = 1; 
  10.     // 繪制形狀為直線 
  11.     public static final int LINE = 2; 
  12.      
  13.     //默認(rèn)構(gòu)造器,在代碼中設(shè)定幾何圖形和背景的話,使用這個(gè)構(gòu)造函數(shù) 
  14.     public ShapeElement() {} 
  15.     //引用資源文件中設(shè)定的幾何圖形和背景的話,使用這個(gè)構(gòu)造函數(shù) 
  16.     //xmlId為資源文件的內(nèi)存地址 
  17.     public ShapeElement(Context context, int xmlId) {} 
  18.      
  19.     //設(shè)置要顯示的集合圖形,參數(shù)為上面的靜態(tài)常量 
  20.     public void setShape(int shape) {} 
  21.     //設(shè)置背景顏色 
  22.     public void setRgbColor(RgbColor color) {} 
  23.     //設(shè)置漸變效果填充的顏色值,參數(shù)為顏色數(shù)組,對(duì)直線無(wú)效 
  24.     public void setRgbColors(RgbColor[] colors) {} 
  25.     //設(shè)置邊框的寬度和顏色 
  26.     public void setStroke(int width, RgbColor color) {} 
  27.     //設(shè)置圓角半徑,這個(gè)方法僅對(duì)矩形有效 
  28.     public void setCornerRadius(float radius) {} 
  29.     //設(shè)置每個(gè)角的半徑,這個(gè)方法僅對(duì)矩形有效 
  30.     //表示四個(gè)角的半徑數(shù)組。如果數(shù)組的長(zhǎng)度不等于8,則該設(shè)置無(wú)效。 
  31.     // 每對(duì)半徑表示一個(gè)角的x軸半徑和y軸半徑。 
  32.     public void setCornerRadiiArray(float[] radii) {} 
  33.     //指定組件的漸變效果方向 
  34.     public void setOrientation(ShapeElement.Orientation orientation) {} 
  35.     public void setGradientOrientation(ShapeElement.Orientation orientation) {} 
  36.     //設(shè)置虛線的間隔和相位 
  37.     //數(shù)組項(xiàng)是成對(duì)的:這些值的偶數(shù)和奇數(shù)索引分別指定要繪制的象素個(gè)數(shù)和空白象素個(gè)數(shù)。 
  38.     public void setDashPathEffectValues(float[] intervals, float phase) {} 

 ShapeElement實(shí)例應(yīng)用

上面我們將常用的源碼做了簡(jiǎn)單分析,接下來(lái)我們將以實(shí)操為主,能夠熟練掌握ShapeElement的應(yīng)用。

我們先來(lái)看看上面的UI界面,上面有三種自定義文本顯示,矩形、橢圓形、直線。而OHOS還提供了圓弧和基于路徑的多形狀圖。在矩形中,我們分別實(shí)現(xiàn)了默認(rèn)的矩形背景,四角相同的圓角矩形背景,四角不同的圓角矩形背景,實(shí)線邊框的矩形背景,虛線邊框的矩形背景,橢圓背景,圓形背景,及直線背景。

首先我們先了解一下,我們?yōu)楹问褂眠@種類型的背景。

1、給app瘦身,可以替代純色和漸變色的組件背景

2、編寫(xiě)一個(gè)矩形、圓、橢圓,便于維護(hù),只需要改動(dòng)少量代碼即可實(shí)現(xiàn)效果

3、節(jié)省內(nèi)存

這些設(shè)置背景的圖形和背景色在ShapeElement類中,ShapeElement類是Element類的子類,該類用于實(shí)現(xiàn)帶有漸變效果的圖形化視圖背景。其提供了矩形、橢圓形、直線、圓弧和基于路徑的多形狀圖,本節(jié)我們先來(lái)對(duì)矩形、橢圓形和直線做示例介紹,后兩種我們?cè)诤罄m(xù)開(kāi)專門(mén)的小節(jié)做詳細(xì)介紹。

矩形

矩形是常見(jiàn)的視圖背景,我們可以設(shè)置默認(rèn)的矩形、帶圓角的矩形、帶邊框的矩形。

  1. //矩形背景 
  2. DirectionalLayout rectangle_layout = initChildDirectionalLayout(this); 
  3.  
  4. //這個(gè)組件顯示的效果是"帶背景的矩形" 
  5. Text rectangle_background_text = initText(this, "帶背景的矩形"); 
  6. //設(shè)置背景色 
  7. ShapeElement rectangle_background_text_element = initChildShapeElement(this); 
  8. rectangle_background_text_element.setShape(ShapeElement.RECTANGLE); 
  9. rectangle_background_text.setBackground(rectangle_background_text_element); 
  10. rectangle_layout.addComponent(rectangle_background_text); 
  11.  
  12. //這個(gè)組件顯示的效果是"帶背景和圓角的矩形" 
  13. Text rectangle_background_radius_text = initText(this, "帶背景和圓角的矩形"); 
  14. //設(shè)置背景色和圓角 
  15. ShapeElement rectangle_background_radius_text_element = initChildShapeElement(this); 
  16. rectangle_background_radius_text_element.setShape(ShapeElement.RECTANGLE); 
  17. rectangle_background_radius_text_element.setCornerRadius(20); 
  18. rectangle_background_radius_text_element.setDashPathEffectValues(new float[]{20f, 25f, 30f, 35f}, 2); 
  19. rectangle_background_radius_text.setBackground(rectangle_background_radius_text_element); 
  20. rectangle_layout.addComponent(rectangle_background_radius_text); 
  21.  
  22. //這個(gè)組件顯示的效果是"帶背景和四角不同圓角的矩形" 
  23. Text rectangle_background_different_radius_text = initText(this, "帶背景和不同圓角的矩形"); 
  24. //設(shè)置背景色和每個(gè)角的圓角 
  25. ShapeElement rectangle_background_different_radius_text_element = initChildShapeElement(this); 
  26. rectangle_background_different_radius_text_element.setShape(ShapeElement.RECTANGLE); 
  27. rectangle_background_different_radius_text_element.setCornerRadiiArray(new float[]{20, 40, 30, 60, 20, 20, 100, 120}); 
  28. rectangle_background_different_radius_text.setBackground(rectangle_background_different_radius_text_element); 
  29. rectangle_layout.addComponent(rectangle_background_different_radius_text); 
  30.  
  31.  
  32. //這個(gè)組件顯示的效果是"漸變背景"矩形 
  33. Text gradient_background_text = initText(this, "帶漸變效果的矩形背景"); 
  34. //設(shè)置背景色和漸變(從下端角到上端角) 
  35. ShapeElement gradient_background_text_element = initChildShapeElement(this); 
  36. gradient_background_text_element.setShape(ShapeElement.RECTANGLE); 
  37. RgbColor[] rgbColors = new RgbColor[]{new RgbColor(34, 197, 255), new RgbColor(255, 197, 34)}; 
  38. gradient_background_text_element.setRgbColors(rgbColors); 
  39. gradient_background_text_element.setGradientOrientation(ShapeElement.Orientation.BOTTOM_END_TO_TOP_START); 
  40. gradient_background_text.setBackground(gradient_background_text_element); 
  41. rectangle_layout.addComponent(gradient_background_text); 
  42.  
  43. //這個(gè)組件的效果是"實(shí)線邊框的背景"矩形 
  44. Text stroke_background_text = initText(this, "實(shí)線邊框的背景"); 
  45. //設(shè)置背景色和路徑 
  46. ShapeElement stroke_background_text_element = initChildShapeElement(this); 
  47. stroke_background_text_element.setShape(ShapeElement.RECTANGLE); 
  48. stroke_background_text_element.setStroke(5, new RgbColor(255, 197, 34)); 
  49. stroke_background_text.setBackground(stroke_background_text_element); 
  50. rectangle_layout.addComponent(stroke_background_text); 
  51.  
  52. //這個(gè)組件的效果是"虛線邊框的背景"矩形 
  53. Text stroke_dash_background_text = initText(this, "虛線邊框的背景"); 
  54. //設(shè)置背景色和路徑 
  55. ShapeElement stroke_dash_background_text_element = initChildShapeElement(this); 
  56. stroke_dash_background_text_element.setShape(ShapeElement.RECTANGLE); 
  57. stroke_dash_background_text_element.setStroke(5, new RgbColor(255, 197, 34)); 
  58. stroke_dash_background_text_element.setDashPathEffectValues(new float[]{10, 21, 32, 43, 54, 65}, 1); 
  59. stroke_dash_background_text.setBackground(stroke_dash_background_text_element); 
  60. rectangle_layout.addComponent(stroke_dash_background_text); 

 

OHOS也提供了加載XML資源文件的方法,在graphic文件夾下可以創(chuàng)建xml類型的可繪制資源,如SVG可縮放矢量圖形文件、基本的幾何圖形(如矩形、圓形、線等)shape資源,下面是shape資源XML格式:

  1. <?xml version="1.0" encoding="UTF-8" ?> 
  2. <shape xmlns:ohos="http://schemas.huawei.com/res/ohos" 
  3.        ohos:shape="rectangle"
  4.     <solid 
  5.         ohos:color="#FFFFFF"/> 
  6. </shape> 

 其中ohos:shape是指定幾何圖形,rectangle為矩形樣式。shape的屬性有solid(背景色)、corners表示圓角、gradient表示漸變、stroke表示邊框。

  1. <?xml version="1.0" encoding="UTF-8" ?> 
  2. <shape xmlns:ohos="http://schemas.huawei.com/res/ohos" 
  3.        ohos:shape="rectangle"
  4.     <solid 
  5.         ohos:color="#FF6A5C"/> 
  6.  
  7.     <corners 
  8.         ohos:radius="20"/> 
  9. <!-- 由于缺少相關(guān)文檔,還未摸索出漸變的設(shè)置方式 --> 
  10. <!--    <gradient/>--> 
  11.  
  12.     <stroke 
  13.         ohos:width="2" 
  14.         ohos:color="#000000"/> 
  15. </shape> 

  

橢圓形

橢圓一般我們不常用,但是圓形我們是常用的,比如默認(rèn)頭像,我們使用純色背景來(lái)實(shí)現(xiàn)。

  1. //橢圓形背景 
  2.  
  3. DirectionalLayout oval_layout = initChildDirectionalLayout(this); 
  4.  
  5. //這個(gè)組件顯示的效果是"帶背景的橢圓" 
  6.  
  7. Text oval_background_text = initText(this, "帶背景的橢圓"); 
  8.  
  9. //設(shè)置背景和橢圓 
  10.  
  11. ShapeElement oval_background_text_element = initChildShapeElement(this); 
  12.  
  13. oval_background_text_element.setShape(OVAL); 
  14.  
  15. oval_background_text.setBackground(oval_background_text_element); 
  16.  
  17. oval_layout.addComponent(oval_background_text); 
  18.  
  19. //這個(gè)組件顯示的效果是"帶背景的圓" 
  20.  
  21. Text circular_background_text = initText(this, "帶背景的圓"); 
  22.  
  23. circular_background_text.setWidth(300); 
  24.  
  25. circular_background_text.setHeight(300); 
  26.  
  27. circular_background_text.setTextAlignment(CENTER); 
  28.  
  29. //設(shè)置背景和圓(組件的寬高一樣的時(shí)候是圓) 
  30.  
  31. ShapeElement circular_background_text_element = initChildShapeElement(this); 
  32.  
  33. circular_background_text_element.setShape(OVAL); 
  34.  
  35. circular_background_text.setBackground(circular_background_text_element); 
  36.  
  37. oval_layout.addComponent(circular_background_text); 
  38.  
  39. layout.addComponent(oval_layout); 

 

直線

直線我們只需要了解即可,這個(gè)實(shí)際項(xiàng)目中沒(méi)有多大意義。

  1. //直線 
  2.  
  3. DirectionalLayout line_layout = initChildDirectionalLayout(this); 
  4.  
  5. Text line_background_text = initText(this, "直線"); 
  6.  
  7. ShapeElement line_background_text_element = initChildShapeElement(this); 
  8.  
  9. line_background_text_element.setShape(LINE); 
  10.  
  11. line_background_text.setBackground(line_background_text_element); 
  12.  
  13. line_layout.addComponent(line_background_text); 
  14.  
  15. layout.addComponent(line_layout); 

 

本小節(jié)我們對(duì)ShapeElement類有了一些具體的了解,在后面的組件中將不在詳細(xì)介紹,可以根據(jù)本節(jié)內(nèi)容自定義不同樣式的組件。對(duì)于ShapeElement類中的另外兩個(gè)繪制幾何圖形的方法,我將在后面的小節(jié)中單獨(dú)介紹。

XML文件方式設(shè)置我在文中簡(jiǎn)單的做了介紹,如果直接在布局XML中引用graphic文件下的資源,可以設(shè)置組件的ohos:background_element屬性。

  1. ohos:background_element="$graphic:rectangle_background_radius_text" 

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

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

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

 

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

2020-12-04 12:42:59

組件鴻蒙Text

2020-12-23 11:45:27

鴻蒙HarmonyOSTextField組件

2015-07-22 10:57:36

watchOS圖表自定義

2020-12-09 11:53:24

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

2017-03-13 13:54:40

戴爾

2022-04-24 15:17:56

鴻蒙操作系統(tǒng)

2012-08-23 09:50:07

測(cè)試測(cè)試人員軟件測(cè)試

2009-11-23 12:45:22

2021-11-01 10:21:36

鴻蒙HarmonyOS應(yīng)用

2009-06-24 15:13:36

自定義JSF組件

2023-02-20 15:20:43

啟動(dòng)頁(yè)組件鴻蒙

2015-09-30 09:57:53

天分熱情工程師

2013-12-02 09:43:29

字符串編程

2015-08-20 10:56:19

算法界面開(kāi)發(fā)

2024-02-22 09:00:00

LogitMat數(shù)據(jù)集算法

2013-07-18 09:21:32

代碼文檔

2022-02-15 07:26:34

web前端算法題

2010-11-23 10:55:47

跳槽

2014-01-17 13:09:48

Linux碎片整理

2022-09-14 15:10:40

前端架構(gòu)
點(diǎn)贊
收藏

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