实践敲码是检验自己掌握的唯一标准。
一、总结下实践中自定义view步骤以及注意地方:
1、定义自定义属性
首先在res/values/ 下建立一个attrs.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<attr name="titleText" format="string" />
<attr name="titleTextColor" format="color" />
<attr name="titleTextSize" format="dimension" />
<declare-styleable name="CustomTitleView">
<attr name="titleText" />
<attr name="titleTextColor" />
<attr name="titleTextSize" />
</declare-styleable>
</resources>
2、继承View,第三个构造器中获取自定义属性
/**
* 获得我自定义的样式属性
*
* @param context
* @param attrs
* @param defStyle
*/
public CustomTitleView(Context context, AttributeSet attrs, int defStyle)
{
super(context, attrs, defStyle);
/**
* 获得我们所定义的自定义样式属性(传递进来的attrs, res/valus/attrs.xml/declare-styleable,传递进来defStyle,0)
)
*/
TypedArray a = context.getTheme().obtainStyledAttributes(attrs, R.styleable.CustomTitleView, defStyle, 0);
int n = a.getIndexCount();
for (int i = 0; i < n; i++)
{
int attr = a.getIndex(i);
switch (attr)
{
case R.styleable.CustomTitleView_titleText:
mTitleText = a.getString(attr);
break;
case R.styleable.CustomTitleView_titleTextColor:
// 默认颜色设置为黑色
mTitleTextColor = a.getColor(attr, Color.BLACK);
break;
case R.styleable.CustomTitleView_titleTextSize:
// 默认设置为16sp,TypeValue也可以把sp转化为px
mTitleTextSize = a.getDimensionPixelSize(attr, (int) TypedValue.applyDimension(
TypedValue.COMPLEX_UNIT_SP, 16, getResources().getDisplayMetrics()));
break;
}
}
a.recycle();//记得必须要回收
/**
* 获得绘制文本的宽和高(初始化画笔在构造器中进行)
*/
mPaint = new Paint();
mPaint.setTextSize(mTitleTextSize);
// mPaint.setColor(mTitleTextColor);
mBound = new Rect();
mPaint.getTextBounds(mTitleText, 0, mTitleText.length(), mBound);//(编辑txt,txt第一个字符,txt最后一个字符矩形边界)
}
注意:第一个构造器改为this(context,null);
第二个构造器改为this(context , attrs, 0);
将super改this,让第一个构造器引用第二个,第二个引用第三,这样做是为了让所有构造器调用第三个构造器方法去获取自定义属性,否则报错
3、测量(模式+大小)
4、画图
canvas基本操作参考http://blog.csdn.net/u013831257/article/details/50599912
5、xml布局
命名空间:xmlns:gaga(自定义)=“ xxxx/该布局对应的Activity包名”
自定义控件引用 <包名.自定义类名
gaga:randomText(在attrs中name对应的属性)="haha" />
6、点击事件中要记得刷新
postInvalidate();
二、实战例子
自定义view还是挺好玩的,清明期间跟着csdn大神们学了2个例子,加深了理解,由于学习进度问题,先不学了,这里先整理下,以后用到再深入,要不然也会忘了,
2.1
android 自定义控件以及自定义view学习(随机验证码生成)
http://blog.csdn.net/qq_16064871/article/details/50557424
2.2
Android BitmapShader 实战 实现圆形、圆角图片
http://blog.csdn.net/lmj623565791/article/details/41967509
http://blog.csdn.net/qq_16064871/article/details/50628381
http://www.cnblogs.com/JczmDeveloper/p/4192117.html
Android开发自定义ImageView控件实现圆角边框等功能
http://www.imgeek.org/article/825307664
http://blog.csdn.net/bruceyangjie/article/details/50513034
2.3
Android 自定义控件实现刮刮卡效果 真的就只是刮刮卡么
http://blog.csdn.net/lmj623565791/article/details/40162163
自定义View--刮刮卡
http://blog.csdn.net/qq_30716173/article/details/51122474
https://www.baidu.com/s?tn=90223186_s_hao_pg&word=%E8%87%AA%E5%AE%9A%E4%B9%89view%E5%88%AE%E5%88%AE%E5%8D%A1
如何在Android设备旋转时暂存数据以保护当前的交互状态?
https://segmentfault.com/a/1190000003965285 2.4
Android 高手进阶之自定义View,自定义属性(带进度的圆形进度条)
http://blog.csdn.net/xiaanming/article/details/10298163
http://blog.csdn.net/books1958/article/details/48267903
https://www.baidu.com/s?tn=90223186_s_hao_pg&word=自定义view进度圈
http://blog.csdn.net/qq_27102463/article/details/51612999
Android 自定义带数字的圆形进度条和中间是文字的圆形进度条View
http://blog.csdn.net/u013361668/article/details/50430174
2.5自定义进阶实例
简单说说Android自定义view学习推荐的方式
http://blog.csdn.net/wingichoy/article/details/50483101
Android从零开始:我的自定义view学习过程
http://blog.csdn.net/zhaozhu365/article/details/52590666
自定义View实例(二)----一步一步教你实现QQ健康界面
http://blog.csdn.net/tyk0910/article/details/51594479
2.6杂七杂八实例
Android 自定义View修炼-Android开发之自定义View开发及实例详解
http://www.cnblogs.com/JczmDeveloper/p/3759183.html
Android自定义View的实现方法,带你一步步深入了解View(四)
http://blog.csdn.net/guolin_blog/article/details/17357967
http://930307.blog.51cto.com/7950022/1345591/
Android自定义View讲解加示例
http://www.jianshu.com/p/e9de56679315
http://www.jb51.net/article/95335.htm