150******41View.draw(Canvas canvas)替代方案
在开发过程中,需要把component绘制到其他的canvas上,安卓中是调用View.draw(Canvas)方法,在鸿蒙中component.addDrawTask((component, canvas) -> {});这个方式只是获取到component的Canvas,并没有将改component绘制到其他canvas上的功能。所以在ohos中应该怎么实现我说的上述功能
eva3w开发者你好,参考
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;
}
150******41无用回复!! 就比如我再xml里已经定义了一个Button,现在需要将生成的button对象加到另外一个我的自定义View的Canvas里,安卓可以使用button.draw(Canvas),ohos这边应该怎么处理2021-06-22 02:33 未知归属地
五维空间幻影猫- 为了保障您的信息安全,请勿上传您的敏感个人信息(如您的密码等信息)和您的敏感资产信息(如关键源代码、签名私钥、调试安装包、业务日志等信息),且您需自行承担由此产生的信息泄露等安全风险。
- 如您发布的内容为转载内容,请注明内容来源。
我要发帖子
HarmonyOS 以前用AVLoadingIndicatorView做的加载样式,现在应该如何实现?(API12+)
HarmonyOS 【RN】在Animated.View的style属性设置zIndex会报错(API12+)
HarmonyOS 应用APMS实现方案 (API12+)
HarmonyOS 组件view可以转换成image吗? (API12+)
如何在HarmonyOS中为自定义View进行手势监听?(API12+)
HarmonyOS 是否有ListItemGroup使用三元运算符加载View的示例?(API12+)
HarmonyOS 如何把图片view暴露出来 (API12+)
HarmonyOS canvas.drawTextBlob无法绘制emoji,如何处理?(API12)




































苏公安网备 32011402010933号