我正在尝试实现一些关于圆圈的文本。我现在有两个向量,一个用来保存字符串,另一个用来保存Drawables。
在我的onDraw()方法中,我遍历每个可绘制的图形并将其绘制到画布上。我想找到一种方法来关联字符串向量元素和可绘制向量中的元素。
例如:用户点击圆圈3,然后他们点击将文本添加到圆圈中,一旦这些步骤完成,圆圈3就知道它对应的文本在Strings向量的元素3中。因此,在onDraw()中,可以将可绘制的向量绘制到画布上,然后是保存每个圆的文本的字符串……
我现在有一些代码:
用户单击将文本添加到圆圈中,弹出一个对话框,要求用户在圆圈中输入他们想要的文本。
stringsVector.setSize(vecForShapes.size());//set the Strings vector to the size of the Drawable vector, because there will never be more text than circles
stringsVector.add(circleID, circleText);//add circleText to the Strings vector at position circleID.
现在,在我的onDraw()中,我重新绘制了这两个向量,并将文本放在该圆的边界上:
for(int i =0;i<_vecForShapes.size();i++)
{
Rect bounds = ((Drawable) _vecForShapes.get(i)).getBounds();//get circle i
String tempString = stringVect.get(i);//get string i
if(tempString !=null)
if(tempString.length() > 17)
{
canvas.drawText(tempString, 0, 11, bounds.left+10, bounds.top+30, colour);
canvas.drawText(tempString, 11,tempString.length(), bounds.left+5, bounds.top+40, colour);
}//end if
else
{
canvas.drawText(tempString,0,tempString.length(), bounds.left+5, bounds.top+40, colour);
}
}
我知道可能有很多更好的方法来做到这一点,但我现在改变这一点太远了,但我仍然愿意听取人们的意见,所以我下次会知道得更好。
谢谢!
发布于 2012-03-22 15:16:33
使用
Canvas.drawPath(Path path, Paint paint)
和
Canvas.drawTextOnPath(String text, Path path, float hOffset, float vOffset, Paint paint)
方法来实现您正在寻找的东西。
https://stackoverflow.com/questions/9807885
复制相似问题