91超碰碰碰碰久久久久久综合_超碰av人澡人澡人澡人澡人掠_国产黄大片在线观看画质优化_txt小说免费全本

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

鴻蒙自定義組件之鴻蒙畫板的概述

發布時間:2020-11-02 16:27:43 來源:億速云 閱讀:411 作者:Leah 欄目:開發技術

這篇文章運用簡單易懂的例子給大家介紹鴻蒙自定義組件之鴻蒙畫板的概述,內容非常詳細,感興趣的小伙伴們可以參考借鑒,希望對大家能有所幫助。

初識鴻蒙OS 2.0

華為的鴻蒙OS 2.0是目前唯一個有希望和安卓、IOS對抗的全新生態系統。9月10日,在東莞正式發布。華為喊出了“HarmonyOS 2.0 連接無限可能”的口號,將是未來十年很有競爭力的優秀操作系統。

自定義Component

這里我編寫一個簡易的畫板。

1.新建一個類DrawComponment 繼承自Componment;
2.實現Component.TouchEventListener,用于對touch事件生成相應的path;
3.實現Component.DrawTask,用于把path畫到屏幕上;

代碼

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);
    //添加繪制任務
    addDrawTask(this::onDraw);
    //設置TouchEvent監聽
    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");
        //獲取點信息
        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;
  }
}

MainAbilitySlice

package com.quqx.draw.slice;

import com.quqx.draw.DrawComponment;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;

import ohos.agp.components.DirectionalLayout;
import ohos.agp.components.DirectionalLayout.LayoutConfig;
import ohos.agp.components.Text;
import ohos.agp.colors.RgbColor;
import ohos.agp.components.element.ShapeElement;
import ohos.agp.utils.Color;
import ohos.agp.utils.TextAlignment;

public class MainAbilitySlice extends AbilitySlice {

  private DirectionalLayout myLayout = new DirectionalLayout(this);

  @Override
  public void onStart(Intent intent) {
    super.onStart(intent);
    LayoutConfig config = new LayoutConfig(LayoutConfig.MATCH_PARENT, LayoutConfig.MATCH_PARENT);
    myLayout.setLayoutConfig(config);

    DrawComponment drawComponment = new DrawComponment(this);
    drawComponment.setLayoutConfig(config);
    ShapeElement element = new ShapeElement();
    element.setRgbColor(new RgbColor(0, 0, 0));
    drawComponment.setBackground(element);
    myLayout.addComponent(drawComponment);

    super.setUIContent(myLayout);
  }

  @Override
  public void onActive() {
    super.onActive();
  }

  @Override
  public void onForeground(Intent intent) {
    super.onForeground(intent);
  }
}

關于鴻蒙自定義組件之鴻蒙畫板的概述就分享到這里了,希望以上內容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

泰兴市| 舟曲县| 汉阴县| 谷城县| 岑巩县| 庆安县| 仲巴县| 辉南县| 旺苍县| 绥阳县| 鹿泉市| 太和县| 东方市| 安顺市| 景泰县| 闵行区| 香河县| 云和县| 临猗县| 巨鹿县| 锡林浩特市| 元阳县| 濮阳市| 揭西县| 稷山县| 牙克石市| 且末县| 宿州市| 克东县| 剑川县| 鄂州市| 高平市| 思茅市| 乌拉特中旗| 潞城市| 大宁县| 绥中县| 宜川县| 灌云县| 张家川| 五大连池市|