1. View绘制
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Align;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.util.Log;
import android.util.TypedValue;
import android.view.View;
public class TextViewVertical extends View {
private int mTextPosx = 0;// x坐标
private int mTextPosy = 0;// y坐标
/**
* 文本
*/
private String mText;
/**
* 文本的颜色
*/
private int mTextColor;
/**
* 文本的大小
*/
private int mTextSize;
/**
* 绘制时控制文本绘制的范围
*/
private Rect mBound;
private Paint mPaint;
public TextViewVertical(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
/**
* 获得我们所定义的自定义样式属性
*/
TypedArray a = context.getTheme().obtainStyledAttributes(attrs, R.styleable.VerticalTextView, defStyle, 0);
int n = a.getIndexCount();
for (int i = 0; i<n; i++)
{
int attr = a.getIndex(i);
switch (attr)
{
case R.styleable.VerticalTextView_text:
mText = a.getString(attr);
break;
case R.styleable.VerticalTextView_textColor:
// 默认颜色设置为黑色
mTextColor = a.getColor(attr, Color.DKGRAY);
break;
case R.styleable.VerticalTextView_textSize:
// 默认设置为16sp,TypeValue也可以把sp转化为px
mTextSize = a.getDimensionPixelSize(attr, (int) TypedValue.applyDimension(
TypedValue.COMPLEX_UNIT_SP, 18, getResources().getDisplayMetrics()));
break;
}
}
a.recycle();
init();
}
public TextViewVertical(Context context, AttributeSet attrs) {
this(context, attrs,0);
}
public TextViewVertical(Context context) {
this(context, null);
}
private void init() {
mPaint = new Paint();
mPaint.setTextSize(mTextSize);
mPaint.setAntiAlias(true);//平滑处理
mPaint.setColor(mTextColor);//默认文字颜色
mPaint.setTextAlign(Align.CENTER);
mBound = new Rect();
mPaint.getTextBounds(mText, 0, mText.length(), mBound);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
Log.e("TextViewVertical", "onDraw");
mTextPosx=getMeasuredWidth()/2;
mTextPosy=0;
for (int i = 0; i < mText.length(); i++) {
char ch = mText.charAt(i);
Paint.FontMetrics fm = mPaint.getFontMetrics();
mTextPosy = (int) (mTextPosy+(-fm.top));
canvas.drawText(String.valueOf(ch), mTextPosx, mTextPosy, mPaint);
}
}
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
{
int widthMode = MeasureSpec.getMode(widthMeasureSpec);
int widthSize = MeasureSpec.getSize(widthMeasureSpec);
int heightMode = MeasureSpec.getMode(heightMeasureSpec);
int heightSize = MeasureSpec.getSize(heightMeasureSpec);
int width;
int height ;
if (widthMode == MeasureSpec.EXACTLY)
{
width = widthSize;
} else
{
float textWidth = mBound.width()/mText.length()+mBound.width()%mText.length();
int desired = (int) (getPaddingLeft() + textWidth + getPaddingRight());
width = desired;
}
if (heightMode == MeasureSpec.EXACTLY)
{
height = heightSize;
} else
{
float textHeight=0;
for (int i = 0; i < mText.length(); i++) {
Paint.FontMetrics fm = mPaint.getFontMetrics();
textHeight = textHeight-fm.top;
}
int desired = (int) (getPaddingTop() + textHeight + getPaddingBottom());
height = desired;
}
setMeasuredDimension(width, height);
}
}
2. 添加自己的属性说明
在values 下的attr(没有则创建)里添加
<declare-styleable name="VerticalTextView">
<attr name="text"/>
<attr name="textColor" />
<attr name="textSize" />
</declare-styleable>
3.布局中使用
<com.example.test.TextViewVertical
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
app:text="历史记录"
app:textColor="@color/colorSecondText"
app:textSize="18sp"
android:visibility="visible"
/>