HarmonyOS 鸿蒙Next有录屏所展示的对图片裁剪以及遮罩笔相关的demo吗

发布于 1周前 作者 wuwangju 来自 鸿蒙OS

HarmonyOS 鸿蒙Next有录屏所展示的对图片裁剪以及遮罩笔相关的demo吗

鸿蒙Next有录屏所展示的对图片裁剪以及遮罩笔相关的demo吗

2 回复

1.裁剪框手动调整大小,暂时没有demo,可以看下:https://developer.huawei.com/consumer/cn/codelabsPortal/carddetails/tutorials_NEXT-ImageEdit

2.遮罩笔:通过手势事件以及Canvas画布

//index文件
import { DrawView } from './components/DrawView';
import { DrawViewModel } from './viewmodel/DrawViewModel';

@Entry
@Component
struct Index {
  @State viewModel: DrawViewModel = new DrawViewModel()

  build() {
    Stack() {
      Image($rawfile('test.png'))
        .objectFit(ImageFit.Fill)
        .width('100%')
        .height('100%')

      DrawView({ viewModel: this.viewModel })
        .width('100%')
        .height('100%')
    }
  }
}
//DrawPathPointModel文件
export class DrawPathPointModel {
  x: number = 0
  y: number = 0
}

export enum DrawPathType {
  pen = 0,     //画笔
  pattern,     //马赛克
}

// 配置画笔
export class DrawPathModel {

  public pathType: DrawPathType = DrawPathType.pen

  public color: string = "#ED1B1B"
  public lineWidth: number = 8

  public img: ImageBitmap = new ImageBitmap("Images/canvas.png")

}

@Observed
export class DrawViewModel {
  public settings: RenderingContextSettings = new RenderingContextSettings(true)
  public context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings)
  public drawModel: DrawPathModel = new DrawPathModel()
  public canvasHeight: number = 0
  public canvasWidth: number = 0

  private pattern: CanvasPattern | null
  private points: DrawPathPointModel[] = []
  // 绘制路径
  private drawPath = new Path2D()

  constructor() {
    this.pattern = this.context.createPattern(this.drawModel.img, 'repeat')
  }

  moveStart(x: number, y: number) {
    this.points.push({x: x, y: y})
    this.drawPath.moveTo(x, y)
    this.drawCurrentPathModel()
  }

  moveUpdate(x: number, y: number) {
    let lastPoint = this.points[this.points.length - 1]
    this.points.push({x: x, y: y})
    this.drawPath.quadraticCurveTo((x + lastPoint.x) / 2, (y + lastPoint.y) / 2, x, y)
    this.drawCurrentPathModel()
  }

  moveEnd() {
    this.points = []
    this.drawPath = new Path2D()
  }

  clearPath() {
    this.points = []
    this.drawPath = new Path2D()
    this.context.clearRect(0, 0, this.canvasWidth, this.canvasHeight)
  }

  canvasAreaChange(area: Area) {
    this.canvasHeight = area.height as number
    this.canvasWidth = area.width as number
  }

  private drawCurrentPathModel() {
    this.context.globalCompositeOperation = 'source-over'
    this.context.lineWidth = this.drawModel.lineWidth
    if (this.drawModel.pathType == DrawPathType.pen) {
      this.context.strokeStyle = this.drawModel.color
    } else {
      if (this.pattern) {
        this.context.strokeStyle = this.pattern
      }
    }
    this.context.lineJoin = 'round'
    this.context.stroke(this.drawPath)
  }

}
//DrawViewModel文件
import { DrawPathType, DrawViewModel } from '../viewmodel/DrawViewModel'

@Component
export struct DrawView {

  @ObjectLink viewModel: DrawViewModel

  build() {
    Column({ space: 8 }) {
      Text('请选择画笔类型')
        .fontColor(Color.Red)
        .textAlign(TextAlign.Center)
        .fontSize(30)
        .width('80%')
        .margin('10%')

      Canvas(this.viewModel.context)
        .width('100%')
        .height('75%')
        .onAreaChange((oldValue: Area, newValue: Area) => {
          this.viewModel.canvasAreaChange(newValue)
         })
        .gesture(
          GestureGroup(GestureMode.Exclusive,
            PanGesture()
              .onActionStart((event: GestureEvent) => {
                let finger: FingerInfo = event.fingerList[0];
                if (finger == undefined) { return }
                let x = finger.localX
                let y = finger.localY
                this.viewModel.moveStart(x,y)
              })
              .onActionUpdate((event: GestureEvent) => {
                let finger: FingerInfo = event.fingerList[0];
                if (finger == undefined) { return }
                let x = finger.localX
                let y = finger.localY
                this.viewModel.moveUpdate(x,y)
              })
              .onActionEnd((event: GestureEvent) => {
                let finger: FingerInfo = event.fingerList[0];
                if (finger == undefined) { return }
                this.viewModel.moveEnd()
              })
          )
        )

      Flex({direction: FlexDirection.Row, justifyContent: FlexAlign.SpaceAround }) {
        Button('普通画笔')
          .onClick(() => {
            this.viewModel.drawModel.pathType = DrawPathType.pen
          })

        Button('马赛克画笔')
          .onClick(() => {
            this.viewModel.drawModel.pathType = DrawPathType.pattern
          })

        Button('清除内容')
          .onClick(() => {
            this.viewModel.clearPath()
          })
      }
    }
  }
}

上面代码是遮罩笔的demo,关键点: 其实就是设置 RenderingContext 线条的颜色,线条支持image,代码中canvas.png文件在和pages平级的目录Images中,DrawView.ets文件在pages目录内的components目录中,DrawViewModel.ets文件在pages目录内的viewmodel目录中,另外裁剪框手动调整大小可以看下这个ImageKnife库:https://gitee.com/openharmony-tpc/ImageKnife#scaletype类型展示效果

更多关于HarmonyOS 鸿蒙Next有录屏所展示的对图片裁剪以及遮罩笔相关的demo吗的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html


HarmonyOS 鸿蒙Next确实包含了与图片裁剪及遮罩笔相关的功能演示,但具体是否以录屏形式直接展示demo,需参考官方发布的最新资料库或开发者文档。鸿蒙系统在其开发框架中提供了丰富的UI组件和图形处理能力,支持图片裁剪、遮罩效果等图像处理功能。

对于图片裁剪,鸿蒙提供了相关的API接口,允许开发者在应用中实现自定义的图片裁剪逻辑。这些API通常与图像加载、显示和处理库相结合,使得开发者能够轻松地集成图片裁剪功能。

至于遮罩笔(通常指用于绘制遮罩效果的工具),鸿蒙系统也提供了相应的图形绘制能力。开发者可以利用Canvas类及其相关方法,结合自定义的遮罩图像或形状,来实现遮罩效果。这些功能在鸿蒙的UI框架中得到了良好的支持,使得开发者能够创建出具有丰富视觉效果的界面。

为了获取最准确的信息和示例代码,建议直接查阅鸿蒙系统的官方文档或示例项目。如果问题依旧没法解决请联系官网客服,官网地址是:https://www.itying.com/category-93-b0.html

回到顶部