Android 自定义View教程(1)
来源:互联网 发布:java培训有哪些骗局 编辑:程序博客网 时间:2024/06/16 07:24
自定义View,很多初学Android的朋友听到这么一句话绝逼是一脸膜拜!因为在很多初学者眼里,能够自己去画一个View绝逼是一件很屌的事。其实自定义View你了解了,其实也就那么几回事,我将一步步,由浅入深,一步步带大家走进自定义View的进阶之路。
我们都知道要先自定义一个View,要先继承一个view,重写他的构造方法。
/** * 作者:linchaokun on 2016/7/21 22:25 * 邮箱:812086325@qq.com * */public class CustomView extends View{ private Paint paint; public CustomView(Context context) { this(context,null); } public CustomView(Context context, AttributeSet attrs) { this(context, attrs,0); } public CustomView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); }这里可以看到实现了3个构造方法,第一个调用第二个,第二个调用第三个,那么3个构造方法都是在什么时候执行呢。
- 构造方法一:当我们new一个view的时候执行。
- 构造方法二:当我们在布局中用到了
android:layout_width="match_parent"
等,但是没有用自定义属性时执行。 - 构造方法三:当我们在布局中使用了自定义属性时执行。
我们可以想象一下,现实生活中,我们画图需要笔和画布,然而我们讲究万物皆对象,在Android中画图也需要笔和画布。
画布Android已经帮我们准备好,我们只需要自己创建一只画笔在画布上作出我们想要的图案就可以了,那么我们先来创建一只画笔出来。
public CustomView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); //初始化画笔 initPaint(); }
因为我们构造方法一调用方法二,方法二调用方法三,所以我们只要把初始化画笔的方法写在构造方法三上。
private void initPaint() { paint = new Paint(); //抗锯齿 paint.setAntiAlias(true); //设置颜色 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(12); }每个方法的意思,我注释已经写得很明白了。
那么画笔我们创建好了,接下来我们就需要来作图了,我们需要实现onDraw()方法。
@Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); // 画一个圆 canvas.drawCircle(getWidth()/2, getHeight()/2,100, paint); }我们可以看到参数中canvas就是Android中给我们提供的画布,我们只需要来draw来作出一个图就可以。
canvas.drawCircle是画一个圆,参数一和参数二确定圆心的坐标位置,参数3是圆的半径,参数4是我们定义的画笔。
这样我们就实现了我们自定义的View,当然不要忘记了把我们自定义的View,引用到我们的布局中。
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent"> <com.linchaokun.customview1.view.CustomView android:layout_width="match_parent" android:layout_height="match_parent" /></RelativeLayout>好了,下面我们来运行一下,看下效果。
1 0
- Android 自定义View教程(1)
- Android 自定义View教程(2)
- Android 自定义View教程(3)
- Android 自定义View教程(一)
- Android自定义View教程(二)
- Android自定义View教程(三)
- Android自定义View教程(四)
- Android自定义View(1)
- android 自定义view(1)
- android 自定义 View(1)
- Android自定义View(1)
- (转载)android 自定义View google官方教程
- Android 自定义View小结(重于理解,不作为教程)
- android自定义View(1)
- Android 自定义View (1)
- Android 自定义View(1)
- android自定义view(1)
- Android自定义View之View详解(1)
- Hibernate的悲观锁并发控制机制及LockMode
- 使用setprecision控制输出流显示浮点数的有效数字个数(C++)
- c语言中 结构变量,结构类型标示,结构类型定义的区别
- 安卓版本发布五部曲
- CodeForces Gym 100735B
- Android 自定义View教程(1)
- Andriod下完全自定义控件和在自定义控件中使用自定义属性
- Visual C++
- 蓝桥杯 ADV-193算法提高 盾神与条状项链
- Codeforces Round #206 (Div. 2) C-Vasya and Robot
- 蓝桥杯 ALGO-29 算法训练 校门外的树(区间处理)
- Poj1611 The Suspects 并查集基础题
- hibernate与struts2一起时的异常处理
- 蓝桥杯 ALGO-11算法训练 瓷砖铺放(递归/动态规划)