這篇文章運(yùn)用簡(jiǎn)單易懂的例子給大家介紹鴻蒙自定義組件之鴻蒙畫(huà)板的概述,內(nèi)容非常詳細(xì),感興趣的小伙伴們可以參考借鑒,希望對(duì)大家能有所幫助。
創(chuàng)新互聯(lián)公司主要從事網(wǎng)頁(yè)設(shè)計(jì)、PC網(wǎng)站建設(shè)(電腦版網(wǎng)站建設(shè))、wap網(wǎng)站建設(shè)(手機(jī)版網(wǎng)站建設(shè))、成都響應(yīng)式網(wǎng)站建設(shè)公司、程序開(kāi)發(fā)、網(wǎng)站優(yōu)化、微網(wǎng)站、微信小程序開(kāi)發(fā)等,憑借多年來(lái)在互聯(lián)網(wǎng)的打拼,我們?cè)诨ヂ?lián)網(wǎng)網(wǎng)站建設(shè)行業(yè)積累了豐富的成都做網(wǎng)站、網(wǎng)站建設(shè)、網(wǎng)站設(shè)計(jì)、網(wǎng)絡(luò)營(yíng)銷經(jīng)驗(yàn),集策劃、開(kāi)發(fā)、設(shè)計(jì)、營(yíng)銷、管理等多方位專業(yè)化運(yùn)作于一體。初識(shí)鴻蒙OS 2.0
華為的鴻蒙OS 2.0是目前唯一個(gè)有希望和安卓、IOS對(duì)抗的全新生態(tài)系統(tǒng)。9月10日,在東莞正式發(fā)布。華為喊出了“HarmonyOS 2.0 連接無(wú)限可能”的口號(hào),將是未來(lái)十年很有競(jìng)爭(zhēng)力的優(yōu)秀操作系統(tǒng)。
自定義Component
這里我編寫(xiě)一個(gè)簡(jiǎn)易的畫(huà)板。
1.新建一個(gè)類DrawComponment 繼承自Componment;
2.實(shí)現(xiàn)Component.TouchEventListener,用于對(duì)touch事件生成相應(yīng)的path;
3.實(shí)現(xiàn)Component.DrawTask,用于把path畫(huà)到屏幕上;
代碼
DrawComponment
package com.quqx.draw; import ohos.agp.components.Component; import ohos.agp.render.Canvas; import ohos.agp.render.Paint; import ohos.agp.render.Path; import ohos.agp.utils.Color; import ohos.agp.utils.Point; import ohos.app.Context; import ohos.hiviewdfx.HiLog; import ohos.hiviewdfx.HiLogLabel; import ohos.media.image.PixelMap; import ohos.multimodalinput.event.MmiPoint; import ohos.multimodalinput.event.TouchEvent; public class DrawComponment extends Component implements Component.DrawTask, Component.TouchEventListener { private static final String TAG = "DrawComponment"; PixelMap mPixelMap; Canvas mCanvas; Path mPath = new Path(); Paint mPaint; Point mPrePoint = new Point(); Point mPreCtrlPoint = new Point(); public DrawComponment(Context context) { super(context); //初始化paint mPaint = new Paint(); mPaint.setColor(Color.WHITE); mPaint.setStrokeWidth(5f); mPaint.setStyle(Paint.Style.STROKE_STYLE); //添加繪制任務(wù) addDrawTask(this::onDraw); //設(shè)置TouchEvent監(jiān)聽(tīng) setTouchEventListener(this::onTouchEvent); } @Override public void onDraw(Component component, Canvas canvas) { canvas.drawPath(mPath, mPaint); } @Override public boolean onTouchEvent(Component component, TouchEvent touchEvent) { switch (touchEvent.getAction()) { case TouchEvent.PRIMARY_POINT_DOWN: { //鴻蒙Log工具 HiLog.debug(new HiLogLabel(0, 0, TAG), "TouchEvent.PRIMARY_POINT_DOWN"); //獲取點(diǎn)信息 MmiPoint point = touchEvent.getPointerPosition(touchEvent.getIndex()); mPath.reset(); mPath.moveTo(point.getX(), point.getY()); mPrePoint.position[0] = point.getX(); mPrePoint.position[1] = point.getY(); mPreCtrlPoint.position[0] = point.getX(); mPreCtrlPoint.position[1] = point.getY(); //PRIMARY_POINT_DOWN 一定要返回true return true; } case TouchEvent.PRIMARY_POINT_UP: break; case TouchEvent.POINT_MOVE: { HiLog.debug(new HiLogLabel(0, 0, TAG), "TouchEvent.POINT_MOVE"); MmiPoint point = touchEvent.getPointerPosition(touchEvent.getIndex()); Point currCtrlPoint = new Point((point.getX() + mPrePoint.position[0]) / 2, (point.getY() + mPrePoint.position[1]) / 2); //繪制三階貝塞爾曲線 mPath.cubicTo(mPrePoint, mPreCtrlPoint, currCtrlPoint); mPreCtrlPoint.position[0] = currCtrlPoint.position[0]; mPreCtrlPoint.position[1] = currCtrlPoint.position[1]; mPrePoint.position[0] = point.getX(); mPrePoint.position[1] = point.getY(); //更新顯示 invalidate(); break; } } return false; } }