Android 自定View基础
来源:互联网 发布:java dom4j解析xml 编辑:程序博客网 时间:2024/06/12 22:38
之前只是零散的学习过自定义View的相关知识,过后就忘的差不都了,从今天起,记录下自己学过的自定义View知识,可能写的比较简单
自定义View 起步
创建一个类继承至View ,重写它的构造方法
public class PracticeView extends View { public PracticeView(Context context) { this(context,null); } public PracticeView(Context context, @Nullable AttributeSet attrs) { this(context, attrs,0); } public PracticeView(Context context,@Nullable AttributeSet attrs,int defStyleAttr) { super(context, attrs, defStyleAttr); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); } @Override protected void onLayout(boolean changed, int left, int top, int right, int bottom) { super.onLayout(changed, left, top, right, bottom); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); }}
这就完成了一个简单的自定义View,在OnDraw()方法中就可以根据我们的需求来绘制各种内容了。关于onMeasure()和onLayout()方法之后在讲解,今天只关注第三个方法onDraw();
在自定义View中Canvas 就相当于一个画布,其中绘制图形我们还需要一个重要的工具画笔
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
一般在第三个构造方法中 去new 一个Paint 对象,加上Paint.ANTI_ALIAS_FLAG参数的意思就是开启抗锯齿来让图形和文字的边缘更加平滑,另外,你也可以使用 Paint.setAntiAlias(boolean aa) 来动态开关抗锯齿。抗锯齿的效果如下:
在未开启的时候就有一种毛边的效果,大多数情况下它都应该是开启的;但在极少数的某些时候,你还真的需要把它关闭。
使用Canvas 绘制基础图形
Canvas.drawColor(@ColorInt int color) 颜色填充
整个绘制区域统一涂上指定的颜色。
canvas.drawColor(Color.RED); 将自定义View 所占屏幕区域染成纯红色
类似的方法还有 drawRGB(int r, int g, int b) 和 drawARGB(int a, int r, int g, int b) ,它们和 drawColor(color)作用都是一样的
int a, int r, int g, int b ARGB 是一种色彩模式,也就是RGB色彩模式附加上Alpha(透明度) a 透明度 r RED 红色 g GREEN 绿色 b BLUE 蓝色 通过 r g b 三原色+透明度 来组成各种颜色值
drawCircle(float centerX, float centerY, float radius, Paint paint) 画圆
参数解释 : 1 圆心 x 坐标 ,2 圆心 y 坐标 ,3 圆的半径 4 画笔
paint.setStyle(Paint.Style.FILL); 绘制实心圆 paint.setColor(Color.BLACK); canvas.drawCircle(200,150,100,paint); 绘制一个圆心为(200,150),半径为100,黑色的圆
Paint.setColor(int color) 设置画笔颜色
Paint 最常用的方法之一,用来设置绘制内容的颜色
Paint.setStyle(Style style) 设置绘制模式
- paint.setStyle(Paint.Style.STROKE) 画线模式(即勾边模式) - paint.setStyle(Paint.Style.FILL); 填充模式 - paint.setStyle(Paint.Style.FILL_AND_STROKE); 上述两模式的合集,一起作用
如果你想绘制空心圆(或者叫环形)
paint.setStyle(Paint.Style.STROKE); paint.setColor(Color.BLACK); canvas.drawCircle(500,150,100,paint);
Paint.setStrokeWidth(float width)
设置线条的宽度
在上述代码中加上一句
paint.setStyle(Paint.Style.STROKE); paint.setColor(Color.BLACK); paint.setStrokeWidth(20); // 线条宽度为 20 像素 canvas.drawCircle(500,150,100,paint);
drawRect(float left, float top, float right, float bottom, Paint paint) 画矩形
参数 left 矩形左上角顶点 x坐标 top 矩形左上角顶点 y坐标 right 矩形右下角顶点 x坐标 bottom 矩形右下角顶点 y坐标
paint.setStyle(Paint.Style.FILL); canvas.drawRect(100, 100, 500, 500, paint); paint.setStyle(Paint.Style.STROKE); canvas.drawRect(700, 100, 1100, 500, paint);
重载方法
drawRect(RectF rect, Paint paint) 和 drawRect(Rect rect, Paint paint)
Rect rect = new Rect(100, 100, 500, 500); canvas.drawRect(rect, paint); RectF rectF = new RectF(700, 100, 1100, 500); canvas.drawRect(rectF, paint); //结果和上面一样
drawPoint(float x, float y, Paint paint) 画点
paint.setStrokeCap(Paint.Cap.ROUND); //设置点的形状 圆型 paint.setStrokeWidth(20); //设置点的大小 canvas.drawPoint(200,200,paint);
paint.setStrokeCap(Paint.Cap.BUTT); //设置点的形状 方形 paint.setStrokeWidth(20); //设置点的大小 canvas.drawPoint(400,200,paint);
drawOval(float left, float top, float right, float bottom, Paint paint) 画椭圆
paint.setStyle(Paint.Style.STROKE); RectF rectF= new RectF(200,200,600,400); canvas.drawOval(rectF,paint); // 或者 canvas.drawOval(200,200,600,400); 一样的效果
drawLine(float startX, float startY, float stopX, float stopY, Paint paint) 画线
canvas.drawLine(200,200,600,60,paint);
drawRoundRect(float left, float top, float right, float bottom, float rx, float ry, Paint paint) 画圆角矩形
参数 left 矩形左上角顶点 x坐标 top 矩形左上角顶点 y坐标 right 矩形右下角顶点 x坐标 bottom 矩形右下角顶点 y坐标 rx 和 ry 是圆角的横向半径和纵向半径。
canvas.drawRoundRect(200,200,600,400,20,20,paint);
drawArc(float left, float top, float right, float bottom, float startAngle, float sweepAngle, boolean useCenter, Paint paint) 绘制弧形或扇形
drawArc() 是使用一个椭圆来描述弧形的。left, top, right, bottom 描述的是这个弧形所在的椭圆;startAngle 是弧形的起始角度(x 轴的正向,即正右的方向,是 0 度的位置;顺时针为正角度,逆时针为负角度),sweepAngle 是弧形划过的角度;useCenter 表示是否连接到圆心,如果不连接到圆心,就是弧形,如果连接到圆心,就是扇形。
paint.setStyle(Paint.Style.FILL); // 填充模式 canvas.drawArc(200, 100, 800, 500, -110, 100, true, paint); // 绘制扇形 canvas.drawArc(200, 100, 800, 500, 20, 140, false, paint); // 绘制弧形 paint.setStyle(Paint.Style.STROKE); // 画线模式 canvas.drawArc(200, 100, 800, 500, 180, 60, false, paint); // 绘制不封口的弧形
阅读全文
0 0
- Android 自定View基础
- Android自定View组件方法
- 自定view其一 基础篇View的绘制流程
- 如何自定View
- 自定view
- 自定view画圆
- android代码混淆之自定View问题
- Android自定view的一些方法的说明
- android自定view实现可移动的凸四边形
- 自定view(一)
- Android 自定对话框
- 自定View的构造函数
- 自定view实践(一) 自定义loadingView
- 自定控件,自定义View NameTextView
- Material设计非得靠Android L吗?看过来,自定View仿elevation效果!
- Android自定View——可以设置宽高比例的ImageView
- Android自定View——可以设置宽高比例的ImageView
- Android自定View-最简单的可拖拽式层叠卡片
- Maven生成可以直接运行的jar包的多种方式
- springcloud eureka服务注册与发现
- 【JAVA】java中实现map集合的数据存取详解三种方法。Android程序员也是要会写的
- 用自己的话总结arrayList与LinkedList
- 【云星数据---Apache Flink实战系列(精品版)】:Apache Flink批处理API详解与编程实战016--DateSet实用API详解016
- Android 自定View基础
- mysql查询语句中执行转义符
- Qt扫描进程列表以及获取进程信息
- java 导出excel copy 模版
- github删除本地和远端分支
- springmvc 出现No mapping found for HTTP request with URI
- 下载apk的services封装
- 萌新报道!
- did not call through to super.onDestroy()