绘制文本居中
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);
}
}
这个大神写的好
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);
}
}
相关文章推荐
- JAVA中使用Formatter类进行输出格式的控制
- Redis同步(主从复制)
- 【jQuery】prev + next选择器
- 文章标题
- iOS学习之驾照题库(练习页面跳转,传值,UITableView,原生json解析)
- 统计dex或jar文件中的方法
- 时区日期处理及定时 (NSDate,NSCalendar,NSTimer,NSTimeZone) -- IOS(实例)
- java 单例设计模式
- HDU1285(拓扑排序+优先队列)
- Switched to NOHz mode on CPU
- 混合APP开发框架资料汇总
- sql SYS对象集合
- ovs-agent流程
- 服务器上的 Git - 生成 SSH 公钥
- mahout in Action研读(1)-给用户推荐图书
- ubuntu apache2 ssl配置
- 宇宙生命进化的八大密度
- 【jQuery】parent > child选择器
- 在iOS上实现一个简单的日历控件
- Myeclipse *.link用法