Android控件结构和自定义控件【5】
作者:互联网
重写view实现全新的控件
两个关键的方法
1. onMeasure() 方法
@Override
protected void onMeasure(int widthMeasureSpec,
int heightMeasureSpec) {
mMeasureWidth = MeasureSpec.getSize(widthMeasureSpec);
mMeasureHeigth = MeasureSpec.getSize(heightMeasureSpec);
setMeasuredDimension(mMeasureWidth, mMeasureHeigth);
//TODO
//...
}
2. onDraw()方法
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
for (int i = 0; i < mRectCount; i++) {
mRandom = Math.random();
float currentHeight = (float) (mRectHeight * mRandom);
canvas.drawRect(
(float) (mWidth * 0.4 / 2 + mRectWidth * i + offset),
currentHeight,
(float) (mWidth * 0.4 / 2 + mRectWidth * (i + 1)),
mRectHeight,
mPaint);
}
//定时刷新
postInvalidateDelayed(300);
}
标签:控件,canvas,onDraw,自定义,int,float,0.4,Android 来源: https://blog.csdn.net/xfb1989/article/details/94441920