对于谷歌给我们提供的ApiDemo里有很多有学习参考价值的实例,今天来学习理解其中的一个实例类LabelView,此类
继承View,并对View中的onMeasure(),Ondraw()方法进行了重写,其中涉及到setMeasuredDimension() , MeasureSpec,Canvas,Paint,以及自定义
属性的一些应用。
像完全自定义控件(也就是继承View自定义控件),一般会想到覆盖onMeasure(),Ondraw(),
默认onMeasure()会总是设置一个100*100尺寸
关于自定义属性 可以参考 Android开发之自定义属性(Define Custom Attributes)
下面是主要实现代码
package com.example.labelview; // Need the following import to get access to the app resources, since this // class is in a sub-package. import android.content.Context; import android.content.res.TypedArray; import android.graphics.Canvas; import android.graphics.Paint; import android.util.AttributeSet; import android.view.View; /** * Example of how to write a custom subclass of View. LabelView * is used to draw simple text views. Note that it does not handle * styled text or right-to-left writing systems. * */ public class LabelView extends View { private Paint mTextPaint; private String mText; private int mAscent; /** * Constructor. This version is only needed if you will be instantiating * the object manually (not from a layout XML file). * @param context */ public LabelView(Context context) { super(context); initLabelView(); // 初始化 } /** * Construct object, initializing with any attributes we understand from a * layout file. These attributes are defined in * SDK/assets/res/any/classes.xml. * * @see android.view.View#View(android.content.Context, android.util.AttributeSet) */ public LabelView(Context context, AttributeSet attrs) { super(context, attrs); initLabelView(); // 得到TypedArray 后面会利用它来获取自定义属性的值 TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.LabelView); // 获取自定义属性text的值 CharSequence s = a.getString(R.styleable.LabelView_text); if (s != null) { setText(s.toString()); } // Retrieve the color(s) to be used for this view and apply them. // Note, if you only care about supporting a single color, that you // can instead call a.getColor() and pass that to setTextColor(). // 获取自定义属性textColor的值,并设置文本相应的颜色值 setTextColor(a.getColor(R.styleable.LabelView_textColor, 0xFF000000)); int textSize = a.getDimensionPixelOffset(R.styleable.LabelView_textSize, 0); if (textSize > 0) { setTextSize(textSize); } // 注意这里记得要回收 TypedArray a.recycle(); } // 初始化 paint,并对其设置相应的属性值 private final void initLabelView() { mTextPaint = new Paint(); mTextPaint.setAntiAlias(true); mTextPaint.setTextSize(16); mTextPaint.setColor(0xFF000000); setPadding(10, 10, 10, 10); } /** * Sets the text to display in this label * @param text The text to display. This will be drawn as one line. */ public void setText(String text) { mText = text; requestLayout(); invalidate(); } /** * 设置文本大小 * Sets the text size for this label * @param size Font size */ public void setTextSize(int size) { mTextPaint.setTextSize(size); // view 在layout上发生的改变(大小,位置),遂调用此方法 requestLayout(); // 使整个View无效,如果该View可见,那么将会系统调用onDraw(...)方法 invalidate(); } /** * 设置文本颜色 * Sets the text color for this label. * @param color ARGB value for the text */ public void setTextColor(int color) { mTextPaint.setColor(color); invalidate(); } /** * 测量View和它的内容,并决定测量宽度和测量高度 * 这个方法被 measure(int, int)所调用 */ @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { // 覆盖onMeasure时必须调用此方法,否则会抛出measurement 运行时异常 setMeasuredDimension(measureWidth(widthMeasureSpec), measureHeight(heightMeasureSpec)); } /** * 决定这个view的宽度 * @param measureSpec A measureSpec packed into an int * @return The width of the view, honoring constraints from measureSpec */ private int measureWidth(int measureSpec) { int result = 0; int specMode = MeasureSpec.getMode(measureSpec); int specSize = MeasureSpec.getSize(measureSpec); // 如果view的宽度是确定值,那么直接获取此确定值 if (specMode == MeasureSpec.EXACTLY) { // We were told how big to be result = specSize; } else { // 如果宽度不是确定值,就需要计算下 // 测量text的宽度 result = (int) mTextPaint.measureText(mText) + getPaddingLeft() + getPaddingRight(); if (specMode == MeasureSpec.AT_MOST) { // Respect AT_MOST value if that was what is called for by measureSpec result = Math.min(result, specSize); } } return result; } /** * 决定这个view的高度 * 注意: 字体的高度=上坡度+下坡度+行间距 * ascent和top都是负数 * @param measureSpec A measureSpec packed into an int * @return The height of the view, honoring constraints from measureSpec */ private int measureHeight(int measureSpec) { int result = 0; int specMode = MeasureSpec.getMode(measureSpec); int specSize = MeasureSpec.getSize(measureSpec); mAscent = (int) mTextPaint.ascent(); if (specMode == MeasureSpec.EXACTLY) { // We were told how big to be result = specSize; } else { // Measure the text (beware: ascent is a negative number) result = (int) (-mAscent + mTextPaint.descent()) + getPaddingTop() + getPaddingBottom(); if (specMode == MeasureSpec.AT_MOST) { // Respect AT_MOST value if that was what is called for by measureSpec result = Math.min(result, specSize); } } return result; } /** * Render the text * 绘制相应的界面,这里是drawText * @see android.view.View#onDraw(android.graphics.Canvas) */ @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); canvas.drawText(mText, getPaddingLeft(), getPaddingTop() - mAscent, mTextPaint); } }
关于谷歌怎么把两个属性包装在一个int里,怎么在一个int里解包获取Mode和size属性,请看MeasureSpec源码
这样做可以节约空间,可以减少更多的对象的创建。
public static class MeasureSpec { private static final int MODE_SHIFT = 30; private static final int MODE_MASK = 0x3 << MODE_SHIFT; public static final int UNSPECIFIED = 0 << MODE_SHIFT; public static final int EXACTLY = 1 << MODE_SHIFT; public static final int AT_MOST = 2 << MODE_SHIFT; public static int makeMeasureSpec(int size, int mode) { return size + mode; } public static int getMode(int measureSpec) { return (measureSpec & MODE_MASK); } public static int getSize(int measureSpec) { return (measureSpec & ~MODE_MASK); } }
onMeasure(int widthMeasureSpec, int heightMeasureSpec) 方法, 系统在绘制对象时,首先得确定对象在屏幕上占用多大的范围,因此在这个方法中,必须得确定好控件的尺寸然后通过一个特定的函数接口(setMeasuredDimension(width, height))去通知系统有关该控件的尺寸信息。系统传递进来的两个参数是一个约束条件,控件到底占据多大的尺寸由这两个参数决定, 每一个参数其实一个MeasureSpec对象,该对象包含了Measure's Mode和Size两个属性:
Mode
UNSPECIFIED 系统对对象的size没进行约束,可以任意设置
EXACTLY 系统对对象的size已经确定,只能为MeasureSpec对象中指定的size
AT_MOST 系统对对象的最大size进行了约束,即该对象的size不能超过MeasureSpec对象中指定的size
参考资料
android中onMeasure初看,深入理解布局之一!
http://zhidao.baidu.com/question/525618489.html
Custom Components
点击下载源码