mfc 橡皮筋类绘制矩形_自定义View入门-绘制基础(1)

前言

说道自定义View,我们一定会想到,自定义View的绘制流程

  • 测量阶段(measure)
  • 布局阶段(layout)
  • 绘制阶段(draw)

我们看到的一些炫酷的view效果,都是在绘制方法里去实现的, 也就是draw(Canvas), 我们先放下 测量与布局, 先从绘制基础开始学起。

详解

说到ondraw(Canvas)方法,不得不提Paint与Canvas。我们先来看Paint

1.Paint

Paint就是"画笔",我们先去看下Paint类的源码解释:

** * The Paint class holds the style and color information about how to draw * geometries, text and bitmaps. */

Paint类可以画几何图形,文本与bitmap。Paint类方法比较多, 这里拿Paint.Style举例:

  • Paint.Style.FILL:填充内部
  • Paint.Style.FILLANDSTROKE :填充内部和描边
  • Paint.Style.STROKE :描边
d564e27cb57b2a08ac7fe2f5427f4f89.png

2.Canvas

(1).定义Canvas就是“画布”,我们先去看下Canvas类的源码解释:

 * The Canvas class holds the "draw" calls. To draw something, you need * 4 basic components: A Bitmap to hold the pixels, a Canvas to host * the draw calls (writing into the bitmap), a drawing primitive (e.g. Rect, * Path, text, Bitmap), and a paint (to describe the colors and styles for the * drawing).
  • 承载像素的位图
  • 持有绘画方法调用的画布
  • 描述画图颜色和风格的画笔
  • 画图的类型。

(2).绘制方法

方法比较多了,这里我就随便举几个例子:

  • 画线
 Paint paint=new Paint(); paint.setColor(Color.BLUE); paint.setStrokeWidth(20); paint.setStyle(Paint.Style.FILL); canvas.drawLine(200,200,450,200,paint);
67fe1343695a7bd94313c74805d9cfb7.png
  • 画矩形
 Paint paint=new Paint(); paint.setColor(Color.BLUE); paint.setStrokeWidth(50); paint.setStyle(Paint.Style.FILL ); canvas.drawRect(100,100,200,200,paint);
1634ef5c2b8ea57ec979d8825ea29b33.png
  • 画扇形-140度
 Paint paint=new Paint(); paint.setColor(Color.BLUE); paint.setStrokeWidth(50); paint.setStyle(Paint.Style.FILL ); canvas.drawArc(100,100,400,400,0,140,false,paint);
31bd4c36cba138b1166f3c482fc7fbd9.png

更多的方法以及方法的含义可以去下面的API地址去看!https://developer.android.google.cn/reference/android/graphics/Canvas.html

今天就讲到这里 ,绘制基础还有一个非常重要的类,Paht(路径)类,下一节讲一下。希望对大家有所帮助!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值