通过PixelMap方法对图片进行编辑裁剪:

  • PixelMap.crop方法,可以根据输入的尺寸对图片进行裁剪。
  • PixelMap.opacity方法,可以通过设置透明比率对图片设置透明效果。
  • PixelMap.scale方法,可以根据输入的宽高对图片进行缩放。
  • PixelMap.rotate方法,可以根据输入的角度对图片进行旋转。
  • PixelMap.flip方法,可以根据输入的条件对图片进行翻转。

以下示例代码为PixelMap.crop图片裁剪方法的使用:

class RegionItem {
  x: number;
  y: number;
  constructor(x: number, y: number) {
    this.x = x;
    this.y = y;
  }
}

export async function cropCommon(pixelMap: PixelMap, cropWidth: number, cropHeight: number, cropPosition: RegionItem) {
  pixelMap.crop({
    size: {
      width: cropWidth,
      height: cropHeight
    },
    x: cropPosition.x,
    y: cropPosition.y
  });
}

export async function banner(pixelMap: PixelMap, width: number, height: number) {
  if (width <= height) {
    const cropWidth = width;
    const cropHeight = Math.floor(width * 0.75);
    const cropPosition = new RegionItem(0, Math.floor((height - cropHeight) / 2));
    cropCommon(pixelMap, cropWidth, cropHeight, cropPosition);
    return;
  }
  if (width * 0.75 >= height) {
    const cropWidth = Math.floor(height / 0.75);
    const cropHeight = height;
    const cropPosition = new RegionItem(Math.floor((width - cropWidth) / 2), 0);
    cropCommon(pixelMap, cropWidth, cropHeight, cropPosition);
    return;
  }
  const cropWidth = width;
  const cropHeight = Math.floor(width * 0.75);
  const cropPosition = new RegionItem(0, Math.floor((height - cropHeight) / 2));
  cropCommon(pixelMap, cropWidth, cropHeight, cropPosition);
}

 

Logo

社区规范:仅讨论OpenHarmony相关问题。

更多推荐