当前位置: 首页 > 科技观察

HarmonyOS自定义组件模仿微信朋友圈首页

时间:2023-03-12 14:02:32 科技观察

更多内容请访问:Harmonyos技术社区https://harmonyos.51cto.com系统不能满足,HarmonyOS提供了自定义组件的方式。我们使用自定义组件来满足项目要求。自定义组件是具有开发人员定义的某些特征的组件。通过扩展Component或其子类,可以精确控制屏幕元素的外观,实现开发者想要实现的效果,响应用户的点击、触摸、长按等操作。下面我们通过自定义一个仿微信朋友圈首页的组件来了解一下自定义组件的过程。简介首先,关于自定义组件,一般遵循以下方法首先,创建一个继承Component或其子类的自定义组件类,并添加一个构造函数,比如MyComponent。实现Component.EstimateSizeListener接口,在onEstimateSize方法中进行组件测量,通过setEstimateSize方法通知组件。自定义组件测量的尺寸需要通过setEstimatedSize通知组件,并且必须返回true才能使测量值生效。setEstimatedSize方法的入参携带模式信息,可以使用Component.EstimateSpec.getChildSizeWithMode方法进行拼接。测量模式自定义xml属性。通过构造方法中携带的参数attrSet,可以获取到xml中配置的属性值,并应用到自定义组件中。实现Component.DrawTask接口,在onDraw方法中执行绘图任务。该方法提供的Canvas可以精确控制屏幕元素的外观。在执行绘图任务之前,需要定义画笔Paint。实现Component.TouchEventListener或其他事件接口,使组件响应用户输入。在xml文件中创建和配置自定义组件。在HarmonyOS中,Component是view的父类。由于组件是通过继承Component来实现的,所以我们也可以继承它来实现我们想要的视图。根据具体流程,我们跟着示例代码了解一下大致流程:创建CustomLayout...;}publicMyComponent(Setthiscontext,AttrSetattr"{");}publicMyComponent(Contextcontext,AttrSetattrSet,StringstyleName){super(context,attrSet,styleName);init(context);}publicvoidinit(Contextcontext){//设置监听器测量组件setEstimateSizeListener(this);//初始化画笔initPaint();//添加绘图任务addDrawTask(this);}privatevoidinitPaint(){paint=newPaint();paint.setAntiAlias(true);paint.setStrokeCap(Paint.StrokeCap.ROUND_CAP);paint.setStyle(Paint.Style.STROKE_STYLE);paintText=newPaint();paintText.setStrokeCap(Paint.StrokeCap.ROUND_CAP);paintText.setStyle(Paint.Style.FILL_STYLE);paintText.setColor(C醇or.WHITE);paintText.setTextSize(50);paintText.setAntiAlias(true);bigImage=PixelMapUtils.createPixelMapByResId(ResourceTable.Media_imageDev,getContext()).get();smallImage=PixelMapUtils.createPixelMapByResId(ResourceTable.Media_icon,getContext()).get();}@OverridepublicvoidaddDrawTask(Component.DrawTasktask){super.addDrawTask(task);task.onDraw(this,mCanvasForTaskOverContent);}@OverridepublicbooleanonEstimateSize(intwidthEstimateConfig,intheightEstimateConfig){intwidth=Component.EstimateSpec.getSize(widthEstimateConfig));intheight=Component.EstimateSpec.getSize(heightEstimateConfig);setEstimatedSize(Component.EstimateSpec.getChildSizeWithMode(width,width,Component.EstimateSpec.NOT_EXCEED),Component.EstimateSpec.getChildSizeWithMode(height,height,Component.EstimateSpec.NOT_EXCEED));returntrue;}@OverridepublicvoidonDraw(Componentcomponent,Canvascanvas){intwidth=getWidth();intcenter=width/2;floatlength=(float)(center-Math.sqrt(2)*1.0f/2*center);//获取大图片的大SmallSizebigImageSize=bigImage.getImageInfo().size;RectFloatbigImageRect=newRectFloat(0,0,width,bigImageSize.height);//获取小图的大小SizesmallImageSize=smallImage.getImageInfo().size;RectFloatsmallImageRect=newRectFloat(length*5,length*5-50,1100,1030);canvas.drawPixelMapHolderRect(newPixelMapHolder(bigImage),bigImageRect,paint);canvas.drawPixelMapHolderRect(newPixelMapHolder(smallImage),smallImageRect,paint);canvas.drawText(paintText,"ABCDEFG",width-length*3.3f,bigImageSize.height-length*0.2f);}}如上代码,我们创建一个MyComponent继承Component,在构造方法中,初始化一些画笔属性,传入默认图片,当然,也可以通过调用接口的方法,在引用的地方传入图片,然后在ondraw方法中进行具体的画笔操作。使用canvas.drawPixelMapHolderRect方法绘制一大一小两张可堆叠图片,并调整position参数。在Ability中引用实现组件后,我们就可以在我们需要展示的Ability中调用这个自定义组件了。...publicclassImageAbilitySliceextendsAbilitySlice{@OverrideprotectedvoidonStart(Intentintent){super.onStart(intent);//super.setUIContent(ResourceTable.Layout_ability_image_main);drawMyComponent();}privatevoiddrawMyComponent(){//声明布局ScrollViewmyLayout=newScrollView(this);DirectionalLayout.LayoutConfigconfig=newDirectionalLayout.LayoutConfig(DirectionalLayout.LayoutConfig.MATCH_PARENT,DirectionalLayout.LayoutConfig.MATCH_PARENT);myLayout.setLayoutConfig(config);myLayout.setReboundEffect(true);MyComponentcustomComponent=newMyComponent(this);myLayout.addComponent(customComponent);超级。setUIContent(myLayout);}}在XML文件中引用需要注意的是微信首页MomentsSliding有下拉回弹效果,所以我们需要在代码中调用setReboundEffect(true)或者在xml中设置ohos:rebound_effect="true"来设置ScrollView的布局属性。效果展示更多内容请访问:与华为官方共建的Harmonyos技术社区https://harmonyos.51cto.com