绘制文本居中

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);


}


}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值