【HarmonyOS NEXT】实现截图功能

【HarmonyOS NEXT】实现截图功能

【需求】

实现:实现点击截图按钮,实现对页面/组件的截图

【步骤】

  1. 编写页面UI
@Entry
@Component
struct Screenshot {
  @Builder
  getSnapContent() {
    Column() {
      Image('')
        .width('100%')
        .objectFit(ImageFit.Auto)
        .borderRadius(6)
    }
    .padding('10%')
    .width('100%')
    .height('100%')
    .justifyContent(FlexAlign.Center)
    .backgroundColor('rgba(0,0,0,0.5)')
    .onClick(() => {
    })

  }

  @Builder
  getIconUI() {
    Image($r('app.media.ic_snapshot'))
      .width(20)
      .aspectRatio(1)
      .fillColor(Color.Black)
      .margin({ right: 10 })
      .onClick(async () => {
      })
  }

  build() {
    Column({ space: 5 }) {
      Row() {
        Text('Hello Snapshot').fontSize(24)
        this.getIconUI()
      }.justifyContent(FlexAlign.SpaceBetween).padding(20).width('100%')

      Image('https://img20.360buyimg.com/img/jfs/t1/241153/31/4968/64736/65e53e56Fd3868b6e/b595d41ca8447ea4.jpg')
        .width('60%')
        .border({ width: 1, color: Color.Red })
        .borderRadius(8)

      Text('我是内容1')
      Text('我是内容2')
      Text('我是内容3')
    }
    .height('100%')
    .width('100%')
    .padding({ top: 50 })
    .backgroundColor(Color.White)
  }
}
  1. 导包
import { image } from '@kit.ImageKit';
import { componentSnapshot } from '@kit.ArkUI';
  1. 给需要快照的组件设置id
Column(){
        
}.id('page')
  1. 点击调api实现截图效果
Image($r('app.media.ic_snapshot'))
  .onClick(async () => {
    this.snapshotImage = await componentSnapshot.get('page')
    this.showSnap = !this.showSnap // 显示截屏
    // Todo: 截屏音效
    // Todo: 保存到相册
  })

【完整代码】

import { image } from '@kit.ImageKit';
import { componentSnapshot } from '@kit.ArkUI';

@Entry
@Component
struct Screenshot {
  @State snapshotImage: image.PixelMap | null = null
  @State showSnap: boolean = false

  @Builder
  getSnapContent() {
    Column() {
      Image(this.snapshotImage)
        .width('100%')
        .objectFit(ImageFit.Auto)
        .borderRadius(6)
    }
    .padding('10%')
    .width('100%')
    .height('100%')
    .justifyContent(FlexAlign.Center)
    .backgroundColor('rgba(0,0,0,0.5)')
    .onClick(() => {
      this.showSnap = false
    })

  }

  @Builder
  getIconUI() {
    Image($r('app.media.ic_snapshot'))
      .width(20)
      .aspectRatio(1)
      .fillColor(Color.Black)
      .margin({ right: 10 })
      .onClick(async () => {
        this.snapshotImage = await componentSnapshot.get('page')
        this.showSnap = !this.showSnap // 显示截屏
        // Todo: 截屏音效
        // Todo: 保存到相册
      })
  }

  build() {
    Column({ space: 5 }) {
      Row() {
        Text('Hello Snapshot').fontSize(24)
        this.getIconUI()
      }.justifyContent(FlexAlign.SpaceBetween).padding(20).width('100%')

      Image('https://img20.360buyimg.com/img/jfs/t1/241153/31/4968/64736/65e53e56Fd3868b6e/b595d41ca8447ea4.jpg')
        .width('60%')
        .border({ width: 1, color: Color.Red })
        .borderRadius(8)

      Text('我是内容1')
      Text('我是内容2')
      Text('我是内容3')
    }
    .height('100%')
    .width('100%')
    .padding({ top: 50 })
    .backgroundColor(Color.White)
    .id('page')
    .bindContentCover($$this.showSnap, this.getSnapContent(), {
      modalTransition: ModalTransition.NONE
    })
  }
}

【效果图】

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值