我在鸿蒙上开发了个“微信朋友圈”主页

在实际开发过程中,我们经常会遇到一些系统原有组件无法满足的情况,而 HarmonyOS 提供了自定义组件的方式,我们使用自定义组件来满足项目需求。

在实际开发过程中,我们经常会遇到一些系统原有组件无法满足的情况,而 HarmonyOS 提供了自定义组件的方式,我们使用自定义组件来满足项目需求。

我在鸿蒙上开发了个“微信朋友圈”主页

自定义组件是由开发者定义的具有一定特性的组件,通过扩展 Component 或其子类实现,可以精确控制屏幕元素的外观,实现开发者想要达到的效果,也可响应用户的点击、触摸、长按等操作。

下面通过自定义一个仿微信朋友圈主页的组件来了解一下自定义组件的过程。

关于自定义组件,一般遵循以下几个方式:

首先,创建一个继承 Component 或其子类的自定义组件类,并添加构造方法,如 MyComponent。

实现 Component.EstimateSizeListener 接口,在 onEstimateSize 方法中进行组件测量,并通过 setEstimatedSize 方法通知组件。

自定义组件测量出的大小需通过 setEstimatedSize 通知组件,并且必须返回 true 使测量值生效。

setEstimatedSize 方法的入参携带模式信息,可使用 Component.EstimateSpec.getChildSizeWithMode 方法进行拼接。

测量模式如下图:

我在鸿蒙上开发了个“微信朋友圈”主页

自定义 xml 属性,通过构造方法中携带的参数 attrSet,可以获取到在 xml 中配置的属性值,并应用在该自定义组件中。

实现 Component.DrawTask 接口,在 onDraw 方法中执行绘制任务,该方法提供的画布 Canvas,可以精确控制屏幕元素的外观。在执行绘制任务之前,需要定义画笔 Paint。

实现 Component.TouchEventListener 或其他事件的接口,使组件可响应用户输入。

在 xml 文件中创建并配置自定义组件。

在 HarmonyOS 中 Component 是视图的父类,既然组件都是继承 Component 来实现的,那么我们也可以继承它来实现我们想要的视图了,根据具体流程,我们按照示例代码来了解一下大致流程。

创建自定义布局

...
public class MyComponent extends Component implements Component.DrawTask,Component.EstimateSizeListener {
private Paint paint;
private Paint paintText;

private PixelMap bigImage;

private PixelMap smallImage;

public MyComponent(Context context) {
this(context, null);
}

public MyComponent(Context context, AttrSet attrSet) {
this(context, attrSet,"");
}

public MyComponent(Context context, AttrSet attrSet, String styleName) {
super(context, attrSet, styleName);
init(context);
}

public void init(Context context) {
// 设置测量组件的侦听器
setEstimateSizeListener(this);
// 初始化画笔
initPaint();
// 添加绘制任务
addDrawTask(this);
}

private void initPaint() {
paint = new Paint();
paint.setAntiAlias(true);
paint.setStrokeCap(Paint.StrokeCap.ROUND_CAP);
paint.setStyle(Paint.Style.STROKE_STYLE);

paintText = new Paint();
paintText.setStrokeCap(Paint.StrokeCap.ROUND_CAP);
paintText.setStyle(Paint.Style.FILL_STYLE);
paintText.setColor(Color.WHITE);
paintText.setTextSize(50);
paintText.setAntiAlias(true);

bigImage = PixelMapUtils.createPixelMapByResId(ResourceTable.Media_imageDev, getContext()).get();
smallImage = PixelMapUtils.createPixelMapByResId(ResourceTable.Media_icon, getContext()).get();

}

@Override
public void addDrawTask(Component.DrawTask task) {
super.addDrawTask(task);
task.onDraw(this, mCanvasForTaskOverContent);
}

@Override
public boolean onEstimateSize(int widthEstimateConfig, int heightEstimateConfig) {
int width = Component.EstimateSpec.getSize(widthEstimateConfig);
int height = Component.EstimateSpec.getSize(heightEstimateConfig);
setEstimatedSize(
Component.EstimateSpec.getChildSizeWithMode(width, width, Component.EstimateSpec.NOT_EXCEED),
Component.EstimateSpec.getChildSizeWithMode(height, height, Component.EstimateSpec.NOT_EXCEED));
return true;
}

@Override
public void onDraw(Component component, Canvas canvas) {
int width = getWidth();
int center = width / 2;

float length = (float) (center - Math.sqrt(2) * 1.0f / 2 * center);

// 获取大图片的大小

Size bigImageSize = bigImage.getImageInfo().size;
RectFloat bigImageRect = new RectFloat(0, 0, width, bigImageSize.height);

// 获取小图片的大小
Size smallImageSize = smallImage.getImageInfo().size;
RectFloat smallImageRect = new RectFloat(length * 5, length * 5 - 50, 1100, 1030);

canvas.drawPixelMapHolderRect(new PixelMapHolder(bigImage), bigImageRect, paint);
canvas.drawPixelMapHolderRect(new PixelMapHolder(smallImage), smallImageRect, paint);
canvas.drawText(paintText,"ABCDEFG",width - length * 3.3f, bigImageSize.height - length * 0.2f);

}

}

如上代码,我们创建一个 MyComponent 继承 Component ,并且在构造方法中,初始化一些画笔属性,传入默认的图片,当然也可以通过调用接口的方式在引用的地方传入图片。

然后在 ondraw 方法中做具体的画笔操作。通过 canvas.drawPixelMapHolderRect 方法画出一大一小两张可堆叠的图片,并调整好位置参数。

在 Ability 中引用

实现组件之后,我们就可以在我们需要展示的 Ability 去调用这个自定义组件了。

...
public class ImageAbilitySlice extends AbilitySlice {

@Override
protected void onStart(Intent intent) {
super.onStart(intent);
// super.setUIContent(ResourceTable.Layout_ability_image_main);
drawMyComponent();
}

private void drawMyComponent() {
// 声明布局
ScrollView myLayout = new ScrollView(this);
DirectionalLayout.LayoutConfig config = new DirectionalLayout.LayoutConfig(
DirectionalLayout.LayoutConfig.MATCH_PARENT, DirectionalLayout.LayoutConfig.MATCH_PARENT);
myLayout.setLayoutConfig(config);
myLayout.setReboundEffect(true);
MyComponent customComponent = new MyComponent(this);
myLayout.addComponent(customComponent);
super.setUIContent(myLayout);
}

}

在 XML 文件中引用

<?xml version="1.0" encoding="utf-8"?>
<ScrollView
ohos:id="$+id:dl"
xmlns:ohos="http://schemas.huawei.com/res/ohos"
ohos:height="match_parent"
ohos:width="match_parent"
ohos:alignment="center"
ohos:rebound_effect="true"
ohos:orientation="vertical">

<com.example.harmonyosdeveloperchenpan.MyComponent
ohos:id="$+id:myComponent"
ohos:height="match_parent"
ohos:width="match_parent"/>

</ScrollView>

需要注意的是,微信朋友圈主页的滑动有下拉回弹效果,所以我们需要设置 ScrollView 的布局属性。

通过在代码中调用 setReboundEffect(true) 或者 xml 中设置 ohos:rebound_effect=“true” 来实现。

效果展示如下图:

我在鸿蒙上开发了个“微信朋友圈”主页

作者:陈潘

内容来源网络,如有侵权,联系删除,本文地址:https://www.230890.com/zhan/74649.html

(0)

相关推荐

  • 儿童铅中毒的表现,儿童铅中毒怎么治疗

    上海新华医院儿科的重金属中毒门诊

    生活 2021年10月28日
  • 牢记夏季是治骨头的关键季节,3招教你治疗关节疾病

    在我国的南方地区,高温潮湿的环境会让人很不舒服。长时间待在空调房里,不会出汗,人体内部的热散不出来,空调带来的凉气钻到体内,侵犯关节,就容易出现颈肩腰腿关节痛。治疗肩颈腰腿痛要趁热,夏季是治骨的关键。

    生活 2021年10月14日
  • 人民币是国际货币吗?为什么?,货币是人民币吗

    可能一些关注国际新闻的人时不时就会听到这样的消息:某某国家(比如俄罗斯、土耳其、伊朗等新兴经济体国家)纷纷抛弃美元、增持人民币。2021年3月31日国际货币基金组织(IMF)发布的“官方外汇储备货币构成”数据显示:2020年四季度人民币外汇储备总额由去年三季度的2455.5亿美元升至2675.2亿美元。如今人民币在全球外汇储备中的占比已升至2.25%,再次刷新2016年第四季度IMF报告该数据以来的新高。

    生活 2021年11月7日
  • 孕妇生活日记(11):节俭的准妈妈

    艾玛吃上早饭了。从昨晚半夜就饿死了,睡前还啃了两块巧克力来着,不知道怎么这么饿麻球买多了,吃完这俩还有仨。真的好好吃,如果不控制着,一袋也就一个小时的量。边吃边看沈阳的早市,我可太羡慕在沈阳的小伙伴,早市好诱人好多好吃的,而且价格老实惠了。除了想去许昌的胖东来,我还想去沈阳逛早市~早上面条也吼吼吃,可以干掉两碗~

    生活 2021年12月13日
  • 周五A股变盘或一路飙涨,本周股票利好消息

    本周(11.15-19日),A股市场在震荡中走强,特别是周五,我们看到了几大指数集体中阳,另外,我们也看到了本周的结构性偏好和分歧,随着时间的推移,低估值和高景气度的各自波动会越加明显,存量博弈背景下,重结构轻指数或是唯一标准……

    生活 2021年11月21日
  • 中秋前后,最适合肌瘤患者的三大食疗方

    中秋节,又称月亮节,是中国四大传统节日之一,这个节日到来后,意味着天气渐凉,秋味更浓。对于肌瘤患者来说,中秋前后,正是肌瘤患者调养的最佳时期。今天,加医生给大家推荐几个食疗方,经常吃一吃,可以帮助调养身体。

    生活 2021年9月20日