Android自定义控件之基本图形绘制
入门示例代码
在Android中Paint类就是画笔,Canvas 就是画布
对于画笔的大小,粗细,颜色,透明度都在paint类中设置,对于画出的成品,比如圆,方形,等在canvas类中的函数生成
入门案例:
package com.loaderman.customviewdemo.paint; import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;
public class BasisView extends View {
public BasisView(Context context) {
super(context);
} public BasisView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
} public BasisView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
} @Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
Paint paint = new Paint();//画笔
paint.setColor(Color.RED);//设置颜色
paint.setStyle(Paint.Style.STROKE); // 填充样式//Paint.Style.FILL填充内部 Paint.Style.FILL_AND_STROKE 填充内部和描边 Paint.Style.STROKE 仅描边
paint.setStrokeWidth(50);//描边宽度值
canvas.drawCircle(190,200,150,paint);//绘制圆
}
}
画笔的基本设置
- setColor(int color)设置画笔颜色
参数color由0xAARRGGBB 组成 ,A代表透明度,R代表红色值,G代表绿色值,B代表蓝色值
- setStyle(tStyle style)设置填充样式
参数styl取值如下:
- Paint.Style.FILL 填充内部
- Paint.Style.FILL_AND_STROKE 填充内部和描边
- Paint.Style.STROKE 仅描边
- setStrokeWidth(float width)
width参数单位是px,当画笔的样式不为FILl时有效
- setAntiAlias(boolean aa)
是否打开抗锯齿功能,一般在绘制不规则的图形中使用,比如圆文字等,在绘制菱角分明的图形中是不需要开启的
canvas基本使用
画布背景设置
drawColor(int color)
drawARGB(int a, int r, int g, int b)
drawRGB(int r, int g, int b)
画一条直线
drawLine(float startX, float startY, float stopX, float stopY, Paint paint)
//画一条直线
如 canvas.drawLine(100,100,200,200,paint);
注意:画直线的错写和画布的style是没有关系
画多条直线
drawLines(float[] pts, Paint paint)
//pts是点的集合,这里不是形成连接线,而是每两个点形成两条直线,ps的组织方式为{x1,y1,x2,y2,x3,y3.....}
//如:
float[] pts ={10,10,100,100,200,200,400,400};
canvas.drawLines(pts,paint);//效果是两条直线
drawLines(float[] pts, int offset, int count, Paint paint)
//如:
float[] pts ={10,10,100,100,200,200,400,400};
canvas.drawLines(pts,2,4,paint);
//表示从pts数据中索引问的数组开始绘制,有4个数组参与绘图,也就是点100,100 和点200,200 效果就是这两个点的直线
画点
drawPoint(float x, float y, Paint paint) //点x代表x坐标 y代表点y坐标
//如:
canvas.drawPoint(100,100,paint);
画多个点
drawPoints(float[] pts, Paint paint)
drawPoints(float[] pts, int offset, int count, Paint paint)
//说明:pts为点的集合 offset指集合中跳过数值的个数,不是点的个数,点的个数有二个数值
//如:
float[] pts ={10,10,100,100,200,200,400,400};
canvas.drawPoints(pts,paint);
canvas.drawPoints(pts,2,4,paint);
画矩形
Rect构造函数
Rect()
Rect(int left, int top, int right, int bottom)
Rect(Rect r)
RectF构造函数
public RectF()
public RectF(float left, float top, float right, float bottom)
public RectF(RectF r)
public RectF(Rect r)
其实这两个构造函数基本相同只是参数的数据类型不同而已
构造矩形结构一般如下
//直接构造
canvas.drawRect(10, 10, 100, 100, paint);
//间接构造
Rect rect = new Rect();
rect.set(10,10,100,100)
绘制矩形
public void drawRect(RectF rect, Paint paint) public void drawRect(Rect r, Paint paint)
public void drawRect(float left, float top, float right, float bottom, Paint paint)
绘制圆角矩形
drawRoundRect(RectF rect, float rx, float ry, Paint paint)
drawRoundRect(float left, float top, float right, float bottom, float rx, float ry, Paint paint)
//其中rx代表x轴的圆角半径,ry代表y轴的圆角半径
绘制圆形
drawCircle(float cx, float cy, float radius, Paint paint)//cx代表x轴坐标,cy代表y轴坐标,radius代表圆的半径
//如
canvas.drawCircle(200,200,100,paint);
绘制椭圆
RectF rectF = new RectF(100,10,300,100);
canvas.drawOval(rectF,paint);
绘制弧
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 代表弧开始的角度
sweepAngle 弧持续的角度
useCenter 是否有弧的两边,为true则有,false只有一条
color
android 定义了很多常量的颜色值可以直接使用
public static final int BLACK = -16777216;
public static final int BLUE = -16776961;
public static final int CYAN = -16711681;
public static final int DKGRAY = -12303292;
public static final int GRAY = -7829368;
public static final int GREEN = -16711936;
public static final int LTGRAY = -3355444;
public static final int MAGENTA = -65281;
public static final int RED = -65536;
public static final int TRANSPARENT = 0;
public static final int WHITE = -1;
public static final int YELLOW = -256;
上面可以通过Color.XXX来直接使用这些颜色
还有其他构造颜色
public static int alpha(int color)) //提取颜色分量 public static int red(int color) ) //提取颜色分量 public static int green(int color)) //提取颜色分量 public static int blue(int color) //提取颜色分量 public static int rgb(int red, int green, int blue) //不带有透明度颜色 public static int rgb(float red, float green, float blue) //不带有透明度颜色 public static int argb(int alpha, int red, int green, int blue) //带有透明度颜色 public static int argb(float alpha, float red, float green, float blue) //带有透明度颜色
Android自定义控件之基本图形绘制的更多相关文章
- 自定义控件之Canvas图形绘制基础练习-青春痘笑脸^_^
对于自定义控件的意义不言而喻,所以对它的深入研究是很有必要的,前些年写过几篇关于UI效果的学习过程,但是中途比较懒一直就停滞了,而对于实际工作还是面试来说系统深入的了解自定义控件那是很有必要的,所以接 ...
- Android自定义控件7--自定义开关--绘制界面内容
本文实现全自定义控件--自定义开关 本文地址:http://www.cnblogs.com/wuyudong/p/5922316.html,转载请注明源地址. 自定义开关 (View),本文完成下面内 ...
- Android 利用SurfaceView进行图形绘制
SurfaceView使用介绍 SurfaceView是View的一个特殊子类,它的目的是另外提供一个线程进行绘制操作. 要使用SurfaceView进行绘制,步骤如下: 1.用SurfaceView ...
- Android 4.0的图形硬件加速及绘制技巧
转:http://zuiniuwang.blog.51cto.com/3709988/721798 从Android 3.0开始,Android 2D的绘制流程就设计为能够更好地支持硬件加速.使用GP ...
- Android自定义控件:图形报表的实现(折线图、曲线图、动态曲线图)(View与SurfaceView分别实现图表控件)
图形报表很常用,因为展示数据比较直观,常见的形式有很多,如:折线图.柱形图.饼图.雷达图.股票图.还有一些3D效果的图表等. Android中也有不少第三方图表库,但是很难兼容各种各样的需求. 如果第 ...
- Android OpenGL ES 开发:绘制图形
OpenGL 绘制图形步骤 上一篇介绍了 OpenGL 的相关概念,今天来实际操作,使用 OpenGL 绘制出图形,对其过程有一个初步的了解. OpenGL 绘制图形主要概括成以下几个步骤: 创建程序 ...
- Android自定义控件
开发自定义控件的步骤: 1.了解View的工作原理 2. 编写继承自View的子类 3. 为自定义View类增加属性 4. 绘制控件 5. 响应用户消息 6 .自定义回调函数 一.Vie ...
- [Android自定义控件] Android自定义控件
转载自:http://blog.163.com/ppy2790@126/blog/static/103242241201382210910473/ 开发自定义控件的步骤: 1.了解View的工作原理 ...
- 【转】Android自定义控件
原文网址:http://blog.163.com/ppy2790@126/blog/static/103242241201382210910473/ 开发自定义控件的步骤: 1.了解View的工作原理 ...
随机推荐
- springboot集成Apollo分布式配置
安装Apollo服务 1.安装mysql 地址:https://www.cnblogs.com/xuaa/p/10782352.html 2.下载Apollo源码到本地 地址:https://gith ...
- element 中表单验证的解析。
https://blog.csdn.net/qq_24504591/article/details/88048894 https://segmentfault.com/a/11900000125513 ...
- JAVA遇见HTML——JSP篇(JSP状态管理)
案例:Cookie在登录中的应用 URL编码与解码的工具类解决中文乱码的问题,这个工具类在java.net.*包里 编码:URLEncoder.encode(String s,String enc)/ ...
- 快速搭建FTP服务器
快速搭建一个本地的FTP服务器 如果需要开发FTP文件上传下载功能,那么需要在本机上搭建一个本地FTP服务器,方便调试.第一步:配置IIS Web服务器1.1 控制面板中找到“程序”并打开 1.2 ...
- HDU 6150 - Vertex Cover | 2017 中国大学生程序设计竞赛 - 网络选拔赛
思路来自 ICPCCamp /* HDU 6150 - Vertex Cover [ 构造 ] | 2017 中国大学生程序设计竞赛 - 网络选拔赛 题意: 给了你一个贪心法找最小覆盖的算法,构造一组 ...
- 指针&虚函数多态性
class Class1 { public: virtual void f() { cout << "Function f() in Class1 \n"; } voi ...
- MNIST 数据集介绍
在学习机器学习的时候,首要的任务的就是准备一份通用的数据集,方便与其他的算法进行比较. MNIST数据集是一个手写数字数据集,每一张图片都是0到9中的单个数字,比如下面几个: MNIST数据库 ...
- Oracle 进程 前台进程-服务进程
一.什么是服务进程(前台进程) 当用户运行一个应用进程时,系统会为用户运行的应用建立一个用户程序,该进程通过某种方式启动一个服务器进程(前台进程),用于处理连接到该实例的用户进程的请求. 二.服务进程 ...
- DOS窗口启动tomact,运用startup.bat/shutdown.bat命令启动/关闭tomcat
设置CATALINA_HOME环境变量1.CATALINA_HOME是TOMCAT安装路径的别名,目的是为了方便使用TOMCAT2.计算机>属性>环境变量, 新建环境变量.变量名为CATA ...
- [Luogu] 网络
https://www.luogu.org/problemnew/show/P3250 树链剖分 + 线段树 + 优先队列 要求未被影响的请求中最大的 所以每次将每条路径在整棵树上的补集的每个节点的优 ...