在APP中,因为绘制的字体在图像之上,用控件不好操作,所以只好自己画图了。
在APP中,本来有很多控件,并且有高亮点。
画图线程一般来说,不宜有过多的计算,故增加一个items类,以操作item对象池。
以下为run中的draw函数中的一部分:
Paint mPaint
=
new
Paint();
mPaint.setAntiAlias( true );
mPaint.setTextSize( 18 );
mPaint.setColor(Color.WHITE);
mPaint.setAntiAlias( true );
mPaint.setTextSize( 18 );
mPaint.setColor(Color.WHITE);
if
(gameSelectItems.getPic(i)
!=
null
)
{
canvas.drawBitmap(gameSelectItems.getPic(i), gameSelectItems.getRect(i).left,
gameSelectItems.getRect(i).top, null );
}
if (gameSelectItems.getStr(i) != null )
{
canvas.drawText(gameSelectItems.getStr(i), gameSelectItems.getRect(i).left,
gameSelectItems.getRect(i).top, mPaint);
}
{
canvas.drawBitmap(gameSelectItems.getPic(i), gameSelectItems.getRect(i).left,
gameSelectItems.getRect(i).top, null );
}
if (gameSelectItems.getStr(i) != null )
{
canvas.drawText(gameSelectItems.getStr(i), gameSelectItems.getRect(i).left,
gameSelectItems.getRect(i).top, mPaint);
}
嗯?!发现字没有写在图片上,而是显示的基线在Top点上(也就是Y轴上)。。。
奇怪的东西。
想了想,是不是基线对齐是在Y轴?
改用以下试验:
if
(gameSelectItems.getPic(i)
!=
null
)
{
canvas.drawBitmap(gameSelectItems.getPic(i), gameSelectItems.getRect(i).left,
gameSelectItems.getRect(i).top, null );
}
if (gameSelectItems.getStr(i) != null )
{
canvas.drawText(gameSelectItems.getStr(i), gameSelectItems.getRect(i).left,
gameSelectItems.getRect(i).bottom, mPaint);
}
{
canvas.drawBitmap(gameSelectItems.getPic(i), gameSelectItems.getRect(i).left,
gameSelectItems.getRect(i).top, null );
}
if (gameSelectItems.getStr(i) != null )
{
canvas.drawText(gameSelectItems.getStr(i), gameSelectItems.getRect(i).left,
gameSelectItems.getRect(i).bottom, mPaint);
}
发现字刚好写在图片下边框上。
看来,写字默认是左对齐,且基线在Y轴。
这样很好呀,不用去计算字体高度,不用担心不同语种字体上下偏移的问题。
那么如果X轴能居中对齐的话,就更完美了,试试看:
Paint mPaint
=
new
Paint();
mPaint.setAntiAlias( true );
mPaint.setTextSize( 18 );
mPaint.setTextAlign(Align.CENTER);
mPaint.setColor(Color.WHITE);
mPaint.setAntiAlias( true );
mPaint.setTextSize( 18 );
mPaint.setTextAlign(Align.CENTER);
mPaint.setColor(Color.WHITE);
这么一来,发现在drawText(String,float X,float Y....)中,
X就是字符串的中间轴,
Y是字符串的基线,
如此完美!