当前位置:   article > 正文

尝试HarmonyOS代码_vectorelement详解 harmonyos

vectorelement详解 harmonyos

 

 

 从官网下载好例子。如下。但是它还需要更新一些依赖。让它更新,更新后了如下

看到成功了,说明代码没有问题。但是,点击

却报错。而且打不开模拟器。

这是你没有实名认证。你要去华为去实名认证。自己玩的话,申请个人就好了。准备好,姓名,身份证号,银行卡号,手机号。然后点击这个

可以模拟的设备就出来了

用哪个Actions 那个。一次只能用1个小时,时间到了,在申请。

我用的是 一个手表的例子,做了一丁点修改。要不会报错的

  1. import ohos.aafwk.ability.Ability;
  2. import ohos.aafwk.content.Intent;
  3. public class PageAbility extends Ability {
  4. @Override
  5. public void onStart(Intent intent) {
  6. super.onStart(intent);
  7. super.setMainRoute(SleepPageSlice.class.getName());
  8. setSwipeToDismiss(true);
  9. }
  10. }
  1. import ohos.aafwk.ability.AbilitySlice;
  2. import ohos.aafwk.content.Intent;
  3. import ohos.agp.components.*;
  4. import ohos.hiviewdfx.HiLog;
  5. import ohos.hiviewdfx.HiLogLabel;
  6. import java.util.ArrayList;
  7. import java.util.List;
  8. import java.util.Optional;
  9. public class SleepPageSlice extends AbilitySlice {
  10. private static final String TAG = "SleepPageSlice";
  11. private static final HiLogLabel LABEL = new HiLogLabel(HiLog.LOG_APP, 0, TAG);
  12. private List<ComponentOwner> list = new ArrayList<>();
  13. private PageSliderProvider provider = new PageSliderProvider() {
  14. @Override
  15. public int getCount() {
  16. return list.size();
  17. }
  18. @Override
  19. public Object createPageInContainer(ComponentContainer componentContainer, int index) {
  20. if (index >= list.size() || componentContainer == null) {
  21. HiLog.error(LABEL, "instantiateItem index error");
  22. return Optional.empty();
  23. }
  24. ComponentOwner container = list.get(index);
  25. componentContainer.addComponent(container.getComponent());
  26. container.instantiateComponent();
  27. return container.getComponent();
  28. }
  29. @Override
  30. public void destroyPageFromContainer(ComponentContainer componentContainer, int index, Object object) {
  31. HiLog.info(LABEL, "destroyItem index:" + index);
  32. if (index >= list.size() || componentContainer == null) {
  33. return;
  34. }
  35. Component content = list.get(index).getComponent();
  36. componentContainer.removeComponent(content);
  37. return;
  38. }
  39. @Override
  40. public boolean isPageMatchToObject(Component component, Object object) {
  41. return component == object;
  42. }
  43. @Override
  44. public void startUpdate(ComponentContainer container) {
  45. super.startUpdate(container);
  46. HiLog.info(LABEL, "startUpdate");
  47. }
  48. };
  49. @Override
  50. public void onStart(Intent intent) {
  51. super.onStart(intent);
  52. HiLog.info(LABEL, "onStart");
  53. // 添加子页面
  54. list.add(new SleepComponentOwner(this));
  55. list.add(new DetailComponentOwner(this));
  56. // 设置主界面
  57. DirectionalLayout layout = new DirectionalLayout(this);
  58. ComponentContainer.LayoutConfig config = new ComponentContainer.LayoutConfig(
  59. ComponentContainer.LayoutConfig.MATCH_PARENT,
  60. ComponentContainer.LayoutConfig.MATCH_PARENT);
  61. layout.setLayoutConfig(config);
  62. // 使用PageSlider做滑动效果
  63. PageSlider slider = new PageSlider(this);
  64. ComponentContainer.LayoutConfig sliderConfig = new ComponentContainer.LayoutConfig(
  65. ComponentContainer.LayoutConfig.MATCH_PARENT,
  66. ComponentContainer.LayoutConfig.MATCH_PARENT);
  67. slider.setLayoutConfig(sliderConfig);
  68. slider.setProvider(provider);
  69. layout.addComponent(slider);
  70. setUIContent(layout);
  71. }
  72. }
  1. import com.huawei.mywearable.ResourceTable;
  2. import ohos.agp.components.Component;
  3. import ohos.agp.components.LayoutScatter;
  4. import ohos.agp.components.element.VectorElement;
  5. import ohos.app.AbilityContext;
  6. import ohos.hiviewdfx.HiLog;
  7. import ohos.hiviewdfx.HiLogLabel;
  8. public class SleepComponentOwner implements ComponentOwner {
  9. private static final String TAG = "SleepComponentOwner";
  10. private static final HiLogLabel LABEL = new HiLogLabel(HiLog.LOG_APP, 0, TAG);
  11. // 目标睡眠时长默认值,单位:分钟
  12. private static final int DEFAULT_SLEEP_TARGET_TIME = 480;
  13. // 睡眠时长默认值,单位:分钟
  14. private static final int DEFAULT_SLEEP_TIME = 0;
  15. private CircleProgressDrawTask drawTask;
  16. private AbilityContext myContext;
  17. private Component root;
  18. public SleepComponentOwner(AbilityContext context) {
  19. init(context);
  20. }
  21. private void init(AbilityContext context) {
  22. myContext = context;
  23. LayoutScatter scatter = LayoutScatter.getInstance(context);
  24. root = scatter.parse(ResourceTable.Layout_layout_sleep, null, false);
  25. drawTask = new CircleProgressDrawTask(root);
  26. drawTask.setMaxValue(DEFAULT_SLEEP_TARGET_TIME);
  27. Component imageComponent = root.findComponentById(ResourceTable.Id_sleep_moon_img);
  28. imageComponent.setBackground(new VectorElement(context, ResourceTable.Id_sleep_moon_img));
  29. }
  30. @Override
  31. public Component getComponent() {
  32. return root;
  33. }
  34. @Override
  35. public void instantiateComponent() {
  36. return;
  37. }
  38. }

 

  1. import ohos.agp.components.Component;
  2. import ohos.agp.render.Canvas;
  3. import ohos.agp.utils.Color;
  4. public class CircleProgressDrawTask implements Component.DrawTask {
  5. // 用于配置圆环的粗细,具体参数可以在xml文件中配置
  6. private static final String STROKE_WIDTH_KEY = "stroke_width";
  7. // 用于配置圆环的最大值,具体参数可以在xml文件中配置
  8. private static final String MAX_PROGRESS_KEY = "max_progress";
  9. // 用于配置圆环的当前值,具体参数可以在xml文件中配置
  10. private static final String CURRENT_PROGRESS_KEY = "current_progress";
  11. // 用于配置起始位置的颜色,具体参数可以在xml文件中配置
  12. private static final String START_COLOR_KEY = "start_color";
  13. // 用于配置结束位置的颜色,具体参数可以在xml文件中配置
  14. private static final String END_COLOR_KEY = "end_color";
  15. // 用于配置背景色,具体参数可以在xml文件中配置
  16. private static final String BACKGROUND_COLOR_KEY = "background_color";
  17. // 用于配置起始位置的角度,具体参数可以在xml文件中配置
  18. private static final String START_ANGLE = "start_angle";
  19. private static final float MAX_ARC = 360f;
  20. private static final int DEFAULT_STROKE_WIDTH = 10;
  21. private static final int DEFAULT_MAX_VALUE = 100;
  22. private static final int DEFAULT_START_COLOR = 0xFFB566FF;
  23. private static final int DEFAULT_END_COLOR = 0xFF8A2BE2;
  24. private static final int DEFAULT_BACKGROUND_COLOR = 0xA8FFFFFF;
  25. private static final int DEFAULT_START_ANGLE = -90;
  26. private static final float DEFAULT_LINER_MAX = 100f;
  27. private static final int HALF = 2;
  28. // 圆环的宽度, 默认10个像素
  29. private int myStrokeWidth = DEFAULT_STROKE_WIDTH;
  30. // 最大的进度值, 默认是100
  31. private int myMaxValue = DEFAULT_MAX_VALUE;
  32. // 当前的进度值, 默认是0
  33. private int myCurrentValue = 0;
  34. // 起始位置的颜色, 默认浅紫色
  35. private Color myStartColor = new Color(DEFAULT_START_COLOR);
  36. // 结束位置的颜色, 默认深紫色
  37. private Color myEndColor = new Color(DEFAULT_END_COLOR);
  38. // 背景颜色, 默认浅灰色
  39. private Color myBackgroundColor = new Color(DEFAULT_BACKGROUND_COLOR);
  40. // 当前的进度值, 默认从-90度进行绘制
  41. private int myStartAngle = DEFAULT_START_ANGLE;
  42. private Component myComponent;
  43. // 传入要进行修改的component
  44. public CircleProgressDrawTask(Component component) {
  45. myComponent = component;
  46. myComponent.addDrawTask(this);
  47. }
  48. // 设置当前进度并且刷新component,value值为当前进度
  49. public void setValue(int value) {
  50. myCurrentValue = value;
  51. myComponent.invalidate();
  52. }
  53. public void setMaxValue(int maxValue) {
  54. myMaxValue = maxValue;
  55. myComponent.invalidate();
  56. }
  57. @Override
  58. public void onDraw(Component component, Canvas canvas) {
  59. // 通过canvas实现绘制圆环的功能
  60. }
  61. }

 

  1. import ohos.agp.components.Component;
  2. public interface ComponentOwner {
  3. // 获取存放的component
  4. Component getComponent();
  5. // 当包含的component被添加到容器时回调
  6. void instantiateComponent();
  7. }
  1. import ohos.agp.components.Component;
  2. import ohos.agp.components.ComponentContainer;
  3. import ohos.agp.components.DirectionalLayout;
  4. import ohos.app.AbilityContext;
  5. import ohos.hiviewdfx.HiLog;
  6. import ohos.hiviewdfx.HiLogLabel;
  7. public class DetailComponentOwner implements ComponentOwner {
  8. private static final String TAG = "DetailComponentOwner";
  9. private static final HiLogLabel LABEL = new HiLogLabel(HiLog.LOG_APP, 0, TAG);
  10. private AbilityContext myContext;
  11. private ComponentContainer root;
  12. public DetailComponentOwner(AbilityContext context) {
  13. init(context);
  14. }
  15. private void init(AbilityContext context) {
  16. root = new DirectionalLayout(context);
  17. ComponentContainer.LayoutConfig config = new ComponentContainer.LayoutConfig(
  18. ComponentContainer.LayoutConfig.MATCH_PARENT,
  19. ComponentContainer.LayoutConfig.MATCH_PARENT);
  20. root.setLayoutConfig(config);
  21. myContext = context;
  22. }
  23. @Override
  24. public Component getComponent() {
  25. return root;
  26. }
  27. @Override
  28. public void instantiateComponent() {
  29. return;
  30. }
  31. }

 

还有 两个布局

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <DirectionalLayout xmlns:ohos="http://schemas.huawei.com/res/ohos"
  3. ohos:width="match_parent"
  4. ohos:height="match_parent"
  5. ohos:orientation="vertical"
  6. ohos:background_element="#FF000000">
  7. <Text
  8. ohos:id="$+id:detail_nodata_date"
  9. ohos:width="match_content"
  10. ohos:height="23vp"
  11. ohos:top_margin="20vp"
  12. ohos:layout_alignment="horizontal_center"
  13. ohos:text_alignment="bottom"
  14. ohos:text_color="$color:sleep_text_lvse"
  15. ohos:text_weight="600"
  16. ohos:text_size="19vp"/>
  17. <Image
  18. ohos:id="$+id:detail_nodata_img"
  19. ohos:width="46vp"
  20. ohos:height="46vp"
  21. ohos:top_margin="25vp"
  22. ohos:layout_alignment="horizontal_center"
  23. ohos:scale_mode="zoom_center"/>
  24. <Text
  25. ohos:width="match_content"
  26. ohos:height="match_content"
  27. ohos:alpha="0.66"
  28. ohos:top_margin="12vp"
  29. ohos:layout_alignment="horizontal_center"
  30. ohos:text_alignment="center"
  31. ohos:text="$string:no_data"
  32. ohos:text_color="$color:sleep_text_lvse"
  33. ohos:text_size="16vp"/>
  34. <Text
  35. ohos:width="match_content"
  36. ohos:height="match_content"
  37. ohos:alpha="0.66"
  38. ohos:layout_alignment="horizontal_center"
  39. ohos:text_alignment="center"
  40. ohos:text="$string:wearing_watch_tips"
  41. ohos:text_color="$color:sleep_text_lvse"
  42. ohos:text_size="16vp"/>
  43. </DirectionalLayout>

 

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <DirectionalLayout xmlns:ohos="http://schemas.huawei.com/res/ohos"
  3. ohos:width="match_parent"
  4. ohos:height="match_parent"
  5. ohos:background_element="#FF000000"
  6. ohos:orientation="vertical">
  7. <Image
  8. ohos:id="$+id:sleep_moon_img"
  9. ohos:width="46vp"
  10. ohos:height="46vp"
  11. ohos:top_margin="11vp"
  12. ohos:layout_alignment="horizontal_center"/>
  13. <Text
  14. ohos:width="match_parent"
  15. ohos:height="19.5vp"
  16. ohos:alpha="0.66"
  17. ohos:layout_alignment="horizontal_center"
  18. ohos:text_alignment="center"
  19. ohos:text="$string:sleep"
  20. ohos:text_color="$color:sleep_text_lvse"
  21. ohos:text_size="16vp"/>
  22. <DirectionalLayout xmlns:ohos="http://schemas.huawei.com/res/ohos"
  23. ohos:width="match_content"
  24. ohos:height="65vp"
  25. ohos:top_margin="8vp"
  26. ohos:layout_alignment="horizontal_center"
  27. ohos:orientation="horizontal">
  28. <Text
  29. ohos:id="$+id:sleep_hour_text"
  30. ohos:width="match_content"
  31. ohos:height="match_content"
  32. ohos:layout_alignment="center"
  33. ohos:text_alignment="center"
  34. ohos:text="$string:dash"
  35. ohos:text_color="$color:sleep_text_lvse"
  36. ohos:text_size="58vp"
  37. />
  38. <Text
  39. ohos:width="match_content"
  40. ohos:height="match_content"
  41. ohos:left_margin="2vp"
  42. ohos:alpha="0.66"
  43. ohos:layout_alignment="bottom"
  44. ohos:bottom_padding="9.5vp"
  45. ohos:text="$string:hour"
  46. ohos:text_color="$color:sleep_text_lvse"
  47. ohos:text_size="16vp"
  48. />
  49. <Text
  50. ohos:id="$+id:sleep_min_text"
  51. ohos:width="match_content"
  52. ohos:height="match_content"
  53. ohos:left_margin="2vp"
  54. ohos:layout_alignment="center"
  55. ohos:text_alignment="center"
  56. ohos:text="$string:double_dash"
  57. ohos:text_color="$color:sleep_text_lvse"
  58. ohos:text_size="58vp"
  59. />
  60. <Text
  61. ohos:width="match_content"
  62. ohos:height="match_content"
  63. ohos:left_margin="2vp"
  64. ohos:alpha="0.66"
  65. ohos:layout_alignment="bottom"
  66. ohos:bottom_padding="9.5vp"
  67. ohos:text="$string:minute"
  68. ohos:text_color="$color:sleep_text_lvse"
  69. ohos:text_size="16vp"
  70. />
  71. </DirectionalLayout>
  72. <DirectionalLayout xmlns:ohos="http://schemas.huawei.com/res/ohos"
  73. ohos:width="match_content"
  74. ohos:height="25vp"
  75. ohos:top_margin="20.5vp"
  76. ohos:layout_alignment="horizontal_center"
  77. ohos:orientation="horizontal">
  78. <Text
  79. ohos:width="match_content"
  80. ohos:height="19.5vp"
  81. ohos:text="$string:goal"
  82. ohos:alpha="0.66"
  83. ohos:text_alignment="bottom"
  84. ohos:bottom_margin="1vp"
  85. ohos:text_color="$color:sleep_text_lvse"
  86. ohos:text_size="16vp"
  87. />
  88. <Text
  89. ohos:id="$+id:sleep_goal_text"
  90. ohos:width="match_content"
  91. ohos:height="match_parent"
  92. ohos:left_margin="2vp"
  93. ohos:text="$string:target_sleep_time"
  94. ohos:text_weight="600"
  95. ohos:text_color="$color:sleep_text_lvse"
  96. ohos:bottom_padding="2vp"
  97. ohos:text_size="21vp"
  98. />
  99. <Text
  100. ohos:width="match_content"
  101. ohos:height="19.5vp"
  102. ohos:left_margin="2vp"
  103. ohos:alpha="0.66"
  104. ohos:text="$string:hour"
  105. ohos:text_color="$color:sleep_text_lvse"
  106. ohos:text_size="16vp"
  107. />
  108. </DirectionalLayout>
  109. </DirectionalLayout>

 

枚举

  1. {
  2. "string":[
  3. {
  4. "name":"app_name",
  5. "value":"MyWearable"
  6. },
  7. {
  8. "name":"mainability_description",
  9. "value":"Java_Wearable_Empty Feature Ability"
  10. },
  11. {
  12. "name":"HelloWorld",
  13. "value":"Hello World"
  14. },
  15. {
  16. "name":"sleep",
  17. "value":"sleep liunn"
  18. },
  19. {
  20. "name":"dash",
  21. "value":"dash liunn"
  22. },
  23. {
  24. "name":"hour",
  25. "value":"hour liunn"
  26. },
  27. {
  28. "name":"double_dash",
  29. "value":"double_dash liunn"
  30. },
  31. {
  32. "name":"minute",
  33. "value":"minute liunn"
  34. },
  35. {
  36. "name":"goal",
  37. "value":"goal liunn"
  38. },
  39. {
  40. "name":"target_sleep_time",
  41. "value":"target_sleep_time liunn"
  42. },
  43. {
  44. "name":"no_data",
  45. "value":"no_data liunn"
  46. },
  47. {
  48. "name":"wearing_watch_tips",
  49. "value":"wearing_watch_tips liunn"
  50. }
  51. ]
  52. }
  1. {
  2. "color":[
  3. {
  4. "name":"sleep_text_lvse",
  5. "value":"#FF00FFD9"
  6. },
  7. {
  8. "name":"mainability_description",
  9. "value":"Java_Wearable_Empty Feature Ability"
  10. },
  11. {
  12. "name":"HelloWorld",
  13. "value":"Hello World"
  14. },
  15. {
  16. "name":"sleep",
  17. "value":"sleep liunn"
  18. }
  19. ]
  20. }

整个 目录结构是这个样子的,

注册一个 

  1. "abilities": [
  2. {
  3. "name": "com.huawei.mywearable.slice.liuTest.PageAbility",
  4. "icon": "$media:icon",
  5. "description": "$string:mainability_description",
  6. "label": "$string:app_name",
  7. "launchType": "standard",
  8. "orientation": "portrait",
  9. "visible": true,
  10. "permissions": [],
  11. "skills": [
  12. {
  13. "actions": [
  14. "action.system.home"
  15. ],
  16. "entities": [
  17. "entity.system.home"
  18. ]
  19. }
  20. ],
  21. "type": "page",
  22. "formEnabled": false
  23. }
  24. ]

模拟器运行一下。 

 

好了,没有报错。

代码参考:  https://developer.harmonyos.com/cn/docs/documentation/doc-guides/wearable-adding-module-0000001053581601

写的有点乱。等熟悉了,整理一下。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/菜鸟追梦旅行/article/detail/243829
推荐阅读
相关标签
  

闽ICP备14008679号