作者:倔强的追求 | 来源:互联网 | 2023-09-03 12:48
首先申明,这篇文章不是我写的,我是对着大神的博客,对着写了一遍而已!感谢写这篇文章的作者,其原创博客地址为:http:www.jianshu.comp4d987769785
首先申明,这篇文章不是我写的,我是对着大神的博客,对着写了一遍而已!
感谢写这篇文章的作者,其原创博客地址为:
http://www.jianshu.com/p/4d987769785c
其示意图如下:
其自定义TextView的代码如下:
/**
* Created by pxw on 2017/6/12.
* 打印机效果TextView
*/
public class FadeInTextView extends TextView {
private StringBuffer mStringBuffer = new StringBuffer();
private Rect textRect = new Rect();
private int textCount;
private ValueAnimator textAnimation;
private int duration = 300;
private int currentIndex = -1;
private String[] arr;
private TextAnimationListener textAnimationListener;
public FadeInTextView(Context context) {
super(context);
}
public FadeInTextView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
}
public FadeInTextView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
public FadeInTextView setTextAnimationListener(TextAnimationListener textAnimationListener){
this.textAnimatiOnListener= textAnimationListener;
return this;
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
if(mStringBuffer != null) {
drawText(canvas,mStringBuffer.toString());
}
}
private void drawText(Canvas canvas, String str) {
textRect.left = getPaddingLeft();
textRect.top = getPaddingTop();
textRect.right = getWidth()-getPaddingRight();
textRect.bottom = getHeight()-getPaddingBottom();
TextPaint paint = getPaint();
Paint.FontMetrics fOntMetrics= paint.getFontMetrics();
int baseLine = (int) ((textRect.bottom+textRect.top-fontMetrics.bottom-fontMetrics.top)/2);
canvas.drawText(str,getPaddingLeft(),baseLine,paint);
}
private void initAnimation(){
textAnimation = ValueAnimator.ofInt(0,textCount-1);
textAnimation.setDuration(textCount*duration);
textAnimation.setInterpolator(new LinearInterpolator());
textAnimation.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
int index = (int) animation.getAnimatedValue();
Log.e("index",index+"");
if(currentIndex != index) {
mStringBuffer.append(arr[index]);
currentIndex = index;
Log.e("index",index+"index");
if(currentIndex == textCount -1) {
if(textAnimationListener != null) {
textAnimationListener.annimationFinish();
}
}
invalidate();
}
}
});
}
public FadeInTextView setTextString(String textString){
if(textString != null) {
textCount = textString.length();
arr = new String[textCount];
for (int i = 0; i
arr[i] = textString.substring(i,i+1);
}
initAnimation();
}
return this;
}
public FadeInTextView startFadeInAnimation(){
if(textAnimation != null) {
mStringBuffer.setLength(0);
currentIndex = -1;
textAnimation.start();
}
return this;
}
public FadeInTextView stopFadeInAnimation(){
if(textAnimation != null) {
textAnimation.end();
}
return this;
}
interface TextAnimationListener{
void annimationFinish();
}
}
调用方法为:
fadeInTextView.setTextString("自定义view实现字符串逐字显示")
.startFadeInAnimation()
.setTextAnimationListener(new FadeInTextView.TextAnimationListener() {
@Override
public void annimationFinish() {
}
});
以上,即可,
再次感谢原创博客作者