HarmonyOS图片,鸿蒙HarmonyOS三方件开发指南-cropper图片裁剪

本文介绍了HarmonyOS中cropper组件的使用方法和开发实现。cropper组件是一个图像裁剪工具,用于在图像上显示可调整大小的裁剪窗口。首先,通过在布局文件中添加组件并设置点击事件来实现裁剪功能。然后,详细解释了CropImage类的实现,包括初始化裁剪窗口、绘制裁剪窗口、处理触摸事件以及获取裁剪后图像的方法。最后,指导如何编译HAR包并提供了项目源代码链接。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

6ae442411329ba04d0f7a4f554644e4d.png

1. cropper组件功能介绍

1.1. 功能介绍:

cropper组件是一种图像裁剪工具,在图像上显示一个可调整大小的裁剪窗口。

1.2. 模拟器上运行效果:

16403b97e533d7fba6c4d247a814f552.png

5cdce982adb9e0f6a05d63952a83285f.png

2003d4320083f9f9075224de9126a657.png

2. cropper使用方法

2.1. 新建工程,增加组件Har包依赖

在应用模块中添加HAR,只需要将cropperlib-debug.har复制到entry\libs目录下即可(由于build.gradle中已经依赖的libs目录下的*.har,因此不需要再做修改)。

2.2. 修改主页面的布局文件

修改主页面的布局文件ability_main.xml,增加com.crop.cropperlib.CropImage组件。

xmlns:ohos="http://schemas.huawei.com/res/ohos"

ohos:height="match_parent"

ohos:width="match_parent"

ohos:orientation="vertical"

ohos:scrollbar_fading_delay="0">

ohos:id="$+id:cropImage"

ohos:width="match_content"

ohos:height="match_content"

ohos:layout_alignment="center"

ohos:top_margin="20vp"

ohos:image_src="$media:mantis.jpg"

/>

ohos:id="$+id:cropButton"

ohos:width="match_content"

ohos:height="match_content"

ohos:text="crop image"

ohos:text_size="19fp"

ohos:text_color="#FFFFFF"

ohos:top_padding="8vp"

ohos:bottom_padding="8vp"

ohos:right_padding="70vp"

ohos:left_padding="70vp"

ohos:background_element="$graphic:background_button"

ohos:top_margin="20vp"

ohos:layout_alignment="center"/>

ohos:id="$+id:image"

ohos:width="match_content"

ohos:height="match_content"

ohos:layout_alignment="horizontal_center"

ohos:top_margin="20vp"

ohos:image_src="$media:black.jpg"/>

2.3. 修改MainAbilitySlince的UI加载代码

MainAbilitySlince类的onStart方法。

@Override

publicvoid onStart(Intent intent) {

super.onStart(intent);

super.setUIContent(ResourceTable.Layout_ability_main);

Button cropButton = (Button) findComponentById(ResourceTable.Id_cropButton);

cropButton.setClickedListener(new Component.ClickedListener() {

@Override

publicvoid onClick(Component component) {

Image image = (Image) findComponentById(ResourceTable.Id_image);

CropImage cropImage = (CropImage) findComponentById(ResourceTable.Id_cropImage);

PixelMap pixelMap = cropImage.getCroppedImage();

image.setPixelMap(pixelMap);

}

});

}

3. cropper组件开发实现

3.1. 新建一个Module

新建一个Module,类型选择HarmonyOS Library,模块名为library。

3.2. 新建一个CropImage类

新建一个CropImage类,继承自Image类,实现Component.TouchEventListener, Component.DrawTask, Component.LayoutRefreshedListener接口,代码如下:

(1)用于初始化时设置裁剪窗口参数:

@Override

publicvoid onRefreshed(Component component) {

initCropWindow();

}

(2)用于绘制裁剪窗口:

@Override

publicvoid onDraw(Component component, Canvas canvas) {

drawDarkenedSurroundingArea(canvas);

drawGuidelines(canvas);

drawBorder(canvas);

}

(3)设置监听触摸事件,用于修改裁剪窗口:

@Override

publicboolean onTouchEvent(Component component, TouchEvent touchEvent) {

switch (touchEvent.getAction()) {

caseTouchEvent.PRIMARY_POINT_DOWN:

MmiPoint point1 = touchEvent.getPointerPosition(touchEvent.getIndex());

onActionDown(point1.getX() - topLeftX, point1.getY() - topLeftY);

invalidate();

returntrue;

caseTouchEvent.PRIMARY_POINT_UP:

caseTouchEvent.CANCEL:

onActionUp();

invalidate();

returntrue;

caseTouchEvent.POINT_MOVE:

MmiPoint point2 = touchEvent.getPointerPosition(touchEvent.getIndex());

onActionMove(point2.getX() - topLeftX, point2.getY() - topLeftY);

invalidate();

returntrue;

default:

returnfalse;

}

}

(4)根据裁剪窗口获取裁剪后的图像

publicPixelMap getCroppedImage() {

floatscaleX = getScaleX();

floatscaleY = getScaleY();

floattransX = getTranslationX();

floattransY = getTranslationY();

floatpixelMapLeft = (transX 

floatpixelMapTop = (transY 

floatcropX = (pixelMapLeft + Frame.LEFT.getCoordinate()) / scaleX;

floatcropY = (pixelMapTop + Frame.TOP.getCoordinate()) / scaleY;

PixelMap originalPixelMap = this.getPixelMap();

Sizesize= originalPixelMap.getImageInfo().size;

floathideX = 0f;

if (size.width > getWidth()) {

hideX = (size.width - getWidth()) / 2f;

}

floathideY = 0f;

if (size.height > getHeight()) {

hideY = (size.height - getHeight()) / 2f;

}

floatcropWidth = Math.min(Frame.getWidth() / scaleX,size.width - cropX);

floatcropHeight = Math.min(Frame.getHeight() / scaleY,size.height - cropY);

Rect cropRect = new Rect((int)(cropX + hideX), (int)(cropY + hideY),(int)cropWidth, (int)cropHeight);

PixelMap.InitializationOptions options = new PixelMap.InitializationOptions();

options.size= newSize(cropRect.width, cropRect.height);

returnPixelMap.create(originalPixelMap, cropRect, options);

}

3.3. 编译HAR包

利用Gradle可以将HarmonyOS Library库模块构建为HAR包,构建HAR包的方法如下:

在Gradle构建任务中,双击PackageDebugHar或PackageReleaseHar任务,构建Debug类型或Release类型的HAR。

待构建任务完成后,可以在cropperlib > bulid > outputs > har目录中,获取生成的HAR包。

项目源代码地址:https://github.com/isoftstone-dev/cropper

欢迎交流:HWIS-HOS@isoftstone.com

【编辑推荐】

【责任编辑:jianghua TEL:(010)68476606】

点赞 0

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值