阿弥陀佛:
学完这个个系列,自定义View就So Easy...
主要是逐步分析TextView源码,分析完成后,你就能够彻底了解TextView,想怎么玩就怎么玩。。。
实现的主要内容:
继承View,实现基本TextView功能:更改内容和字体大小。
步骤1: 定义atts.xml文件:
步骤2:解析属性文件
//TypedArray:一组容器用来保存属性的数组
TypedArray a = theme.obtainStyledAttributes(attrs,
R.styleable.TextViewAppearance, defStyleAttr, defStyleRes);
TypedArray appearance = null;
int ap = a.getResourceId(
R.styleable.TextViewAppearance_textAppearance, -1);
a.recycle();
if (ap != -1) {
appearance = theme.obtainStyledAttributes(
ap, R.styleable.TextAppearance);
}
if (appearance != null) {
int n = appearance.getIndexCount();
for (int i = 0; i < n; i++) {
int attr = appearance.getIndex(i);
switch (attr) {
case R.styleable.TextView_text:
text = a.getText(attr);
break;
}
}
}
步骤3:初始化TextPaint
//TextPaint主要用来存储字体大小、颜色等属性
private TextPaint mPaint;
mPaint = new TextPaint();
mPaint.setColor(getResources().getColor(R.color.red));
mPaint.setTextSize(DensityUtil.dip2px(getContext(), textSize));
步骤4:调用onDraw绘制文字
canvas.drawText(text.toString(), getWidth() / 2, getHeight() / 2, mPaint);
步骤5:定义改变内容和字体大小的方法:
public CharSequence getText() {
return text;
}
public void setText(CharSequence text) {
this.text = text;
invalidate();
}
public int getTextSize() {
return textSize;
}
public void setTextSize(int textSize) {
this.textSize = textSize;
mPaint.setTextSize(DensityUtil.dip2px(getContext(),textSize));
invalidate();
}
自定义TextView完整代码:
public class LincolnTextView extends View {
private CharSequence text;
private TextPaint mPaint;
private int textSize = 30;
public LincolnTextView(Context context) {
this(context, null);
}
public LincolnTextView(Context context, @Nullable AttributeSet attrs) {
this(context, attrs, R.attr.textViewStyle);
}
public LincolnTextView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
this(context, attrs, defStyleAttr, 0);
}
public LincolnTextView(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
CharSequence text = "AAAA";
final Resources.Theme theme = context.getTheme();
TypedArray a = theme.obtainStyledAttributes(attrs,
R.styleable.TextViewAppearance, defStyleAttr, defStyleRes);
TypedArray appearance = null;
int ap = a.getResourceId(
R.styleable.TextViewAppearance_textAppearance, -1);
a.recycle();
if (ap != -1) {
appearance = theme.obtainStyledAttributes(
ap, R.styleable.TextAppearance);
}
if (appearance != null) {
int n = appearance.getIndexCount();
for (int i = 0; i < n; i++) {
int attr = appearance.getIndex(i);
switch (attr) {
case R.styleable.TextView_text:
text = a.getText(attr);
break;
}
}
}
mPaint = new TextPaint();
mPaint.setColor(getResources().getColor(R.color.red));
mPaint.setTextSize(DensityUtil.dip2px(getContext(), textSize));
setText(text);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawText(text.toString(), getWidth() / 2, getHeight() / 2, mPaint);
}
public CharSequence getText() {
return text;
}
public void setText(CharSequence text) {
this.text = text;
invalidate();
}
public int getTextSize() {
return textSize;
}
public void setTextSize(int textSize) {
this.textSize = textSize;
mPaint.setTextSize(DensityUtil.dip2px(getContext(),textSize));
invalidate();
}
}
步骤6:定义布局文件:
步骤7:改变字号和内容:
textView = (LincolnTextView) findViewById(R.id.lincolnTextView);
button = (Button)findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
textView.setText("BBBB");
textView.setTextSize(20);
}
});
Github源码:
View_2_TextView
------------------第一节到此结束,敬请期待下一节分析---------------------