HarmonyOS鸿蒙Next中android-ViewOutlineProvider替换api是什么

HarmonyOS鸿蒙Next中android-ViewOutlineProvider替换api是什么 想裁剪某个view,看了下鸿蒙clipShape好像符合,但是不太会用 提供一个简单场景吧

Row() {
    ForEach([200个0.5], (value: number, index) => {
        Text()
            .width(value + "%")
            .height("100%")
            .backgroundColor("red");
    });
}
.height(14)
.width("100%")
.borderRadius(7);

这样绘制出来是一个组合的进度条,我想给最左边最右边加一个radius,最终效果是附件里面的


更多关于HarmonyOS鸿蒙Next中android-ViewOutlineProvider替换api是什么的实战教程也可以访问 https://www.itying.com/category-93-b0.html

3 回复

“这样也是可以的 interface Slide { width: string color: string } @Entry @Component struct Index { @State backColor: Slide[] = [ { width: “30”, color: “#ffdd1575” }, { width: “20”, color: “#ffa4ef0b” }, { width: “50”, color: “#ff11c4e2” } ] build() { Column() { Row() { ForEach(this.backColor, (item: Slide, index) => { Text() .width(item.width + ‘%’) .height(“100%”) .backgroundColor(item.color) .borderRadius({ bottomLeft: index === 0 ? 10 : “”, topLeft: index === 0 ? 10 : “”, topRight: index === (this.backColor.length-1)? 10 : “”, bottomRight: index === (this.backColor.length-1) ? 10 : “” }) }) } .width(‘100%’) .height(100) .backgroundColor(”#ffea0c0c") .borderRadius(10) } .padding(20) } "

更多关于HarmonyOS鸿蒙Next中android-ViewOutlineProvider替换api是什么的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html


在HarmonyOS鸿蒙Next中,android-ViewOutlineProvider的替换API是ohos.agp.components.ViewOutlineProviderViewOutlineProvider用于定义视图的轮廓,通常用于实现自定义的视图裁剪或阴影效果。在鸿蒙系统中,ohos.agp.components.ViewOutlineProvider提供了类似的功能,允许开发者定义视图的轮廓形状。

ohos.agp.components.ViewOutlineProvider的使用方式与Android中的ViewOutlineProvider类似。开发者需要继承ViewOutlineProvider类,并重写getOutline方法,在该方法中定义视图的轮廓。然后通过Component.setOutlineProvider方法将自定义的ViewOutlineProvider设置给对应的组件。

例如,以下代码展示了如何在鸿蒙系统中使用ViewOutlineProvider

import ohos.agp.components.Component;
import ohos.agp.components.ViewOutlineProvider;
import ohos.agp.utils.Rect;

class CustomOutlineProvider extends ViewOutlineProvider {
    override getOutline(component: Component, outline: Rect): void {
        outline.setRoundRect(0, 0, component.width, component.height, 20);
    }
}

// 设置给组件
let component: Component = ...; // 获取组件实例
component.setOutlineProvider(new CustomOutlineProvider());

在HarmonyOS鸿蒙Next中,为了替代Android中的ViewOutlineProvider,可以使用UIViewsetClipPath方法来实现类似的功能。通过自定义UIView并重写其onDraw方法,使用Path对象定义裁剪路径,然后调用setClipPath方法应用裁剪效果。这种方式可以实现与ViewOutlineProvider类似的视图轮廓控制,同时适配鸿蒙系统的UI框架。

回到顶部