【Android绘图】绘图之基础篇(一)

栏目: Android · 发布时间: 5年前

内容简介:自定义主要的方法:我们来看看效果

自定义 view 总是绕不开 onDraw ,而 onDraw 则少不了 PaintCanvas ,这里我们就说说这个。

Paint

主要的方法:

paint.setColor(Color.RED); //设置画笔颜色 
paint.setAntiAlias(true);//抗锯齿功能
paint.setStyle(Style.FILL);//设置填充样式
paint.setStrokeWidth(30);//设置画笔宽度
paint.setTextSize();//设置字体大小
复制代码
  • setStyle 主要有 FILLSTROKEFILL_AND_STROKE

我们来看看效果

Paint paint = new Paint();
        paint.setColor(Color.RED);
        paint.setStrokeWidth(10);
        paint.setStyle(Paint.Style.FILL);
        canvas.drawCircle(200, 300, 100, paint);
        paint.setStyle(Paint.Style.STROKE);
        canvas.drawCircle(500, 300, 100, paint);
        paint.setStyle(Paint.Style.FILL_AND_STROKE);
        canvas.drawCircle(800, 300, 100, paint);
复制代码
【Android绘图】绘图之基础篇(一)

FILLFILL_AND_STROKE 貌似是一样的?我们将 strokeWidth 设置为 100

【Android绘图】绘图之基础篇(一)
可以明显的看到第2、3个圆变大了, STROKEFILL_AND_STROKE 都会为圆加上 strokeWidth/2

Canvas

  • 画直线

drawLine(float startX, float startY, float stopX, float stopY,Paint paint)

paint.setColor(Color.RED);
         canvas.drawLine(100, 100, 200, 300, paint);
复制代码

drawLines(float[] pts, Paint paint) 其中,pts 长度必须是 4个倍数

paint.setColor(Color.BLACK);
        float[] pts = {50, 50, 150, 50,
                150, 50, 150, 150,
                150, 150, 250, 150,
                250, 150, 250, 250};
        canvas.drawLines(pts, paint);
复制代码

需要注意的是,100,150 为第1个点,300,250为第2个点,第1、2个点连线,3、4个点连线

【Android绘图】绘图之基础篇(一)

drawLines(float[] pts, int offset, int count,Paint paint) offset 表示起始位置 count 表示从起始位置画多少个点,必须是4个倍数

float[] pts = {50, 50, 150, 50,
                150, 50, 150, 150,
                150, 150, 250, 150,
                250, 150, 250, 250};
        paint.setColor(Color.BLACK);
        canvas.drawLines(pts, 0, 16, paint);
复制代码

即和第二步中效果一致,我们修改 offset 为 4, count 为 12,效果为下图

【Android绘图】绘图之基础篇(一)
  • 画矩形

drawRect (float left, float top, float right, float bottom, Paint paint)

canvas.drawRect(100, 200, 400, 800, paint);
复制代码

其代表的坐标点如下图所示

【Android绘图】绘图之基础篇(一)

drawRect (RectF rect, Paint paint) drawRect (Rect r, Paint paint) 这2个其实是一样的,left、top、right、bottom和第1个是一致的

public Rect(int left, int top, int right, int bottom) {
        this.left = left;
        this.top = top;
        this.right = right;
        this.bottom = bottom;
    }
复制代码
public RectF(float left, float top, float right, float bottom) {
        this.left = left;
        this.top = top;
        this.right = right;
        this.bottom = bottom;
    }
复制代码
  • 圆角矩形

drawRoundRect(RectF rect, float rx, float ry, Paint paint) 其中,rx代表X轴圆角半径,ry代表Y轴圆角半径

RectF rectF = new RectF(100, 200, 400, 800);
        canvas.drawRoundRect(rectF, 100, 200, paint);
复制代码
【Android绘图】绘图之基础篇(一)

drawRoundRect(float left, float top, float right, float bottom, float rx, float ry,Paint paint) 同矩形,rx、ry同方法1

  • 圆形

drawCircle(float cx, float cy, float radius, Paint paint) 其中,cx为圆心X轴坐标,cy为圆心Y轴坐标,radius为半径

canvas.drawCircle(100, 200, 50, paint);
复制代码
【Android绘图】绘图之基础篇(一)
  • 椭圆 drawOval(RectF oval, Paint paint) drawOval(float left, float top, float right, float bottom, Paint paint) 同矩形
RectF rectF = new RectF(100, 200, 400, 800);
        canvas.drawOval(rectF, paint);
复制代码
【Android绘图】绘图之基础篇(一)
  • 弧线

drawArc(RectF oval, float startAngle, float sweepAngle, boolean useCenter,Paint paint) drawArc(float left, float top, float right, float bottom, float startAngle, float sweepAngle, boolean useCenter, Paint paint)

startAngle 弧开始的角度,以X轴正方向为0度 sweepAngle 弧持续的角度 useCenter 是否有弧的两边,true 闭合的,false 非闭合的

paint.setStyle(Paint.Style.STROKE);

        RectF rect = new RectF(50, 100, 200, 400);
        canvas.drawArc(rect, 0, 90, true, paint);

        
        RectF rectF = new RectF(100, 200, 400, 800);
        canvas.drawArc(rectF, 0, 90, false, paint);
复制代码
【Android绘图】绘图之基础篇(一)
  • 示例 这里就不贴代码了,主要用的画线、矩形和圆,再加上一些位置计算
    【Android绘图】绘图之基础篇(一)

参考资料: 自定义控件之绘图篇(一):概述及基本几何图形绘制


以上所述就是小编给大家介绍的《【Android绘图】绘图之基础篇(一)》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

C专家编程

C专家编程

[美] Peter Vander Linde / 徐波 / 人民邮电出版社 / 2002-12 / 40.00元

《C专家编程》展示了最优秀的C程序员所使用的编码技巧,并专门开辟了一章对C++的基础知识进行了介绍。 书中对C的历史、语言特性、声明、数组、指针、链接、运行时、内存,以及如何进一步学习C++等问题作了细致的讲解和深入的分析。全书撷取几十几个实例进行讲解,对C程序员具有非常高的实用价值。 这本《C专家编程》可以帮助有一定经验的C程序员成为C编程方面的专家,对于具备相当的C语言基础的程序员......一起来看看 《C专家编程》 这本书的介绍吧!

CSS 压缩/解压工具
CSS 压缩/解压工具

在线压缩/解压 CSS 代码

随机密码生成器
随机密码生成器

多种字符组合密码

Markdown 在线编辑器
Markdown 在线编辑器

Markdown 在线编辑器