鸿蒙自定义组件以及流程问题 原创

飞中缘a
发布于 2021-3-25 17:37
1.8w浏览
3收藏

1、自定义类继承Component


2、添加构造函数,对应的几个构造

3、添加测量函数onMeasure对应onEstimateSize,实现Component.EstimateSizeListener

4、添加计算函数onLayout对应onArrange,实现ComponentContainer.ArrangeListener

5、添加绘制函数onDraw不变,实现Component.DrawTask

之后就可以实现具体的逻辑了

 


代码如下:

public class TextDemo extends Component implements Component.DrawTask, Component.EstimateSizeListener, ComponentContainer.ArrangeListener {
   private Paint mPaint = new Paint();
   private int color;
   private RectFloat mRectFloat;

   {
       /**
        * 防抖动
        */
       mPaint.setDither(true);
       /**
        * 抗锯齿,降低分辨率,提高绘制效率
        */
       mPaint.setAntiAlias(true);
   }

   public TextDemo(Context context) {
       this(context, null);
   }

   public TextDemo(Context context, AttrSet attrSet) {
       this(context, attrSet, null);
   }

   public TextDemo(Context context, AttrSet attrSet, String styleName) {
       super(context, attrSet, styleName);
       initialize(attrSet);
       setEstimateSizeListener(this);
       addDrawTask(this);
   }

   /**
    * 获取自定义属性
    *
    * @param attrs
    */
   private void initialize(AttrSet attrs) {
       color = attrs.getAttr("text_color").get().getIntegerValue();

   }


   /**
    * 计算
    *
    * @param i
    * @param i1
    * @param i2
    * @param i3
    * @return
    */
   @Override
   public boolean onArrange(int i, int i1, int i2, int i3) {
//        do something
       return false;
   }

   /**
    * 测量
    *
    * @param i
    * @param i1
    * @return
    */
   @Override
   public boolean onEstimateSize(int i, int i1) {
//        do something
       return false;
   }

   /**
    * 绘制
    *
    * @param component
    * @param canvas
    */
   @Override
   public void onDraw(Component component, Canvas canvas) {
//        do something
       canvas.drawRect(mRectFloat, mPaint);
   }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.
  • 77.
  • 78.
  • 79.
  • 80.
  • 81.
  • 82.

©著作权归作者所有,如需转载,请注明出处,否则将追究法律责任
已于2021-3-26 16:28:00修改
2
收藏 3
回复
举报
2
5
3
5条回复
按时间正序
/
按时间倒序
麒麟Berlin
麒麟Berlin

请问view.draw(canvas)怎么替代

回复
2021-3-25 20:06:28
Tuer白晓明
Tuer白晓明

如果能够再详细点就更好了~

回复
2021-3-26 08:39:35
飞中缘a
飞中缘a 回复了 麒麟Berlin
请问view.draw(canvas)怎么替代

onDraw(Component component, Canvas canvas) 

用这个

回复
2021-3-26 09:48:01
白鹿白鹿
白鹿白鹿

简单明了

回复
2021-3-26 14:33:22
飞中缘a
飞中缘a 回复了 白鹿白鹿
简单明了

感谢支持!

回复
2021-3-26 16:27:20


回复
    相关推荐