文档开发者中心
我的

View.draw(Canvas canvas)替代方案

在开发过程中,需要把component绘制到其他的canvas上,安卓中是调用View.draw(Canvas)方法,在鸿蒙中component.addDrawTask((component, canvas) -> {});这个方式只是获取到component的Canvas,并没有将改component绘制到其他canvas上的功能。所以在ohos中应该怎么实现我说的上述功能

点赞
1
收藏
1
回复
3
分享
举报
浏览789 发布于2021-06-16 07:54未知归属地
全部评论
最多点赞
最新发布
最早发布

开发者你好,参考

 private Component DrawCircleProgress(int maxRadius, int minRadius, int speed) {

       Component circleProgress = new Component(progressBarSlice.this);

       circleProgress.addDrawTask(new Component.DrawTask() {

           @Override

           public void onDraw(Component component, Canvas canvas) {

               // 旋转一周后重置旋转次数

               if (roateNum == circleNum) {

                   roateNum = 0;

               }

               // 旋转画布canvas

               if (isClockwise) {

                   canvas.rotate(rotateDegrees * roateNum, component.getWidth() / 2, component.getHeight() / 2);

               } else {

                   canvas.rotate(-rotateDegrees * roateNum, component.getWidth() / 2, component.getHeight() / 2);

               }

               roateNum++;

               // 取component最短边,并减去最大圆的半径,得到所有圆所在的圆路径的半径

               int radius = (component.getWidth() > component.getHeight() ? component.getHeight() : component.getWidth()) / 2 - maxRadius;

               // 每个小圆的半径增量

               float radiusIncrement = (float) (maxRadius - minRadius) / circleNum;

               // 每隔多少度绘制一个小圆,弧度制

               double angle = 2 * Math.PI / circleNum;

               // 按位置画小圆,关键地方

               // 每个小圆的位置可以由正弦余弦函数求出,并且每个小圆半径依次递增,若反方向则依次递减

               if (isClockwise) {

                   for (int i = 0; i < circleNum; i++) {

                       float x = (float) (component.getWidth() / 2 + Math.cos(i * angle) * radius);

                       float y = (float) (component.getHeight() / 2 - Math.sin(i * angle) * radius);

                       paint.setAlpha((float) (1 - (float)i / circleNum));

                       canvas.drawCircle(x, y, maxRadius - radiusIncrement * i, paint);

                   }

               } else {

                   for (int i = 0; i < circleNum; i++) {

                       float x = (float) (component.getWidth() / 2 + Math.cos(i * angle) * radius);

                       float y = (float) (component.getHeight() / 2 - Math.sin(i * angle) * radius);

                       paint.setAlpha((float) (1 - (float)i / circleNum));

                       canvas.drawCircle(x, y, minRadius + radiusIncrement * i, paint);

                   }

               }

                   getUITaskDispatcher().delayDispatch(new Runnable() {

                   @Override

                   public void run() {

                       circleProgress.invalidate();

                   }

               }, speed);

           }

       });

       return circleProgress;

   }

展开全部
1
2楼回复于2021-06-21 09:31 未知归属地
  • 150******41
    无用回复!! 就比如我再xml里已经定义了一个Button,现在需要将生成的button对象加到另外一个我的自定义View的Canvas里,安卓可以使用button.draw(Canvas),ohos这边应该怎么处理
    2021-06-22 02:33 未知归属地
欢迎开发小伙伴们进来帮帮楼主
1楼回复于2021-06-16 08:18 未知归属地
写回答
新增插入模板功能
一键使用模板,快速填写内容,轻松发帖~
知道了
  • 为了保障您的信息安全,请勿上传您的敏感个人信息(如您的密码等信息)和您的敏感资产信息(如关键源代码、签名私钥、调试安装包、业务日志等信息),且您需自行承担由此产生的信息泄露等安全风险。
  • 如您发布的内容为转载内容,请注明内容来源。

我要发帖子

了解社区公约,与您携手共创和谐专业的开发者社区。