package
cc.coin.view;
import
android.content.Context;
import
android.graphics.Canvas;
import
android.graphics.Paint;
import
android.graphics.Rect;
import
android.util.AttributeSet;
import
android.util.TypedValue;
import
android.widget.TextView;
/**
* 可根据字符串所占的长度(非字符串长度)自动缩小字体大小, 以适应显示区域的宽度
*
* @author sheng
*
*/
public
class
CustomTextView
extends
TextView {
private
Paint testPaint;
private
float
cTextSize;
public
CustomTextView (Context context, AttributeSet attrs) {
super
(context, attrs);
}
/**
* Re size the font so the specified text fits in the text box * assuming
* the text box is the specified width.
* 在此方法中学习到:getTextSize返回值是以像素(px)为单位的,而setTextSize()是以sp为单位的,
* 因此要这样设置setTextSize(TypedValue.COMPLEX_UNIT_PX, size);
*/
private
void
refitText(String text,
int
textWidth) {
if
(textWidth >
0
) {
testPaint =
new
Paint();
testPaint.set(
this
.getPaint());
int
availableWidth = textWidth -
this
.getPaddingLeft()
-
this
.getPaddingRight();
float
[] widths =
new
float
[text.length()];
Rect rect =
new
Rect();
testPaint.getTextBounds(text,
0
, text.length(), rect);
int
textWidths = rect.width();
cTextSize =
this
.getTextSize();
while
(textWidths > availableWidth){
cTextSize = cTextSize -
1
;
testPaint.setTextSize(cTextSize);
textWidths = testPaint.getTextWidths(text, widths);
}
this
.setTextSize(TypedValue.COMPLEX_UNIT_PX, cTextSize);
}
};
@Override
protected
void
onDraw(Canvas canvas) {
super
.onDraw(canvas);
refitText(getText().toString(),
this
.getWidth());
}
}