Wrap text when using a canvas painting realized

private void drawText(String str,Paint pa,Canvas ca) {

float[] widths ;

float toalwidth = 0 ;

int mTextXPoint = 10; // text starting point X

int mTextYPoint = 10; // text starting point Y

widths = new float[50];

int index = 0;

int count = 1 ;

float height = pa.getTextSize();

pa.getTextWidths(str, widths);

for(int i=0 ;i< 50 ;i++) {

Log.e(TAG, "toalwidth =" + toalwidth);

toalwidth = toalwidth + widths[i] ;

if((( mWidth -20 ) < toalwidth) || (widths[i] == 0)) {

String text = str.substring(index, i);

Log.e(TAG, "text =" + text +"  i=" +i +"  index=" +index);

index = i;

if(toalwidth >( mWidth -20 ) ) {

ca.drawText(text, mTextXPoint , mTextYPoint + count *(height + 10), pa);

}else 

ca.drawText (text, (mWidth - toalwidth) / 2, mTextYPoint + count * (height + 10), pa); // mWidth is the width of the screen. Assurance center

count ++ ;

toalwidth = 0;

if(widths[i] == 0)

return ;

}

}

}

Guess you like

Origin blog.csdn.net/dxh040431104/article/details/6157349