自定义View(一),初识自定义View
看了无数资料,总结一下自定义View
先明白一个自定义View的三大流程
onMeasure()
测量,决定View的大小onLayout()
布局,决定View在ViewGroup中的位置onDraw()
绘制,画出这个View的内容
这三个方法都存在于View类中,我们自定义View需要针对这三个方法做出修改来达到我们需要的目标或功能
先来一个最基本的例子,我们单纯的画一个圆,我们只需修改onDraw()方法即可
MyCustomVew.java
- public class MyCustomView extends View {
- public MyCustomView(Context context, AttributeSet attrs, int defStyleAttr) {
- super(context, attrs, defStyleAttr);
- // TODO Auto-generated constructor stub
- }
- public MyCustomView(Context context, AttributeSet attrs) {
- super(context, attrs);
- // TODO Auto-generated constructor stub
- }
- public MyCustomView(Context context) {
- super(context);
- // TODO Auto-generated constructor stub
- }
- @Override
- protected void onDraw(Canvas canvas) {
- // TODO Auto-generated method stub
- super.onDraw(canvas);
- // 实例化画笔并打开抗锯齿
- Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
- // 设置画笔颜色
- paint.setColor(Color.RED);
- /**
- * 画笔样式分三种:
- * 1.Paint.Style.STROKE:描边
- * 2.Paint.Style.FILL_AND_STROKE:描边并填充
- * 3.Paint.Style.FILL:填充 既然是画圆,那么就选择样式为描边
- */
- paint.setStyle(Paint.Style.STROKE);
- /*
- * 设置描边的粗细,单位:像素px 注意:当setStrokeWidth(0)的时候描边宽度并不为0而是只占一个像素
- */
- paint.setStrokeWidth(10);
- // 参数含义依次为:圆心X坐标、圆心Y坐标、圆半径、画笔
- canvas.drawCircle(500, 500, 200, paint);
- }
- }
在Activity的布局文件中引入这个自定义View
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:app="http://schemas.android.com/apk/res-auto"
- android:id="@+id/main_root_ll"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="vertical" >
- <com.example.testcustomview.MyCustomView
- android:id="@+id/main_cv"
- android:layout_width="match_parent"
- android:layout_height="match_parent" />
- </LinearLayout>
运行结果如下

如果我们想要让这个圆动起来呢?我们只要不断的去修改onDraw()不断的绘制就可以了
譬如我们想要画一个由小到大的实心圆,我们需要做的就是不断的改变的半径
MyCustomView
- public class MyCustomView extends View implements Runnable {
- private int radiu;// 圆的半径
- private Paint paint;
- public MyCustomView(Context context, AttributeSet attrs) {
- super(context, attrs);
- initPaint();
- }
- public MyCustomView(Context context) {
- this(context, null);
- }
- public void initPaint() {
- paint = new Paint(Paint.ANTI_ALIAS_FLAG);
- // 设置画笔颜色
- paint.setColor(Color.RED);
- /**
- * 画笔样式分三种: 1.Paint.Style.STROKE:描边 2.Paint.Style.FILL_AND_STROKE:描边并填充
- * 3.Paint.Style.FILL:填充 既然是画圆,那么就选择样式为描边
- */
- paint.setStyle(Paint.Style.FILL_AND_STROKE);
- /*
- * 设置描边的粗细,单位:像素px 注意:当setStrokeWidth(0)的时候描边宽度并不为0而是只占一个像素
- */
- paint.setStrokeWidth(10);
- }
- @Override
- protected void onDraw(Canvas canvas) {
- // TODO Auto-generated method stub
- super.onDraw(canvas);
- canvas.drawCircle(500, 500, radiu, paint);
- }
- @Override
- public void run() {
- while (radiu <= 200) {
- try {
- radiu += 10;
- Thread.sleep(300);
- //刷新View
- postInvalidate();
- } catch (InterruptedException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- }
- }
可以看到我们在run方法中调用了一个postInvalidate(),这个方法还有一个对应的方法Invalidate(),这两个方法的区别在于
postInvalidate()
前者是在非UI线程中使,用来刷新界面Invalidate()
在UI线程自身中使用,用来刷新界面
刚才的例子是画了一个圆,canvas还提供了其他一系列方法来供我们调用,用来画各种各样的图形
下篇文章来介绍
自定义View(一),初识自定义View的更多相关文章
- [asp.net mvc 奇淫巧技] 01 - 封装上下文 - 在View中获取自定义的上下文
我们在asp.net 开发中已经封装了最强大的HttpContext,我们可以在HttpContext中可以获取到几乎任何想获取的东西,也可以在HttpContext写入需要返回客户端的信息.但是这些 ...
- Android 自定义View修炼-【2014年最后的分享啦】Android实现自定义刮刮卡效果View
一.简介: 今天是2014年最后一天啦,首先在这里,我祝福大家在新的2015年都一个个的新健康,新收入,新顺利,新如意!!! 上一偏,我介绍了用Xfermode实现自定义圆角和椭圆图片view的博文& ...
- Android 自定义View修炼-自定义可动画展开收缩View的实现
有时候需要点击一个view可以动画展开和收缩折叠一个View这样的效果,这样就可以直接自定义View来实现. 本例中,采用继承FrameLayout来实现自定义的ExpandView.下面将详细介绍各 ...
- 自定义View 一 (继承VIew重写onDraw方法)
项目:具有圆形效果的自定义View 一.继承View并重写onDraw方法 public class CircleView extends View{ private static final int ...
- Android查缺补漏(View篇)--自定义 View 的基本流程
View是Android很重要的一部分,常用的View有Button.TextView.EditView.ListView.GridView.各种layout等等,开发者通过对这些View的各种组合以 ...
- Android view相关与自定义View
一.关于view的机制的问答 1.gesturedetector和ontouchevent的区别 gesturedetector指的是手势检测器,根据动态手势的运动特性,提出了速率边沿检测算法来分割手 ...
- 【Android - 自定义View】之自定义九宫格手势解锁控件
首先来介绍一下这个自定义View: (1)这个自定义View的名称叫做 LockView ,继承自View类: (2)这个自定义View实现了应用中常见的九宫格手势解锁功能,可以用于保证应用安全: ( ...
- 【Android - 自定义View】之自定义View实现“刮刮卡”效果
首先来介绍一下这个自定义View: (1)这个自定义View的名字叫做 GuaguakaView ,继承自View类: (2)这个View实现了很多电商项目中的“刮刮卡”的效果,即用户可以刮开覆盖层, ...
- 【Android - 自定义View】之自定义可滚动的流式布局
首先来介绍一下这个自定义View: (1)这个自定义View的名称叫做 FlowLayout ,继承自ViewGroup类: (2)在这个自定义View中,用户可以放入所有继承自View类的视图,这个 ...
- 【Android - 自定义View】之自定义可下拉刷新或上拉加载的ListView
首先来介绍一下这个自定义View: (1)这个自定义View的名称叫做 RefreshableListView ,继承自ListView类: (2)在这个自定义View中,用户可以设置是否支持下拉刷新 ...
随机推荐
- c# Mono.Cecil IL方式 读MethodBody
using Kufen.Common.Definitions; using Mono.Cecil; using System; using System.Collections.Generic; us ...
- 【嵌入式开发】裸机引导操作系统和ARM 内存操作 ( DRAM SRAM 类型 简介 | Logical Bank | 内存地址空间介绍 | 内存芯片连接方式 | 内存初始化 | 汇编代码示例 )
[嵌入式开发]ARM 内存操作 ( DRAM SRAM 类型 简介 | Logical Bank | 内存地址空间介绍 | 内存芯片连接方式 | 内存初始化 | 汇编代码示例 ) 一. 内存 ...
- 使用poi读取excel数据示例
使用poi读取excel数据示例 分两种情况: 一种读取指定单元格的值 另一种是读取整行的值 依赖包: <dependency> <groupId>org.apache.poi ...
- 怎么去掉织梦(dedecms)网站首页默认携带的index.html尾巴
如果你的网站带小尾巴,比如这样的 那你就要注意了,因为这会导致搜索引擎会认为和不带尾巴的页面是两个页面,都会参与排名,会分散首页的权重. 那我们怎么去除呢? 方法1: 1)在空间面板里面找到默认首页设 ...
- ai切片的完美解决方案
ai切片的完美解决方案1 背景拖到外面2 导出psd3 ps切片 背景夹层黑色就看清楚啦
- perl oneline
可参考博客:http://blog.csdn.net/carzyer/article/details/5117429 Perl常用命令行参数概览 -e 指定字符串以作为脚本(多个字符串迭加)执行 -M ...
- 安卓学习日记第二天——Fragment
一.基本概念 Fragment是依赖于Activity的,不能独立存在的. 一个Activity里可以有多个Fragment. 一个Fragment可以被多个Activity重用. Fragment有 ...
- spring整合mybatis报.UnsatisfiedDependencyException错误
tomcat启动报org.springframework.beans.factory.UnsatisfiedDependencyException:错误 org.springframework.bea ...
- string字符串成员函数
string字符串成员函数 string str1="aaa"; char c='c'; str1.assign("ABCAAAAAAABBBBB");//替换 ...
- DRF框架(七) ——三大认证组件之频率组件、jwt认证
drf频率组件源码 1.APIView的dispatch方法的 self.initial(request,*args,**kwargs) 点进去 2.self.check_throttles(re ...