关于自定义组件-以apidemo中的LabelView进行讲解

对于自定义组件,可以有两种方式,一种是继承View,重写里面的onDraw()函数,或者继承ViewGroup或者子类,自定义组合类型的组件。

下面以apidemo为例子进行讲解

下面是代码,我又进行了重新注释,应该好懂点了

1.对于构造函数,单个参数的构造函数是代码new的时候使用的,第二个构造函数是系统反射成对象用的,要想再xml中使用必须有两个参数的那个构造函数。

2.requestLayout();这个方法的作用是把新布局应用上去,但是界面还没刷新。

3.invalidate();这个是用来告诉界面刷新的,调用了这个方法不一定会立刻刷新,会在将来的某一刻刷新,刷新时调用onDraw()方法。

 

具体的一些细节还没有写完,先写到这里,有时间了再补充上去

 

了解一下这些api有利于明白自定义组件的过程

view

http://note.youdao.com/share/?id=1fcf0de6c7bbfec507fdb2b0962708ab&type=note

MessureSpec

http://note.youdao.com/share/?id=b992b2b83cd8ff3245ad5609a9499966&type=note

Paint

http://note.youdao.com/share/?id=735734a14ff01d96690f5c845f18ec7a&type=note

 

/*
 * Copyright (C) 2007 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.example.android.apis.view;

// 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;

import com.example.android.apis.R;


/**
 * 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 int mAscent;
    private String mText;
    
    /**
     * 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() {
    	//create text paint to setting text
        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;
        //Call this when something has changed which has invalidated the layout of this view. This will schedule a layout pass of the view tree. 
        //当有些东西已经改变使这个view的布局已经无效使调用此方法,这个方法会把一个新的布局安排到视图树中去。
        requestLayout();
        //Invalidate the whole view. If the view is visible, onDraw(android.graphics.Canvas) will be called at some point in the future. This must be called from a UI thread. To call from a non-UI thread, call postInvalidate(). 
        //调用这个方法使当前的整个view显示无效,当这个view可见时,将来的某一时刻会调用onDraw(android.graphics.Canvas),这个方法必须在UI线程中调用,如果从非UI线程中调用postInvalidate()方法,从而换成新的显示。
        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) {
    	//This mehod must be called by onMeasure(int, int) to store the measured width and measured height. Failing to do so will trigger an exception at measurement time.
        //这个方法必须在onMeasure(int, int)中被调用,去存储测量的宽度和高度。如果不调用的话,在测量期间会抛出异常。
    	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
        	//计算这个view的大小:size = 左右空隙的大小+字符串的大小
            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
            	//在AT_MOST模式,即最大模式的情况下,size只能小于等于指定的大小,大小取指定大小和计算出的大小中的一个最小的。
                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();//返回字体基线上部的大小,返回的是负数
        //关于字体的基线,可以看下我的这个翻译http://note.youdao.com/share/?id=735734a14ff01d96690f5c845f18ec7a&type=note
        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);
    }
}


 

你可能感兴趣的:(关于自定义组件-以apidemo中的LabelView进行讲解)