在开发中遇到一个这样的需求,在选择星期时设计狮想要这个这样的效果:
以上比较瞎扯,最终我是选择用自定义View继承CheckBox实现的,然而遇到的效果是这的:
“一”怎么了,为什么是在上面显示并没有对齐,而且旁边的“日”也没有居中显示。关键代码实现如下:
if (isChecked()) {
// 绘制背景圆
canvas.drawCircle(centerX, centerY, centerX-mPadding, mBgPaint);
// 设置文字画笔颜色
mFontPaint.setColor(mFontCheckColor);
// 获取文字TEXT,以及文字的Rect,来得到文字的高度和宽度
String text = getText().toString();
Rect fontRect = new Rect();
mFontPaint.getTextBounds(text, 0, text.length(), fontRect);
// 设置文字左下角的起始点坐标,绘制文字
canvas.drawText(text, centerX-fontRect.width()/2, centerY+fontRect.height()/2, mFontPaint);
}
@Override
protected void onDraw(Canvas canvas) {
// 设置文字画笔颜色
mFontPaint.setColor(mFontCheckColor);
// 获取文字TEXT,以及文字的Rect,来得到文字的高度和宽度
String text = getText().toString();
Rect fontRect = new Rect();
mFontPaint.getTextBounds(text, 0, text.length(), fontRect);
fontRect.offset(0, -fontRect.top);
canvas.drawText(text, 0, fontRect.bottom, mFontPaint);
Paint linePaint = new Paint();
linePaint.setStyle(Paint.Style.STROKE);
linePaint.setColor(Color.RED);
linePaint.setStrokeWidth(2);
canvas.drawRect(fontRect, linePaint);
}
getFontMetrics(),getFontMetricsInt()用于返回字符串的测量,而两个方法的区别就是返回值得类型。返回值一共有五个属性:
setTextAlign可以设置画笔绘制文本的对齐方式,选择center即可完成文本的水平居中。
mPaint.setTextAlign(Paint.Align.CENTER);
private void textCenter(String[] strings, Paint paint, Canvas canvas, Point point,Paint.Align align){
mPaint.setTextAlign(align);
Paint.FontMetrics fontMetrics= mPaint.getFontMetrics();
float top = fontMetrics.top;
float bottom = fontMetrics.bottom;
int length = strings.length;
float total = (length-1)*(-top+bottom)+(-fontMetrics.ascent+fontMetrics.descent);
float offset = total/2-bottom;
for (int i=0; i
public class FontBgCheckBox extends CheckBox {
private Paint mBgPaint;
private Paint mFontPaint;
private int mFontCheckColor, mFontUncheckColor, mBgCheckColor;
private int mPadding;
public FontBgCheckBox(Context context, AttributeSet attrs) {
super(context, attrs);
mFontCheckColor = 0xFFFFFFFF;
mFontUncheckColor = Color.parseColor("#FF616161");
mBgCheckColor = Color.parseColor("#FF50BDE7");
mPadding = dp2px(6, getContext());
mBgPaint = new Paint();
mBgPaint.setColor(mBgCheckColor);
mBgPaint.setAntiAlias(true);
mFontPaint = new Paint();
mFontPaint.setTextSize(dp2px(16, getContext()));
mFontPaint.setAntiAlias(true);
}
@Override
protected void onDraw(Canvas canvas) {
int centerX = getWidth() / 2;
int centerY = getHeight() / 2;
if (isChecked()) {
canvas.drawCircle(centerX, centerY, centerX - mPadding, mBgPaint);
mFontPaint.setColor(mFontCheckColor);
String text = getText().toString();
textCenter(new String[]{text}, mFontPaint, canvas, centerX, centerY, Align.CENTER);
} else {
mFontPaint.setColor(mFontUncheckColor);
String text = getText().toString();
textCenter(new String[]{text}, mFontPaint, canvas, centerX, centerY, Align.CENTER);
}
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
setMeasuredDimension(widthMeasureSpec, widthMeasureSpec);
}
public static int dp2px(float value, Context context) {
final float scale = context.getResources().getDisplayMetrics().densityDpi;
return (int) (value * (scale / 160) + 0.5f);
}
}
……
-