import java.util.List;
import com.dts.app.levelreader.R;
import com.dts.app.levelreader.entity.Word;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.util.Log;
import android.widget.TextView;
/***
* zdy a text view for display article
*
* @author LD
* @date 2015-05
*/publicclassATextViewextendsTextView {/** TAG **/private String TAG = "ATextView";
/** Paint for text **/private Paint mTextPaint;
/** text content **/private String mText;
/** the height for text from baseline **/privateint mAscent;
@SuppressWarnings("unused")
private List<Word> words;
/**
* Constructor. This version is only needed if you will be instantiating the
* object manually (not from a layout XML file).
*
* @param context
*/publicATextView(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)
*/publicATextView(Context context, AttributeSet attrs) {
super(context, attrs);
initLabelView();
/**
* get textView TypedArray
*
***/
TypedArray a = context.obtainStyledAttributes(attrs,
R.styleable.ATextView);
/***
* get text string; CharSequence is a interface
*/
CharSequence s = a.getString(R.styleable.ATextView_AText);
if (s != null) {
setText(s.toString());
}
/**
* get color for text
*/
setTextColor(a.getColor(R.styleable.ATextView_ATextColor, Color.WHITE));
/**
* get size for text
*/int textSize = a.getDimensionPixelOffset(
R.styleable.ATextView_ATextSize, 0);
if (textSize > 0) {
setTextSize(textSize);
}
/**
* must do this
*/
a.recycle();
}
/**
* init a label view for if not exist a attr
*/privatefinalvoidinitLabelView() {
mTextPaint = new Paint();
mTextPaint.setAntiAlias(true);
mTextPaint.setTextSize(16 * getResources().getDisplayMetrics().density);
mTextPaint.setColor(Color.WHITE);
setPadding(5, 3, 3, 5);
}
/***
* for other activity use
*
* @param words
*/publicvoidsetWords(List<Word> words) {
this.words = words;
}
/**
* Sets the text to display in this label
*
* @param text
* The text to display. This will be drawn as one line.
*/publicvoidsetText(String text) {
Log.i(TAG, "setText");
mText = text;
requestLayout();
invalidate();
}
/**
* Sets the text size for this label
*
* @param size
* Font size
*/publicvoidsetTextSize(int size) {
mTextPaint.setTextSize(size);
requestLayout();
invalidate();
}
/**
* Sets the text color for this label.
*
* @param color
* ARGB value for the text
*/publicvoidsetTextColor(int color) {
mTextPaint.setColor(color);
invalidate();
}
/**
* @see android.view.View#measure(int, int)
*/@OverrideprotectedvoidonMeasure(int widthMeasureSpec, int heightMeasureSpec) {
Log.i(TAG, "onMeasure");
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
*/privateintmeasureWidth(int measureSpec) {
int result = 0;
int specMode = MeasureSpec.getMode(measureSpec);
int specSize = MeasureSpec.getSize(measureSpec);
if (specMode == MeasureSpec.EXACTLY) {
/**
* parent must want to do
*/
result = specSize;
} else {
result = (int) mTextPaint.measureText(mText) + getPaddingLeft()
+ getPaddingRight();
if (specMode == MeasureSpec.AT_MOST) {
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
*/privateintmeasureHeight(int measureSpec) {
int result = 0;
int specMode = MeasureSpec.getMode(measureSpec);
int specSize = MeasureSpec.getSize(measureSpec);
mAscent = (int) mTextPaint.ascent();
if (specMode == MeasureSpec.EXACTLY) {
result = specSize;
} else {
result = (int) (-mAscent + mTextPaint.descent()) + getPaddingTop()
+ getPaddingBottom();
if (specMode == MeasureSpec.AT_MOST) {
result = Math.min(result, specSize);
}
}
return result * 10;
}
/**
* Render the text
*
* @see android.view.View#onDraw(android.graphics.Canvas)
*/@OverrideprotectedvoidonDraw(Canvas canvas) {
super.onDraw(canvas);
Log.i(TAG, "onDraw");
// Paint mPaint = new Paint();// mPaint.setColor(Color.YELLOW);// canvas.drawRect(0, 0, getMeasuredWidth(), getMeasuredHeight(),// mPaint);
canvas.drawText(mText, getPaddingLeft(), getPaddingTop() - mAscent,
mTextPaint);
}
}