您的位置:首页 > 其它

绘制文本居中

2015-12-16 17:46 316 查看
x 是文本区域中心,y是文本区域下面的y坐标

这个大神写的好
http://blog.csdn.net/carrey1989/article/details/10399727
package com.example.customview01.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.Paint.FontMetrics;

import android.graphics.Rect;

import android.util.AttributeSet;

import android.util.Log;

import android.util.TypedValue;

import android.view.View;

import com.example.customview01.R;

public class CustomTitleView extends View {

private Context context;

private int textcolor;

private String text;

private int textSize;

private Paint mPaint;

private Rect textBound = new Rect();

public CustomTitleView(Context context) {

super(context);

// TODO Auto-generated constructor stub

}

public CustomTitleView(Context context, AttributeSet attrs) {

this(context, attrs, 0);

}

public CustomTitleView(Context context, AttributeSet attrs, int defStyle) {

super(context, attrs, defStyle);

this.context = context;

TypedArray typearrs = context.getTheme().obtainStyledAttributes(attrs,

R.styleable.CustomTitleView, defStyle, 0);

textcolor = typearrs

.getColor(R.styleable.CustomTitleView_titleColor, 0);

text = typearrs.getString(R.styleable.CustomTitleView_titleText);

textSize = typearrs.getDimensionPixelOffset(

R.styleable.CustomTitleView_titleTextSize, (int) TypedValue

.applyDimension(TypedValue.COMPLEX_UNIT_SP, 16,

getResources().getDisplayMetrics()));

Log.i("lmj", textcolor + "");

Log.i("lmj", text + "");

Log.i("lmj", textSize + "");

mPaint = new Paint();

// 得到字体的宽度

mPaint.setTextSize(textSize);

mPaint.getTextBounds(text, 0, text.length(), textBound);

}

@Override

protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

// TODO Auto-generated method stub

super.onMeasure(widthMeasureSpec, heightMeasureSpec);

int widthMode = MeasureSpec.getMode(widthMeasureSpec);

int widthSize = MeasureSpec.getSize(widthMeasureSpec);

int heightMode = MeasureSpec.getMode(heightMeasureSpec);

int heightSize = MeasureSpec.getSize(heightMeasureSpec);

if (widthMode == MeasureSpec.EXACTLY) {

} else {

widthSize = getPaddingLeft() + textBound.width()

+ getPaddingRight();

}

if (heightMode == MeasureSpec.EXACTLY) {

} else {

heightSize = getPaddingTop() + textBound.height()

+ getPaddingBottom();

}

// 设置整个view 的宽度和高度

Log.i("lmj", widthSize + "sw");

Log.i("lmj", heightSize + "sh");

setMeasuredDimension(widthSize, heightSize);

}

@Override

protected void onDraw(Canvas canvas) {

// TODO Auto-generated method stub

super.onDraw(canvas);

Log.i("lmj", getWidth() + "w");

Log.i("lmj", getHeight() + "h");

Log.i("lmj", textBound.width() + "tw");

Log.i("lmj", textBound.height() + "th");

Log.i("lmj", getMeasuredWidth() + "gw");

mPaint.setColor(Color.GREEN);

canvas.drawRect(0, 0, getMeasuredWidth(), getMeasuredHeight(), mPaint);

mPaint.setColor(textcolor);

// 绘制文本的区域的,中间x坐标

int x = getWidth() / 2;

// float y = getHeight() / 2 - textBound.height() / 2;

FontMetrics fm = mPaint.getFontMetrics();

mPaint.setAntiAlias(true);

mPaint.setTextAlign(Align.CENTER);

//

float y = getHeight() / 2 - fm.descent + (fm.bottom - fm.top) / 2;

Log.i("lmj", x + "x");

Log.i("lmj", y + "y");

canvas.drawText(text, x, (int) y, mPaint);

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: