HarmonyOS 自定义View怎么转换为PixelMap

HarmonyOS
2024-12-24 15:40:34
浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
zbw_apple

参考以下代码:

import componentSnapshot from '@ohos.arkui.componentSnapshot'
import image from '@ohos.multimedia.image'
@Entry
@Component
struct waterMarkPage {
  @State pixmap: image.PixelMap | undefined = undefined
  build() {
    Column() {
      Stack({ alignContent: Alignment.Center }){
        Image($r('app.media.startIcon')).autoResize(true).width(300).height(300)
        Row() {
          Text("水印").width(40).height(20).fontSize(16).fontColor(Color.White)
            .border({ color: Color.Red, width: 1 }).borderRadius(4)
            .margin({top:10,right:10})
        }
        .width(300).height(300)
        .alignItems(VerticalAlign.Top)
        .justifyContent(FlexAlign.End)
        Row() {
          Image($r('app.media.startIcon')).autoResize(true).width(40).height(40).margin({bottom:10,right:10})
        }
        .width(300).height(300)
        .alignItems(VerticalAlign.Bottom)
        .justifyContent(FlexAlign.End)
      }
      .id("root")
      Button("生成水印图片")
        .onClick(() => {
          componentSnapshot.get("root")
            .then((pixmap: image.PixelMap) => {
              this.pixmap = pixmap
            }).catch((err:Error) => {
            console.log("error: " + err)
          })
        }).margin(10)
      Image(this.pixmap).width(300).height(300).border({ color: Color.Blue, width: 1 }).margin(5)
    }
    .width('100%')
    .height('100%')
    .alignItems(HorizontalAlign.Center)
  }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
分享
微博
QQ
微信
回复
2024-12-24 18:15:03
相关问题
HarmonyOS 有没有类似Canvas的自定义view
1035浏览 • 1回复 待解决
如何将app.media.app_icon,转换为PixelMap
3074浏览 • 1回复 待解决
HarmonyOS 怎么将日期转换为星期几
1090浏览 • 1回复 待解决
base64怎么转换为PDF文件
263浏览 • 0回复 待解决
HarmonyOS 怎么自定义装饰器
782浏览 • 1回复 待解决
HarmonyOS SVG转换pixelMap
722浏览 • 1回复 待解决
HarmonyOS 怎么自定义Tab的Tabbar
745浏览 • 1回复 待解决
HarmonyOS 自定义绘制接口怎么使用
677浏览 • 1回复 待解决
json 如何转换自定义的业务类对象
1623浏览 • 1回复 待解决