我想在中心(水平和垂直)将文本绘制成矩形。如果有太多的文本裁剪它不适合矩形。
我已经尝试按照这个例子展示它,但没有运气。
有任何想法吗?
尝试这个
private void drawRectText(String text, Canvas canvas, Rect r) {
textPaint.setTextSize(20);
textPaint.setTextAlign(Align.CENTER);
int width = r.width();
int numOfChars = textPaint.breakText(text,true,width,null);
int start = (text.length()-numOfChars)/2;
canvas.drawText(text,start,start+numOfChars,r.exactCenterX(),r.exactCenterY(),textPaint);
}
这个功能对我有用。
private void drawDigit(Canvas canvas, int textSize, float cX, float cY, int color, String text) {
Paint tempTextPaint = new Paint();
tempTextPaint.setAntiAlias(true);
tempTextPaint.setStyle(Paint.Style.FILL);
tempTextPaint.setColor(color);
tempTextPaint.setTextSize(textSize);
float textWidth = tempTextPaint.measureText(text);
//if cX and cY are the origin coordinates of the your rectangle
//cX-(textWidth/2) = The x-coordinate of the origin of the text being drawn
//cY+(textSize/2) = The y-coordinate of the origin of the text being drawn
canvas.drawText(text, cX-(textWidth/2), cY+(textSize/2), tempTextPaint);
}