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

鸿蒙HarmonyOS三方软件开发指南(8)-RoundedImage_0

时间:2023-03-23 11:19:58 科技观察

更多内容请访问:与华为官方共建的鸿蒙技术社区https://harmonyos.51cto.com/#zz1。RoundedImage组件功能介绍1.1.功能介绍:RoundedImage组件可以将图片显示为圆形、椭圆形、圆角矩形。目前仅支持以上三种样式。1.2.在模拟器上运行的效果:2.RoundedImage的使用方法2.1。新建一个项目,添加组件Har包依赖在application模块中添加HAR,复制library-debug.har到entry\libs目录即可(由于libs目录下build*.har已经依赖.gradle,所以不需要修改)。2.2.修改主页面布局文件修改主页面布局文件ability_main.xml,添加com.custom.library.RoundedImage组件,自定义组件的宽高。2.3.修改MainAbilitySlince的UI加载代码在MainAbilitySlince类的onStart函数中。添加如下代码可显示圆角矩阵:@OverridepublicvoidonStart(Intentintent){super.onStart(intent);super.setUIContent(ResourceTable.Layout_ability_main);RoundedImageroundedImage1=(RoundedImage)findComponentById(ResourceTable.Id_image1);roundedImage1.setPixelMapToRoundedMedia.Rect(ResourceTable.Id_image1);roundedImage1.setPixelMapToRoundedMediaRect(,100,50,100,50);RoundedImageroundedImage2=(RoundedImage)findComponentById(ResourceTable.Id_image2);roundedImage2.setPixelMapToRoundedRect(ResourceTable.Media_photo1,100,100,100,100);RoundedImageroundedImage3=(RoundedImage)findComponentById(ResourceTable.Id_image3);roundedImage3.setPixelMapToRoundedRect(ResourceTable.Media_photo2,50,100,50,100);}增加如下代码可显示圆形:@OverridepublicvoidonStart(Intentintent){super.onStart(intent);super.setUIContent(ResourceTable.Layout_ability_main);RoundedImageroundedImage1=(RoundedImage)findComponentById(ResourceTable.Id_image1);roundedImage1.setPixelMapToCircleImage(ResourceTable.Media_photo);RoundedImageroundedImage2=(RoundedImage)findComponentById(ResourceTable.Id_image2);roundedImage2.setPixelMapToCircleImage(ResourceTable.Media_photo1);RoundedImageroundedImage3=(RoundedImage)findComponentById(ResourceTable.Id_image3);roundedImage3.setPixelMapToCircleImage(ResourceTable.Media_photo2);}添加如下代码显示椭圆:@OverridepublicvoidonStart{super.onStart(intent);super.setUIContent(ResourceTable.Layout_ability_main);RoundedImageroundedImage1=(RoundedImage)findComponentById(ResourceTable.Id_image1);roundedImage1.setPixelMapToOvalImage(ResourceTable.Media_photo3);RoundedImageroundedImage2=(RoundedImage)findComponentById)(ResourceroundedImage2.setPixelMapToOvalImage(ResourceTable).Media_photo4);RoundedImageroundedImage3=(RoundedImage)findComponentById(ResourceTable.Id_image3);roundedImage3.setPixelMapToOvalImage(ResourceTable.Media_photo5);}3.RoundedImage开发与实现3.1.模块名称为library3.2.新建RoundedImage类新建RoundedImage类,继承Image类,实现DrawTask.onDraw接口,代码如下:绘制圆:@OverridepublicvoidonDraw(Componentcomponent,Canvascanvas){floatcenterX=getWidth()/2f;floatcenterY=getHeight()/2f;floatradius=Math.min(centerX,centerY);Paintpaint=newPaint();Shadershader=newPixelMapShader(holder,Shader.TileMode.CLAMP_TILEMODE,Shader.TileMode.CLAMP_TILEMODE);paint.setShader(着色器,Paint.ShaderType.SWEEP_SHADER);canvas.drawCircle(centerX,centerY,radius,paint);}用于绘制椭圆:@OverridepublicvoidonDraw(Componentcomponent,Canvascanvas){Paintpaint=newPaint();Shadershader=newPixelMapShader(holder,Shader.TileMode.CLAMP_TILEMODE,Shader.TileMode.CLAMP_TILEMODE);paint.setShader(shader,Paint.ShaderType.SWEEP_SHADER);PixelMappixelMap=holder.getPixelMap();intmin=Math.min(pixelMap.getImageInfo().size.width,pixelMap.getImageInfo().size.height);intradiusX=Math.min(min,minImageLength);floathalfRadiusX=radiusX/2f;floatquarterRadiusX=radiusX/4f;floatleft=getWidth()/2f-halfRadiusX;floatright=getWidth()/2f+halfRadiusX;floattop=getHeight()/2f-quarterRadiusX;floatbottom=getHeight()/2f+quarterRadiusX;RectFloatrect=newRectFloat(左,上,右,下);canvas.drawOval(矩形,paint);}用于设置圆角矩形,调用Image方法设置:setCornerRadii(newfloat[]{topLeft,topLeft,topRigth,topRigth,bottomRight,bottomRight,bottomLeft,bottomLeft});3.3.使用Gradle编译HAR包将HarmonyOSLibrary库模块构建成HAR包。构建HAR包的方法如下:在Gradle构建任务中,双击PackageDebugHar或PackageReleaseHar任务,构建一个Debug类型或Release类型的HAR。>在har目录下获取生成的HAR包。项目源码地址:https://github.com/isoftstone-dev/RoundedImage_HarmonyOS欢迎交流:HWIS-HOS@isoftstone.com?版权所有归作者及HarmonyOS技术社区所有。如需转载请注明出处,否则将追究法律责任。了解更多请访问:与华为官方共建鸿蒙科技社区https://harmonyos.51cto.com/#zz

最新推荐
猜你喜欢