[Android5 系列二] 1. 全实例之控件(Widget)

package com.oscar999.androidstudy.view;import com.oscar999.androidstudy.R;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;public class MyView extends View {private Paint mTextPaint;private int mAscent;private String mText;public MyView(Context context, AttributeSet attrs) {super(context, attrs);initMyView();TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.MyView);CharSequence s = a.getString(R.styleable.MyView_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.MyView_textColor, 0xFF000000));int textSize = a.getDimensionPixelOffset(R.styleable.MyView_textSize, 0);if (textSize > 0) {setTextSize(textSize);}a.recycle();// TODO Auto-generated constructor stub}private final void initMyView() {// create text paint to setting textmTextPaint = new Paint();mTextPaint.setAntiAlias(true);// Must manually scale the desired text size to match screen densitymTextPaint.setTextSize(16 * getResources().getDisplayMetrics().density);mTextPaint.setColor(0xFF000000);setPadding(3, 3, 3, 3);}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.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().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// methodmTextPaint.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) */@Overrideprotected 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.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 beresult = specSize;} else {// Measure the textresult = (int) mTextPaint.measureText(mText) + getPaddingLeft()+ getPaddingRight();if (specMode == MeasureSpec.AT_MOST) {// Respect AT_MOST value if that was what is called for by// measureSpecresult = 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 beresult = 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// measureSpecresult = Math.min(result, specSize);}}return result;}protected void onDraw(Canvas canvas) {super.onDraw(canvas);canvas.drawText(mText, this.getPaddingLeft(), this.getPaddingTop()- mAscent, mTextPaint);}}2. 在 res/layouts下新增 attrs.xml

,歌里唱的是“你离开我,就是旅行的意义”,

[Android5 系列二] 1. 全实例之控件(Widget)

相关文章:

你感兴趣的文章:

标签云: