接着上一篇,介绍一个官方的自定义控件实例:


一个自定义控件实例

自定义控件LabelView描述了一些自定义控件的基本方面:

* 继承View类实现以完全的自定义控件

*参数化的构造函数,这些构造函数带有视图参数(定义在xml文件里的参数),其中一些通过父类传递,更重要的是有一些自定义属性被定义和使用

*一些自定义类的标准公用属性,如setText(),setTextSize(),setTextColor()等等

*一个重写的onMeasure()方法来决定并设置组件的渲染尺寸(注,在LabelView中,真实的工作在私有的measureWidth方法中进行)

*一个重写的onDraw()方法将提供的标签绘制到画布上

下面是官方LabelView的源码(详细分析可参考:http://blog.csdn.net/manymore13/article/details/8769013)

/** * 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 a = context.obtainStyledAttributes(attrs,                R.styleable.LabelView);        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().        setTextColor(a.getColor(R.styleable.LabelView_textColor, 0xFF000000));        int textSize = a.getDimensionPixelOffset(R.styleable.LabelView_textSize, 0);        if (textSize > 0) {            setTextSize(textSize);        }        a.recycle();    }    private final void initLabelView() {        mTextPaint = new Paint();        mTextPaint.setAntiAlias(true);        // Must manually scale the desired text size to match screen density        mTextPaint.setTextSize(16 * getResources().getDisplayMetrics().density);        mTextPaint.setColor(0xFF000000);        setPadding(3, 3, 3, 3);    }    /**     * 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) {        // This text size has been pre-scaled by the getDimensionPixelOffset method        mTextPaint.setTextSize(size);        requestLayout();        invalidate();    }    /**     * Sets the text color for this label.     * @param color ARGB value for the text     */    public void setTextColor(int color) {        mTextPaint.setColor(color);        invalidate();    }    /**     * @see android.view.View#measure(int, int)     */    @Override    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {        setMeasuredDimension(measureWidth(widthMeasureSpec),                measureHeight(heightMeasureSpec));    }    /**     * Determines the width of this 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);        if (specMode == MeasureSpec.EXACTLY) {            // We were told how big to be            result = specSize;        } else {            // Measure the 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;    }    /**     * Determines the height of this view     * @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     *      * @see android.view.View#onDraw(android.graphics.Canvas)     */    @Override    protected void onDraw(Canvas canvas) {        super.onDraw(canvas);        canvas.drawText(mText, getPaddingLeft(), getPaddingTop() - mAscent, mTextPaint);    }}

下一篇,自定义组合控件的介绍

更多相关文章

  1. Android控件的继承关系图
  2. android的EditText控件实现只读
  3. Android之自定义控件深入
  4. gradle配置参数详解
  5. Android(安卓)dialog,activity 屏蔽Home键的教程详解
  6. Android之在string.xml配置文字颜色粗体等效果
  7. Android-UI 开源控件
  8. Android(安卓)onMeasure
  9. Android入门级之WebView的使用

随机推荐

  1. HTC one/M7电信802d 毒蛇ViperOne2.1.0/
  2. Android平台如何确定deconfig及dtsi的总
  3. Android的本地Json解析
  4. Android的ROS开发环境配置(Android+ROS+r
  5. static 和 visibility hidden 的区别
  6. 安卓五子棋小游戏
  7. 2016~4.12Android(安卓)之图片处理
  8. Android(安卓)Studio创建Serializable对
  9. android图像绘制(五)——画布保存为图片
  10. PopWindow Android(安卓)7.0 位置显示不