ArkTS通用属性

news2024/9/23 13:21:03

目录

一、尺寸设置

宽高,外边距,内边距,尺寸size

layoutWeight

constraintSize

二、位置设置

align

direction

position

offset

使用Edge方式position,offset

三、布局约束

aspectRatio

displayPriority

四、Flex布局

flexBasis

flexGrow

flexShrink

alignSelf

五、边框

border

六、背景设置

backgroundColor

backgroundImage

backgroundImageSize

backgroundImagePosition

七、透明度设置

opacity

八、显隐控制

visibility

九、禁用控制

enabled

十、浮层

overlay

 十一、Z序控制

zIndex

十二、图形变换

十三、图像效果

blur

backdropBlur

shadow

grayscale

brightness

saturate

contrast

invert

sepia

hueRotate

十四、形状裁剪

clip12+

clipShape12+

mask12+

maskShape12+

十五、颜色渐变

linearGradient

sweepGradient

radialGradient


一、尺寸设置

官方文档

宽高,外边距,内边距,尺寸size

Row() {
        // 宽度80 ,高度80 ,外边距20(蓝色区域),上下左右的内边距分别为5、15、10、20(白色区域)
        Row() {
          Row().size({ width: '100%', height: '100%' }).backgroundColor(Color.Yellow)
        }
        .width(80)
        .height(80)
        .padding({ top: 5, left: 10, bottom: 15, right: 20 })
        .margin(20)
        .backgroundColor(Color.White)
      }.backgroundColor(Color.Blue)

layoutWeight

layoutWeight(value: number | string)

对子组件进行重新布局。

父容器尺寸确定时,设置了layoutWeight属性的子元素与兄弟元素占主轴尺寸按照权重进行分配,忽略元素本身尺寸设置,表示自适应占满剩余空间。

默认值:0

仅在Row/Column/Flex布局中生效。

 // 父容器尺寸确定时,设置了layoutWeight的子元素在主轴布局尺寸按照权重进行分配,忽略本身尺寸设置。
      Row() {
        // 权重1,占主轴剩余空间1/3
        Text('layoutWeight(1)')
          .size({ width: '30%', height: 110 }).backgroundColor(0xFFEFD5).textAlign(TextAlign.Center)
          .layoutWeight(1)
        // 权重2,占主轴剩余空间2/3
        Text('layoutWeight(2)')
          .size({ width: '30%', height: 110 }).backgroundColor(0xF5DEB3).textAlign(TextAlign.Center)
          .layoutWeight(2)
        // 未设置layoutWeight属性,组件按照自身尺寸渲染
        Text('no layoutWeight')
          .size({ width: '30%', height: 110 }).backgroundColor(0xD2B48C).textAlign(TextAlign.Center)
      }.size({ width: '90%', height: 140 }).backgroundColor(0xAFEEEE)

constraintSize

设置约束尺寸。constraintSize的优先级高于Width和Height。取值结果参考constraintSize取值对width/height影响。

默认值:

{

minWidth: 0,

maxWidth: Infinity,

minHeight: 0,

maxHeight: Infinity

}

单位:vp

Text('this is a Text.this is a Text.this is a Text.this is a Text.this is a Text.this is a Text.this is a Text.this is a Text.this is a Text.this is a Text.this is a Text.this is a Text.this is a Text.this is a Text.this is a Text')
        .width('90%')
        .constraintSize({ maxWidth: 200 })

二、位置设置

align

设置容器元素绘制区域内的子元素的对齐方式。

只在Stack、Button、Marquee、StepperItem、text、TextArea、TextInput、FolderStack中生效,其中和文本相关的组件Marquee、text、TextArea、TextInput的align结果参考textAlign。

不支持textAlign属性的组件则无法设置水平方向的文字对齐。

默认值:Alignment.Center

说明:

在Stack中该属性与alignContent效果一致,只能设置子组件在容器内的对齐方式。

.align(Alignment.TopStart)

 // 元素内容<元素宽高,设置内容在与元素内的对齐方式
        Text('align').fontSize(9).fontColor(0xCCCCCC).width('90%')
        Stack() {
          Text('First show in bottom end').height('65%').backgroundColor(0xD2B48C)
          Text('Second show in bottom end').backgroundColor(0xF5DEB3).opacity(0.9)
        }.width('90%').height(50).margin({ top: 5 }).backgroundColor(0xFFE4C4)
        .align(Alignment.BottomEnd)

 

direction

.direction(Direction.Ltr)

设置容器元素内主轴方向上的布局。

属性配置为auto的时候,按照系统语言方向进行布局。

该属性在Column组件上不生效。

默认值:Direction.Auto

 

// 父容器设置direction为Direction.Ltr,子元素从左到右排列
        Text('direction').fontSize(9).fontColor(0xCCCCCC).width('90%')
        Row() {
          Text('1').height(50).width('25%').fontSize(16).backgroundColor(0xF5DEB3)
          Text('2').height(50).width('25%').fontSize(16).backgroundColor(0xD2B48C)
          Text('3').height(50).width('25%').fontSize(16).backgroundColor(0xF5DEB3)
          Text('4').height(50).width('25%').fontSize(16).backgroundColor(0xD2B48C)
        }
        .width('90%')
        .direction(Direction.Ltr)

position

绝对定位,确定子组件相对父组件的位置。当父容器为Row/Column/Flex时,设置position的子组件不占位。

Position类型基于父组件左上角确定位置;Edges类型基于父组件四边确定位置,top/left/right/bottom分别为组件各边距离父组件相应边的边距,通过边距来确定组件相对于父组件的位置;

LocalizedEdges类型基于父组件四边确定位置,支持镜像模式

适用于置顶显示、悬浮按钮等组件在父容器中位置固定的场景。

不支持在宽高为零的布局容器上设置。

当父容器为RelativeContainer, 且子组件设置了alignRules属性, 则子组件的position属性不生效。

相对于父组件左上角偏移,x,y支持正负数

// 设置子组件左上角相对于父组件左上角的偏移位置
      Row() {
        Text('2 position(30, 10)')
          .size({ width: '60%', height: '30' })
          .backgroundColor(0xbbb2cb)
          .border({ width: 1 })
          .fontSize(16)
          .align(Alignment.Start)
          .position({ x: 30, y: 10 })//直接设置数字x,y
        Text('4 position(50%, 70%)')
          .size({ width: '50%', height: '50' })
          .backgroundColor(0xbbb2cb)
          .border({ width: 1 })
          .fontSize(16)
          .position({ x: '50%', y: '70%' })//也可以设置百分比
      }.width('90%').height(100).border({ width: 1, style: BorderStyle.Dashed })

 设置定位点,本来是左上角,改变这个定位点

  1. // 相对于起点偏移,其中x为最终定位点距离起点水平方向间距,x>0往左,反之向右。
  2. // y为最终定位点距离起点垂直方向间距,y>0向上,反之向下

 .markAnchor({ x: 25, y: 25 })

        Text('text')
          .fontSize('30px')
          .textAlign(TextAlign.Center)
          .size({ width: 25, height: 25 })
          .backgroundColor(Color.Green)
          .markAnchor({ x: 25, y: 25 })//定位点向左25,向上25
        Text('text')
          .fontSize('30px')
          .textAlign(TextAlign.Center)
          .size({ width: 25, height: 25 })
          .backgroundColor(Color.Green)
          .markAnchor({ x: -100, y: -25 })//定位点向右100,向下25
        Text('text')
          .fontSize('30px')
          .textAlign(TextAlign.Center)
          .size({ width: 25, height: 25 })
          .backgroundColor(Color.Green)
          .markAnchor({ x: 25, y: -25 })//定位点向左25,向下25

offset

相对偏移,组件相对原本的布局位置进行偏移。offset属性不影响父容器布局,仅在绘制时调整位置。

         Text('2  offset(15, 30)')
          .size({ width: 120, height: '50' })
          .backgroundColor(0xbbb2cb)
          .border({ width: 1 })
          .fontSize(16)
          .align(Alignment.Start)
          .offset({ x: 15, y: 30 })
        Text('3').size({ width: '15%', height: '50' }).backgroundColor(0xdeb887).border({ width: 1 }).fontSize(16)
          .textAlign(TextAlign.Center)
        Text('4 offset(-5%, 20%)')
          .size({ width: 100, height: '50' })
          .backgroundColor(0xbbb2cb)
          .border({ width: 1 })
          .fontSize(16)
          .offset({ x: '-5%', y: '20%' })

使用Edge方式position,offset

top,left,bottom,right

.position({bottom: 0, right: 0})

.position({ top: '10%', left: '50%' })

.offset({top: 30, left: 0})

.offset({bottom: 10, right: 30})


      Row() {
        Text('bottom:0, right:0').size({ width: '30%', height: '50' }).backgroundColor(0xdeb887).border({ width: 1 }).fontSize(16)
          .textAlign(TextAlign.Center).position({bottom: 0, right: 0})
        Text('top:0, left:0').size({ width: '30%', height: '50' }).backgroundColor(0xdeb887).border({ width: 1 }).fontSize(16)
          .textAlign(TextAlign.Center).position({top: 0, left: 0})
        Text('top:10%, left:50%').size({ width: '50%', height: '30' }).backgroundColor(0xbbb2cb).border({ width: 1 }).fontSize(16)
          .textAlign(TextAlign.Center).position({ top: '10%', left: '50%' })
        Text('bottom:0, left:30').size({ width: '50%', height: '30' }).backgroundColor(0xbbb2cb).border({ width: 1 }).fontSize(16)
          .textAlign(TextAlign.Center).position({ bottom: 0, left: 30 })
      }.width('90%').height(100).border({ width: 1, style: BorderStyle.Dashed })



      Row() {
        Text('1').size({ width: '25%', height: 50 }).backgroundColor(0xdeb887).border({ width: 1 }).fontSize(16)
          .textAlign(TextAlign.Center)
        Text('2 top:30, left:0').size({ width: '25%', height: 50 }).backgroundColor(0xbbb2cb).border({ width: 1 }).fontSize(16)
          .textAlign(TextAlign.Center).offset({top: 30, left: 0})
        Text('3').size({ width: '25%', height: 50 }).backgroundColor(0xdeb887).border({ width: 1 }).fontSize(16)
          .textAlign(TextAlign.Center)
        Text('4 bottom:10, right:30').size({ width: '25%', height: 50 }).backgroundColor(0xbbb2cb).border({ width: 1 }).fontSize(12)
          .textAlign(TextAlign.Center).offset({bottom: 10, right: 30})
      }.width('90%').height(150).border({ width: 1, style: BorderStyle.Dashed })

三、布局约束

aspectRatio

指定当前组件的宽高比,aspectRatio = width/height。

// 组件宽度 = 组件高度*1.5 = 90
          Text(item)
            .backgroundColor(0xbbb2cb)
            .fontSize(20)
            .aspectRatio(1.5)
            .height(60)
          // 组件高度 = 组件宽度/1.5 = 60/1.5 = 40
          Text(item)
            .backgroundColor(0xbbb2cb)
            .fontSize(20)
            .aspectRatio(1.5)
            .width(60)

displayPriority

设置当前组件在布局容器中显示的优先级。

默认值:1

说明:

仅在Row/Column/Flex(单行)容器组件中生效。

小数点后的数字不作优先级区分,即区间为[x, x + 1)内的数字视为相同优先级。例如:1.0与1.9为同一优先级。

子组件的displayPriority均为1时,优先级没有区别。

当子组件的displayPriority大于1时,若父容器空间不足,隐藏低优先级节点。

Text(item.text)
            .width(120)
            .height(60)
            .fontSize(24)
            .textAlign(TextAlign.Center)
            .backgroundColor(0xbbb2cb)
            .displayPriority(3)

四、Flex布局

三个属性

flexBasis

flexBasis(value: number | string)

设置组件的基准尺寸。可以设置宽高

flexGrow

flexGrow(value: number)

设置组件在父容器的剩余空间所占比例。子组件可以分配多余空间

flexShrink

flexShrink(value: number)

设置父容器压缩尺寸分配给此属性所在组件的比例。当父容器为Column、Row时,需设置主轴方向的尺寸。

父组件空间不足,压缩子组件

alignSelf

alignSelf(value: ItemAlign)

子组件在父容器交叉轴的对齐格式。

// alignSelf会覆盖Flex布局容器中的alignItems设置

Flex({ direction: FlexDirection.Row, alignItems: ItemAlign.Center }){

         Text('alignSelf End')

                .alignSelf(ItemAlign.End)//子组件覆盖父组件

}

// xxx.ets
@Entry
@Component
struct FlexExample {
  build() {
    Column({ space: 5 }) {
      Text('flexBasis').fontSize(9).fontColor(0xCCCCCC).width('90%')
      // 基于主轴的基准尺寸
      // flexBasis()值可以是字符串'auto',表示基准尺寸是元素本来的大小,也可以是长度设置,相当于.width()/.height()
      Flex() {
        Text('flexBasis(100)')
          .flexBasis(100) // 这里表示宽度为100vp
          .height(100)
          .backgroundColor(0xF5DEB3)
          .textAlign(TextAlign.Center)
        Text(`flexBasis('auto')`)
          .flexBasis('auto') // 这里表示宽度保持原本设置的60%的宽度
          .width('60%')
          .height(100)
          .backgroundColor(0xD2B48C)
          .textAlign(TextAlign.Center)
      }.width('90%').height(120).padding(10).backgroundColor(0xAFEEEE)

      Text('flexGrow').fontSize(9).fontColor(0xCCCCCC).width('90%')
      // flexGrow()表示剩余空间分配给该元素的比例
      Flex() {
        Text('flexGrow(2)')
          .flexGrow(2) // 父容器分配给该Text的宽度为剩余宽度的2/3
          .height(100)
          .backgroundColor(0xF5DEB3)
          .textAlign(TextAlign.Center)
        Text('flexGrow(1)')
          .flexGrow(1) // 父容器分配给该Text的宽度为剩余宽度的1/3
          .height(100)
          .backgroundColor(0xD2B48C)
          .textAlign(TextAlign.Center)
      }.width('90%').height(120).padding(10).backgroundColor(0xAFEEEE)

      Text('flexShrink').fontSize(9).fontColor(0xCCCCCC).width('90%')
      // flexShrink()表示该元素的压缩比例,基于超出的总尺寸进行计算
      // 第一个text压缩比例是0,另外两个都是1,因此放不下时等比例压缩后两个,第一个不压缩
      Flex({ direction: FlexDirection.Row }) {
        Text('flexShrink(0)')
          .flexShrink(0)
          .width('50%')
          .height(100)
          .backgroundColor(0xF5DEB3)
          .textAlign(TextAlign.Center)
        Text('default flexShrink') // 默认值为1
          .width('40%')
          .height(100)
          .backgroundColor(0xD2B48C)
          .textAlign(TextAlign.Center)
        Text('flexShrink(1)')
          .flexShrink(1)
          .width('40%')
          .height(100)
          .backgroundColor(0xF5DEB3)
          .textAlign(TextAlign.Center)
      }.width('90%').height(120).padding(10).backgroundColor(0xAFEEEE)

      Text('alignSelf').fontSize(9).fontColor(0xCCCCCC).width('90%')
      // alignSelf会覆盖Flex布局容器中的alignItems设置
      Flex({ direction: FlexDirection.Row, alignItems: ItemAlign.Center }) {
        Text('no alignSelf,height:70')
          .width('33%')
          .height(70)
          .backgroundColor(0xF5DEB3)
          .textAlign(TextAlign.Center)
        Text('alignSelf End')
          .alignSelf(ItemAlign.End)
          .width('33%')
          .height(70)
          .backgroundColor(0xD2B48C)
          .textAlign(TextAlign.Center)
        Text('no alignSelf,height:100%')
          .width('34%')
          .height('100%')
          .backgroundColor(0xF5DEB3)
          .textAlign(TextAlign.Center)
      }.width('90%').height(120).padding(10).backgroundColor(0xAFEEEE)
    }.width('100%').margin({ top: 5 })
  }
}

五、边框

border

border(value: BorderOptions)

设置边框样式。

// xxx.ets
@Entry
@Component
struct BorderExample {
  build() {
    Column() {
      Flex({ justifyContent: FlexAlign.SpaceAround, alignItems: ItemAlign.Center }) {
        // 线段.borderStyle(BorderStyle.Dashed).borderWidth(5).borderColor(0xAFEEEE).borderRadius(10)
        Text('dashed')
          .borderStyle(BorderStyle.Dashed).borderWidth(5).borderColor(0xAFEEEE).borderRadius(10)
          .width(120).height(120).textAlign(TextAlign.Center).fontSize(16)
        // 点线.border({ width: 5, color: 0x317AF7, radius: 10, style: BorderStyle.Dotted })
        Text('dotted')
          .border({ width: 5, color: 0x317AF7, radius: 10, style: BorderStyle.Dotted })
          .width(120).height(120).textAlign(TextAlign.Center).fontSize(16)
      }.width('100%').height(150)

      Text('.border')
        .fontSize(50)
        .width(300)
        .height(300)
        .border({
          width: { left: 3, right: 6, top: 10, bottom: 15 },
          color: { left: '#e3bbbb', right: Color.Blue, top: Color.Red, bottom: Color.Green },
          radius: { topLeft: 10, topRight: 20, bottomLeft: 40, bottomRight: 80 },
          style: {
            left: BorderStyle.Dotted,
            right: BorderStyle.Dotted,
            top: BorderStyle.Solid,
            bottom: BorderStyle.Dashed
          }
        }).textAlign(TextAlign.Center)
    }
  }
}

六、背景设置

backgroundColor

backgroundColor(value: ResourceColor)

设置组件背景色。

backgroundImage

backgroundImage(src: ResourceStr | PixelMap, repeat?: ImageRepeat)

设置组件的背景图片

backgroundImageSize

backgroundImageSize(value: SizeOptions | ImageSize)

设置组件背景图片的宽高

 Row()
        .backgroundImage('/comment/bg.jpg', ImageRepeat.X)
        .backgroundImageSize({ width: '250px', height: '140px' })
        .width('90%')
        .height(70)
        .border({ width: 1 })

backgroundImagePosition

backgroundImagePosition(value: Position | Alignment)

设置背景图的位置。

Row()
        .width(100)
        .height(50)
        .backgroundImage($r('app.media.avatar'), ImageRepeat.NoRepeat)
        .backgroundImageSize({ width: 100, height: 100 })
        .backgroundImagePosition({ x: -50, y: -30 })
        .border({ width: 1 })

七、透明度设置

opacity

opacity(value: number | Resource)

设置组件的不透明度。

元素的不透明度,取值范围为0到1,1表示不透明,0表示完全透明, 达到隐藏组件效果,但是在布局中占位。

默认值:1

说明:

子组件会继承父组件的透明度,并与自身的透明度属性叠加。如:父组件透明度为0.1,子组件设置透明度为0.8,则子组件实际透明度为0.1*0.8=0.08。

Text().width('90%').height(50).opacity(0.7).backgroundColor(0xAFEEEE)

八、显隐控制

visibility

visibility(value: Visibility)

控制组件的显隐

控制当前组件显示或隐藏。根据具体场景需要可使用条件渲染代替。

默认值:Visibility.Visible

// xxx.ets
@Entry
@Component
struct VisibilityExample {
  build() {
    Column() {
      Column() {
        // 隐藏不参与占位
        Text('None').fontSize(9).width('90%').fontColor(0xCCCCCC)
        Row().visibility(Visibility.None).width('90%').height(80).backgroundColor(0xAFEEEE)

        // 隐藏参与占位
        Text('Hidden').fontSize(9).width('90%').fontColor(0xCCCCCC)
        Row().visibility(Visibility.Hidden).width('90%').height(80).backgroundColor(0xAFEEEE)

        // 正常显示,组件默认的显示模式
        Text('Visible').fontSize(9).width('90%').fontColor(0xCCCCCC)
        Row().visibility(Visibility.Visible).width('90%').height(80).backgroundColor(0xAFEEEE)
      }.width('90%').border({ width: 1 })
    }.width('100%').margin({ top: 5 })
  }
}

九、禁用控制

组件是否可交互,可交互状态下响应点击事件、触摸事件、拖拽事件、按键事件、焦点事件和鼠标事件。

enabled

enabled(value: boolean)

设置组件是否可交互。

值为true表示组件可交互,响应点击等操作。

值为false表示组件不可交互,不响应点击等操作。

默认值:true

// 点击没有反应
      Button('disable').enabled(false)

十、浮层

overlay

overlay(value: string | CustomBuilder | ComponentContent, options?: { align?: Alignment; offset?: { x?: number; y?: number } })

在当前组件上,增加遮罩文本或者叠加自定义组件以及ComponentContent作为该组件的浮层。

文本 

Image($r('app.media.img'))
            .width(240).height(240)
            .overlay("Winter is a beautiful season, especially when it snows.", {
              align: Alignment.Bottom,
              offset: { x: 0, y: -15 }
            })

组件

// xxx.ets
@Entry
@Component
struct OverlayExample {
  @Builder OverlayNode() {
    Column() {
      Image($r('app.media.img1'))
      Text("This is overlayNode").fontSize(20).fontColor(Color.White)
    }.width(180).height(180).alignItems(HorizontalAlign.Center)
  }

  build() {
    Column() {
      Image($r('app.media.img2'))
        .overlay(this.OverlayNode(), { align: Alignment.Center })
        .objectFit(ImageFit.Contain)
    }.width('100%')
    .border({ color: Color.Black, width: 2 }).padding(20)
  }
}

 十一、Z序控制

zIndex

zIndex(value: number)

设置组件的堆叠顺序。

 // stack会重叠组件, 默认后定义的在最上面,具有较高zIndex值的元素在zIndex较小的元素前面
        Text('1, zIndex(2)')
          .size({ width: '40%', height: '30%' }).backgroundColor(0xbbb2cb)
          .zIndex(2)
        Text('2, default zIndex(1)')
          .size({ width: '70%', height: '50%' }).backgroundColor(0xd2cab3).align(Alignment.TopStart)
          .zIndex(1)

十二、图形变换

用于对组件进行旋转、平移、缩放、矩阵变换等操作。

Row()
        .rotate({
          x: 0,
          y: 0,
          z: 1,
          centerX: '50%',
          centerY: '50%',
          angle: 300
        }) // 组件以矢量(0,0,1)为旋转轴,绕中心点顺时针旋转300度


Row()
        .translate({ x: 100, y: 10 }) // x轴方向平移100,y轴方向平移10
Row()
        .scale({ x: 2, y: 0.5 }) // 高度缩小一倍,宽度放大一倍,z轴在2D下无效果

//矩阵变化
Row()
        .width(100).height(100).backgroundColor(0xAFEEEE)
        .transform(matrix4.identity().translate({ x: 50, y: 50 }).scale({ x: 1.5, y: 1 }).rotate({
          x: 0,
          y: 0,
          z: 1,
          angle: 60
        }))
// xxx.ets
import { matrix4 } from '@kit.ArkUI';

@Entry
@Component
struct TransformExample {
  build() {
    Column() {
      Text('rotate').width('90%').fontColor(0xCCCCCC).padding(15).fontSize(14)
      Row()
        .rotate({
          x: 0,
          y: 0,
          z: 1,
          centerX: '50%',
          centerY: '50%',
          angle: 300
        }) // 组件以矢量(0,0,1)为旋转轴,绕中心点顺时针旋转300度
        .width(100).height(100).backgroundColor(0xAFEEEE)

      Text('translate').width('90%').fontColor(0xCCCCCC).padding(10).fontSize(14)
      Row()
        .translate({ x: 100, y: 10 }) // x轴方向平移100,y轴方向平移10
        .width(100).height(100).backgroundColor(0xAFEEEE).margin({ bottom: 10 })

      Text('scale').width('90%').fontColor(0xCCCCCC).padding(15).fontSize(14)
      Row()
        .scale({ x: 2, y: 0.5 }) // 高度缩小一倍,宽度放大一倍,z轴在2D下无效果
        .width(100).height(100).backgroundColor(0xAFEEEE)

      Text('Matrix4').width('90%').fontColor(0xCCCCCC).padding(15).fontSize(14)
      Row()
        .width(100).height(100).backgroundColor(0xAFEEEE)
        .transform(matrix4.identity().translate({ x: 50, y: 50 }).scale({ x: 1.5, y: 1 }).rotate({
          x: 0,
          y: 0,
          z: 1,
          angle: 60
        }))
    }.width('100%').margin({ top: 5 })
  }
}

十三、图像效果

blur

blur(value: number, options?: BlurOptions)

为组件添加内容模糊效果。

 // 对字体进行模糊
Text('blur text')
          .blur(5)

backdropBlur

backdropBlur(value: number, options?: BlurOptions)

为组件添加背景模糊效果。

 // 对背景进行模糊 
Text()
        .width('90%')
        .height(40)
        .fontSize(16)
        .backdropBlur(3)
        .backgroundImage($r('app.media.avatar'))
        .backgroundImageSize({ width: 100, height: 40 })

shadow

shadow(value: ShadowOptions | ShadowStyle)

为组件添加阴影效果。

// 添加阴影效果,图片效果不变
//fill:true内部填充
      Image($r('app.media.zfb_pro_list1'))
        .width('90%')
        .height(30)
        .shadow({ radius: 10, color: Color.Green, offsetX: 20, offsetY: 20 })

      // 添加内部阴影效果
      Text('shadow').fontSize(15).fontColor(0xCCCCCC).width('90%')
      Image($r('app.media.zfb_pro_list1'))
        .width('90%')
        .height(30)
        .shadow({ radius: 5, color: Color.Green, offsetX: 20, offsetY: 20,fill:true }).opacity(0.5)

grayscale

grayscale(value: number)

为组件添加灰度效果。

// 灰度效果0~1,越接近1,灰度越明显
      Text('grayscale').fontSize(15).fontColor(0xCCCCCC).width('90%')
      Image($r('app.media.zfb_pro_list1')).width('90%').height(30).grayscale(0.3)
      Image($r('app.media.zfb_pro_list1')).width('90%').height(30).grayscale(0.8)

brightness

brightness(value: number)

为组件添加高光效果。

  // 高光效果,1为正常图片,<1变暗,>1亮度增大
      Text('brightness').fontSize(15).fontColor(0xCCCCCC).width('90%')
      Image($r('app.media.zfb_pro_list1')).width('90%').height(30).brightness(1.2)

saturate

saturate(value: number)

为组件添加饱和度效果。

     // 饱和度,原图为1
      Text('saturate').fontSize(15).fontColor(0xCCCCCC).width('90%')
      Image($r('app.media.zfb_pro_list1')).width('90%').height(30).saturate(2.0)
      Image($r('app.media.zfb_pro_list1')).width('90%').height(30).saturate(0.7)

contrast

contrast(value: number)

为组件添加对比度效果。

// 对比度,1为原图,>1值越大越清晰,<1值越小越模糊
      Text('contrast').fontSize(15).fontColor(0xCCCCCC).width('90%')
      Image($r('app.media.zfb_pro_list1')).width('90%').height(30).contrast(2.0)
      Image($r('app.media.zfb_pro_list1')).width('90%').height(30).contrast(0.8)

invert

invert(value: number | InvertOptions)

反转输入的图像。

// 图像反转比例
      Text('invert').fontSize(15).fontColor(0xCCCCCC).width('90%')
      Image($r('app.media.zfb_pro_list1')).width('90%').height(30).invert(0.2)
      Image($r('app.media.zfb_pro_list1')).width('90%').height(30).invert(0.8)

sepia

sepia(value: number)

将图像转换为深褐色。

// 深褐色
      Text('sepia').fontSize(15).fontColor(0xCCCCCC).width('90%')
      Image($r('app.media.zfb_pro_list1')).width('90%').height(30).sepia(0.8)

hueRotate

hueRotate(value: number | string)

色相旋转效果。

// 色相旋转
      Text('hueRotate').fontSize(15).fontColor(0xCCCCCC).width('90%')
      Image($r('app.media.zfb_pro_list1')).width('90%').height(30).hueRotate(90)
    }.width('100%').margin({ top: 5 })

十四、形状裁剪

用于对组件进行裁剪、遮罩处理。

clip12+

clip(value: boolean)

是否对当前组件进行裁剪。

Row() {
        Image($r('app.media.testImg')).width('500px').height('280px')
      }
      .clip(true) // 如这里不设置clip为true,则Row组件的圆角不会限制其中的Image组件,Image组件的四个角会超出Row
      .borderRadius(20)

clipShape12+

clipShape(value: CircleShape | EllipseShape | PathShape | RectShape)

按指定的形状对当前组件进行裁剪。

 // 用一个280px直径的圆对图片进行裁剪
      Image($r('app.media.testImg'))
        .clipShape(new Circle({ width: '280px', height: '280px' }))
        .width('500px').height('280px')

mask12+

mask(value: ProgressMask)

maskShape12+

为组件上添加指定形状的遮罩。

mask(value:  CircleShape | EllipseShape | PathShape | RectShape)

为组件上添加指定形状的遮罩。

// 给图片添加了一个500px*280px的方形遮罩
      Image($r('app.media.testImg'))
        .maskShape(new Rect({ width: '500px', height: '280px' }).fill(Color.Gray))
        .width('500px').height('280px')

      // 给图片添加了一个280px*280px的圆形遮罩
      Image($r('app.media.testImg'))
        .maskShape(new Circle({ width: '280px', height: '280px' }).fill(Color.Gray))
        .width('500px').height('280px')

十五、颜色渐变

linearGradient

线性渐变。

- angle: 线性渐变的起始角度。0点方向顺时针旋转为正向角度。

默认值:180

角度为字符串时仅支持类型deg,grad,rad,trun。

- direction: 线性渐变的方向,设置angle后不生效。

默认值:GradientDirection.Bottom

- colors: 指定某百分比位置处的渐变色颜色,设置非法颜色直接跳过。

- repeating: 为渐变的颜色重复着色。

默认值:false

// xxx.ets
@Entry
@Component
struct ColorGradientExample {
  build() {
    Column({ space: 5 }) {
      Text('linearGradient').fontSize(12).width('90%').fontColor(0xCCCCCC)
      Row()
        .width('90%')
        .height(50)
        .linearGradient({
          angle: 90,//角度
          colors: [[0xff0000, 0.0], [0x0000ff, 0.3], [0xffff00, 1.0]]
        })
      Text('linearGradient Repeat').fontSize(12).width('90%').fontColor(0xCCCCCC)
      Row()
        .width('90%')
        .height(50)
        .linearGradient({
          direction: GradientDirection.Left, // 渐变方向
          repeating: true, // 渐变颜色是否重复
          colors: [[0xff0000, 0.0], [0x0000ff, 0.3], [0xffff00, 0.5]] // 数组末尾元素占比小于1时满足重复着色效果
        })
    }
    .width('100%')
    .padding({ top: 5 })
  }
}

sweepGradient

角度渐变,仅绘制0-360度范围内的角度,超出时不绘制渐变色,只绘制纯色。

- center:为角度渐变的中心点,即相对于当前组件左上角的坐标。

- start:角度渐变的起点。

默认值:0

角度为字符串时仅支持类型deg,grad,rad,trun。

- end:角度渐变的终点。

默认值:0

角度为字符串时仅支持类型deg,grad,rad,trun。

- rotation: 角度渐变的旋转角度。

默认值:0

角度为字符串时仅支持类型deg,grad,rad,trun。

- colors: 指定某百分比位置处的渐变色颜色,设置非法颜色直接跳过。

- repeating: 为渐变的颜色重复着色。

默认值:false

说明:

设置为小于0的值时,按值为0处理,设置为大于360的值时,按值为360处理。

当start、end、rotation的数据类型为string,合法的取值为纯数字或纯数字后带"deg"(度)、"rad"(弧度)、"grad"(梯度)、"turn"(圈)单位,例如:"90"、 "90deg"、"1.57rad"。

@Entry
@Component
struct ColorGradientExample {
  build() {
    Column({ space: 5 }) {
      Text('sweepGradient').fontSize(12).width('90%').fontColor(0xCCCCCC)
      Row()
        .width(100)
        .height(100)
        .sweepGradient({
          center: [50, 50],
          start: 0,
          end: 359,
          colors: [[0xff0000, 0.0], [0x0000ff, 0.3], [0xffff00, 1.0]]
        })
      
      Text('sweepGradient Reapeat').fontSize(12).width('90%').fontColor(0xCCCCCC)
      Row()
        .width(100)
        .height(100)
        .sweepGradient({
          center: [50, 50],
          start: 0,
          end: 359,
          rotation: 45, // 旋转角度
          repeating: true, // 渐变颜色是否重复
          colors: [[0xff0000, 0.0], [0x0000ff, 0.3], [0xffff00, 0.5]] // 数组末尾元素占比小于1时满足重复着色效果
        })
    }
    .width('100%')
    .padding({ top: 5 })
  }
}

 

radialGradient

径向渐变。

- center:径向渐变的中心点,即相对于当前组件左上角的坐标。

- radius:径向渐变的半径。

取值范围:[0,+∞)

说明:

设置为小于的0值时,按值为0处理。

- colors: 指定某百分比位置处的渐变色颜色,设置非法颜色直接跳过。

- repeating: 为渐变的颜色重复着色。

默认值:false

// xxx.ets
@Entry
@Component
struct ColorGradientExample {
  build() {
    Column({ space: 5 }) {
      Text('radialGradient').fontSize(12).width('90%').fontColor(0xCCCCCC)
      Row()
        .width(100)
        .height(100)
        .radialGradient({
          center: [50, 50],
          radius: 60,
          colors: [[0xff0000, 0.0], [0x0000ff, 0.3], [0xffff00, 1.0]]
        })
      Text('radialGradient Repeat').fontSize(12).width('90%').fontColor(0xCCCCCC)
      Row()
        .width(100)
        .height(100)
        .radialGradient({
          center: [50, 50],
          radius: 60,
          repeating: true,
          colors: [[0xff0000, 0.0], [0x0000ff, 0.3], [0xffff00, 0.5]] // 数组末尾元素占比小于1时满足重复着色效果
        })
    }
    .width('100%')
    .padding({ top: 5 })
  }
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1976619.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

数字转罗马字符

import java.util.Scanner;/*** author gyf* ClassName Test* Date 2024/7/31 17:14* Version V1.0* Description : 方法一*/ public class Test {public static void main(String[] args) {Scanner scanner new Scanner(System.in);System.out.println("请输入一个字符串…

php类与对象

前言 欢迎来到我的博客 个人主页:北岭敲键盘的荒漠猫-CSDN博客 本文主要整理php类与对象相关的知识点 适合有编程基础的人观看 因为我这个也是整理第n语言&#xff0c;这些老套的概念就不再多啰嗦了。 直接整理相应的语法 感觉把php当第一语言学的人不太多了 快速理解类与对…

MyBatis的example.createCriteria()方法学习记录

目录 一、mapper的crud方法:1. insert方法insert(User user)insertSelective(User user) 2. select方法selectByPrimaryKey(id)selectByExample(example)selectCountByExample(example) 3. update方法updateByPrimaryKey(User user)updateByPrimaryKeySelective(User user)upda…

从零开始的MicroPython(六)ADC

上一篇&#xff1a;PWM 文章目录 ADC是什么ESP32的ADC代码 ADC是什么 ADC的英文全称是Analog / Digital Converter&#xff0c;是将模拟信号转换为数字信号的转换器&#xff0c;ADC是单片机读取传感器信号的常见方式。 我们日常生活中的信号&#xff0c;例如光照强度&#xf…

机器学习练手(三):基于决策树的iris 多分类和波士顿房价预测

总结&#xff1a;本文为和鲸python 可视化探索训练营资料整理而来&#xff0c;加入了自己的理解&#xff08;by GPT4o&#xff09; 原活动链接 原作者&#xff1a;vgbhfive&#xff0c;多年风控引擎研发及金融模型开发经验&#xff0c;现任某公司风控研发工程师&#xff0c;对…

python通过pyautogui自动给微信聊天窗口发消息

使用py脚本自动给聊天窗口发消息 1.突然的自我2.编写脚本玩一把i.先获取窗口位置ii.模拟聊天iii.疗效不错呢 1.突然的自我 突然想到pyautogui可以做那么事情&#xff0c; 那么是不是可以模拟聊天呢&#xff0c;如果结合现在的大模型chatGPT一边问然后得到结果一边自动和别人聊…

一文读懂新版Nacos的使用方式

文章目录 什么是 NacosNacos 架构Nacos 的本地启动 构建提供者 provider-nacos-8081搭建环境编写配置文件 application.yaml构建数据库编写业务实体类控制器类逻辑层与数据层接口 模块结构 构建消费者 consumer-nacos-8080搭建环境编写 yaml 文件配置编写业务编写配置类编写 Co…

Linux系统之NFS服务配置

准备工作 克隆两台linux&#xff0c;并更改其Mac地址&#xff0c;作为NFS客户端&#xff1b;将服务器更名为学号nfsserver&#xff0c;配置IP地址为192.168.学号.1 将客户端Client1更名为学号client1&#xff0c;配置IP地址为192.168.学号.2 将客户端Client2更名为学号clien…

达梦数据库一体机在宜昌市财政局上线了!

财政作为国家治理的基础和重要支柱&#xff0c;其数字化转型已成为构建现代财政制度的必由之路&#xff0c;引领着财政管理体系向更高效、更智能的方向迈进。 达梦数据全面助力财政信息化转型与智能化发展&#xff0c;采用 DAMEGN PAI I 系列数据库一体机&#xff0c;为宜昌市财…

python实现图像分割算法3

python实现区域增长算法 算法原理基本步骤数学模型Python实现详细解释优缺点应用领域区域增长算法是一种经典的图像分割技术,它的目标是将图像划分为多个互不重叠的区域。该算法通过迭代地合并与种子区域相似的邻域像素来实现分割。区域增长算法通常用于需要精确分割的场景,如…

css实现文字根据条件渐变

body 选择器 body { padding: 50vh 0; text-align: center; font-size: 6em; } padding: 50vh 0; 设置了body的上下内边距为视口高度的50%&#xff0c;左右内边距为0。text-align: center; 使得body内的文本内容居中显示。font-size: 6em; 设置了字体大小为当前字体尺寸的6倍…

Solana 自建节点搭建教程:手把手教你成为区块链网络的重要一员

区块链技术正在迅速改变世界&#xff0c;而Solana作为新一代高性能公链&#xff0c;以其出色的性能和低廉的交易费用吸引了众多开发者和用户。如果你想成为Solana生态系统的一部分&#xff0c;搭建自己的Solana节点是一个绝佳的选择。本教程将详细介绍如何一步步搭建Solana自建…

MyBatis 如何通过拦截器修改 SQL

目录 1. 实现Interceptor接口2. 注册配置文件 假如我们想实现多租户&#xff0c;或者在某些 SQL 后面自动拼接查询条件。在开发过程中大部分场景可能都是一个查询写一个 SQL 去处理&#xff0c;我们如果想修改最终 SQL 可以通过修改各个 mapper.xml 中的 SQL 来处理。 但实际过…

【C语言】结构体内存布局解析——字节对齐

&#x1f984;个人主页:小米里的大麦-CSDN博客 &#x1f38f;所属专栏:https://blog.csdn.net/huangcancan666/category_12718530.html &#x1f381;代码托管:黄灿灿 (huang-cancan-xbc) - Gitee.com ⚙️操作环境:Visual Studio 2022 目录 一、引言 二、什么是字节对齐&…

开源个性化自托管服务仪表板:Dashy

**Dashy&#xff1a;**一站式管理&#xff0c;个性化展现- 精选真开源&#xff0c;释放新价值。 概览 Dashy是一个创新的自托管仪表板解决方案&#xff0c;旨在为用户提供一个集中管理多个在线服务的平台。通过直观的界面设计&#xff0c;Dashy允许用户快速访问他们的自托管应…

【C++】内联函数vs宏 nullptr

目录 宏的优缺点分析概念回顾宏的缺点宏的优点 内联函数&#xff08;inline&#xff09;inline函数的定义和声明总结 宏的优缺点分析 概念回顾 下面是宏的申明方式&#xff1a; #define name( parament-list ) stuff //其中的 parament-list 是一个由逗号隔开的符号表&#x…

一个能够在网上爬取思维导图的python小程序

这个小程序是为需要从网上爬取思维导图的朋友写的,时间久了怕被遗忘在垃圾箱里,所以贴出来,给需要的同学使用。 河西石原创地址:https://haigear.blog.csdn.net/article/details/140878039 二、使用方法及流程介绍 简单的说明一下使用的方法: 1、在网上找到自己需要的思…

GBase8c psycopg2安装(centos6)

GBase8c psycopg2安装(centos6) 安装步骤&#xff1a; [rootcentos6 ~]# cd /opt/python/ [rootcentos6 python]# ls psycopg2-2.7.7.tar.gz [rootcentos6 python]# tar -zxf psycopg2-2.7.7.tar.gz [rootcentos6 python]# cd psycopg2-2.7.7 # 安装命令 [rootcentos6 psycop…

【C++:jsoncpp库的配置CMAKE的安装】

CMAKE的安装&#xff1a; 安装路径&#xff1a;Download CMake安装就是无脑Next跳出以下窗口以上步骤完了之后&#xff0c;页面如此&#xff0c;然后点击generate jsoncpp库的配置&#xff1a; 打开生成的源文件所在路径&#xff0c;找到名为jsoncpp.sln的文件&#xff0c;以vs…

大数据信用报告怎么查?有哪些注意事项?

大数据信用对于有资金周转的人来说是比较重要的&#xff0c;主要由于大数据信用无形的被不少机构用于贷前风控&#xff0c;无论是机构要求的还是自查&#xff0c;提前了解大数据信用情况是常规操作&#xff0c;那大数据信用报告如何查询?有哪些需要注意的呢?本文详细为大家讲…