Android自定义View训练【1】
来源:互联网 发布:linux mysql 日志 编辑:程序博客网 时间:2024/06/09 17:30
前言:最近这段时间想根据网上博客的案例,自己练习自定义View,代码仅仅作为练习用,还有很多不足之处,见谅
案例取自:http://blog.csdn.net/wingichoy/article/details/50455412
先上传源码
public class PracticeView1 extends View { private float mrectwidth = 200; private float mrectheight = 100; private float mround = 20; private float len = 15; private Paint mPaint = new Paint(); private Path mPath = new Path(); public PracticeView1(Context context) { super(context); } public PracticeView1(Context context, AttributeSet attrs) { super(context, attrs); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); int mwidthmode = MeasureSpec.getMode(widthMeasureSpec); int mwidthsize = MeasureSpec.getSize(widthMeasureSpec); int mheightmode = MeasureSpec.getMode(heightMeasureSpec); int mheightsize = MeasureSpec.getSize(heightMeasureSpec); mrectwidth = (float) mwidthsize; mrectheight = (float) mheightsize; if(mwidthmode == MeasureSpec.AT_MOST) { mrectwidth = (float) (600/2); } if(mheightmode == MeasureSpec.AT_MOST) { mrectheight = (float) (300/2); } setMeasuredDimension((int) mrectwidth, (int) mrectheight); Log.d("SunJ:", "mrectwidth " + String.valueOf(mrectwidth) + ", mrectheight " + String.valueOf(mrectheight)); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); mPaint.setColor(Color.GRAY); mPaint.setStyle(Paint.Style.FILL); mPaint.setAntiAlias(true); RectF mRectF = new RectF(0, 0, mrectwidth, mrectheight-len); canvas.drawRoundRect(mRectF, mround, mround, mPaint); mPath.moveTo(mrectwidth/2, mrectheight); mPath.lineTo(mrectwidth/2+len, mrectheight-len); mPath.lineTo(mrectwidth/2-len, mrectheight-len); mPath.close(); canvas.drawPath(mPath, mPaint); }}
效果图:
要点:
1.重写onMeasure()是为了在xml中设置layout_weight/layout_height属性为wrap_content时,不会变成match_parent
2.在onDraw()里面的矩形尺寸是由onMeasure()来得到的,这样子,在xml中设置具体的宽和高时,该矩形的大小同样也会发生相应的变化,这样,才能做到随意改变大小;而不会出现当设置具体的宽和高时,view缺胳膊少腿或者是多出空白的区域
0 0
- Android自定义View训练【1】
- Android自定义View训练【2】
- android自定义View(1)
- Android 自定义View (1)
- Android 自定义View(1)
- android自定义view(1)
- Android 自定义View、ViewGroup 实战训练之ClearEditText
- Android 自定义View、ViewGroup 实战训练之CustomFrameDialog
- Android 自定义View、ViewGroup 实战训练之CircleProgress
- Android View---自定义View
- Android View---自定义View
- Android 自定义View学习(1)
- Android 记录1 自定义View
- Android自定义View(1)
- android 自定义view(1)
- android 自定义 View(1)
- Android自定义View(1)
- Android 自定义View 之 自定义View属性
- Centos7.1 for MySQL5.6.30源码安装及多实例配置
- 【教程】Mac os x电脑设置安装cisco AnyConnect方法
- bootstrap+jsoup+jsp智联页面抓取系统
- 条款1:视C++为一个语言联邦
- UML——对象图
- Android自定义View训练【1】
- 6、重建二叉树
- 凸优化概念
- Oracle优化07-分析及动态采样-动态采样
- OpenCV人脸检测 人脸打码
- DIY个人智能家庭网关—— 前言
- H.264中NAL、Slice与frame意思及相互关系
- jQuery_内容过滤选择器
- sdfsad