importandroid.content.Context;importandroid.graphics.Canvas;importandroid.graphics.Color;importandroid.graphics.Paint;importandroid.graphics.Rect;importandroid.support.annotation.Nullable;importandroid.text.TextUtils;importandroid.util.AttributeSet;importandroid.view.View;importcom.shoneworn.supermvp.uitls.NumberUtils;/*** Created by chenxiangxiang on 2019/5/16.
*
*@authorshoneworn
* @只支持单个文字的跳动,只想支持数字。*/
public class SingleScrollNumberView extendsView {private String mText = "";private int mTextColor =Color.BLACK;private int mTextSize = 48;private int textPadding = 6;privatePaint mPaint;privateContext mContext;private static String BASE_NUMBER = "0123456789";/*** 由于只想用一个view完事,但是文字绘制的时候,不带边距,只好在measure的时候,用大字体占个大位置。在onDraw的时候,用小字体。这样文字就自带了边距*/
private inttextOutHeight;private inttextOutWidth;private inttextInnerWidth;private int scrollY = 0;private int scale = 2;publicSingleScrollNumberView(Context context) {this(context, null);
}publicSingleScrollNumberView(Context context, @Nullable AttributeSet attrs) {this(context, attrs, 0);
}public SingleScrollNumberView(Context context, @Nullable AttributeSet attrs, intdefStyleAttr) {super(context, attrs, defStyleAttr);this.mContext =context;
initPaint();
}private voidinitPaint() {
mPaint= newPaint();//设置抗锯齿
mPaint.setAntiAlias(true);//设置字体和颜色
mPaint.setTextSize(mTextSize *scale);
mPaint.setColor(mTextColor);
setPadding(textPadding, textPadding, textPadding, textPadding);
}publicSingleScrollNumberView setText(String text) {this.mText =text;return this;
}public SingleScrollNumberView setTextColor(inttextColor) {this.mTextColor =textColor;
mPaint.setColor(mTextColor);return this;
}public SingleScrollNumberView setTextSize(inttextSize) {this.mTextSize =textSize;
mPaint.setTextSize(mTextSize*scale);return this;
}public SingleScrollNumberView setTextPadding(intpadding) {this.textPadding =padding;
setPadding(textPadding, textPadding, textPadding, textPadding);return this;
}
@Overrideprotected void onMeasure(int widthMeasureSpec, intheightMeasureSpec) {int widthMode =MeasureSpec.getMode(widthMeasureSpec);int heightMode =MeasureSpec.getMode(heightMeasureSpec);int width =MeasureSpec.getSize(widthMeasureSpec);int height =MeasureSpec.getSize(heightMeasureSpec);if (widthMode ==MeasureSpec.AT_MOST) {
Rect bounds= newRect();
mPaint.getTextBounds(mText,0, mText.length(), bounds);
textOutWidth=bounds.width();
width= bounds.width() + getPaddingLeft() +getPaddingRight();
}if (heightMode ==MeasureSpec.AT_MOST) {
Rect bounds= newRect();
mPaint.getTextBounds(mText,0, mText.length(), bounds);
textOutHeight=bounds.height();
height= textOutHeight + getPaddingTop() +getPaddingBottom();
}//设置宽高
setMeasuredDimension(width, height);
}
@Overrideprotected voidonDraw(Canvas canvas) {super.onDraw(canvas);if (TextUtils.isEmpty(mText)) return;if (mText.length() > 1) return;if (!BASE_NUMBER.contains(mText)) return;//保持垂直方向居中//getPaddingLeft() + (textOutWidth - textInnerWidth) / 2 保持水平方向居中
mPaint.setTextSize(mTextSize);
Paint.FontMetricsInt fontMetrics=mPaint.getFontMetricsInt();int dy = (fontMetrics.bottom - fontMetrics.top) / 2 -fontMetrics.bottom;int baseLine = getHeight() / 2 +dy;for (int i = 0; i <= NumberUtils.toInt(mText); i++) {
mPaint.setTextSize(mTextSize);
Rect innerBounds= newRect();
mPaint.getTextBounds(mText,0, mText.length(), innerBounds);
textInnerWidth=innerBounds.width();
canvas.drawText(String.valueOf(i), getPaddingLeft()+ (textOutWidth - textInnerWidth) / 2, baseLine + i * textOutHeight -scrollY, mPaint);
}
}private voidanimateView() {this.postDelayed(newRunnable() {
@Overridepublic voidrun() {//最大数字出现后,就要停下来。还要停稳,不然多几个数字,就会出现高低不平了。
if (scrollY >= textOutHeight *NumberUtils.toInt(mText)) {
scrollY= textOutHeight *NumberUtils.toInt(mText);
SingleScrollNumberView.this.postInvalidate();return;
}
scrollY+= textOutHeight / 4;
SingleScrollNumberView.this.postInvalidate();
animateView();
}
},8);
}public voidstart() {
scrollY= 0;
animateView();
}
}