-
-
Notifications
You must be signed in to change notification settings - Fork 811
/
AnvilText.java
164 lines (138 loc) · 5.38 KB
/
AnvilText.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package com.hanks.htextview.animatetext;
import android.animation.ValueAnimator;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Rect;
import android.view.animation.BounceInterpolator;
import com.hanks.htextview.R;
import com.hanks.htextview.util.CharacterUtils;
import java.lang.reflect.Field;
/**
* Created by hanks on 15-12-14.
*/
public class AnvilText extends HText {
private Paint bitmapPaint;
private Bitmap[] smokes = new Bitmap[50];
private float ANIMA_DURATION = 800;
private int mTextHeight = 0;
private int mTextWidth;
private float progress;
private Matrix mMatrix;
private float dstWidth;
@Override
protected void initVariables() {
bitmapPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
bitmapPaint.setColor(Color.WHITE);
bitmapPaint.setStyle(Paint.Style.FILL);
// load drawable
try {
for (int j = 0; j < 50; j++) {
String drawable;
if (j < 10) {
drawable = "wenzi000" + j;
} else {
drawable = "wenzi00" + j;
}
Resources resources = mHTextView.getResources();
int imgId = resources.getIdentifier(drawable, "drawable", mHTextView.getContext().getPackageName());
smokes[j] = BitmapFactory.decodeResource(resources, imgId);
}
} catch (Exception e) {
e.printStackTrace();
}
mMatrix = new Matrix();
}
@Override
protected void animateStart(CharSequence text) {
ValueAnimator valueAnimator = ValueAnimator.ofFloat(0, 1)
.setDuration((long) ANIMA_DURATION);
valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
progress = (float) animation.getAnimatedValue();
mHTextView.invalidate();
}
});
valueAnimator.start();
if (smokes.length > 0) {
mMatrix.reset();
dstWidth = mTextWidth * 1.2f;
if (dstWidth < 404f) dstWidth = 404f;
mMatrix.postScale(dstWidth / (float) smokes[0].getWidth(), 1f);
}
}
@Override
protected void animatePrepare(CharSequence text) {
Rect bounds = new Rect();
mPaint.getTextBounds(mText.toString(), 0, mText.length(), bounds);
mTextHeight = bounds.height();
mTextWidth = bounds.width();
}
@Override
protected void drawFrame(Canvas canvas) {
float offset = startX;
float oldOffset = oldStartX;
int maxLength = Math.max(mText.length(), mOldText.length());
float percent = progress;
boolean showSmoke = false;
for (int i = 0; i < maxLength; i++) {
// draw old text
if (i < mOldText.length()) {
mOldPaint.setTextSize(mTextSize);
int move = CharacterUtils.needMove(i, differentList);
if (move != -1) {
mOldPaint.setAlpha(255);
float p = percent * 2f;
p = p > 1 ? 1 : p;
float distX = CharacterUtils.getOffset(i, move, p, startX, oldStartX, gaps, oldGaps);
canvas.drawText(mOldText.charAt(i) + "", 0, 1, distX, startY, mOldPaint);
} else {
float p = percent * 2f;
p = p > 1 ? 1 : p;
mOldPaint.setAlpha((int) ((1 - p) * 255));
canvas.drawText(mOldText.charAt(i) + "", 0, 1, oldOffset, startY, mOldPaint);
}
oldOffset += oldGaps[i];
}
// draw new text
if (i < mText.length()) {
if (!CharacterUtils.stayHere(i, differentList)) {
showSmoke = true;
float interpolation = new BounceInterpolator().getInterpolation(percent);
mPaint.setAlpha(255);
mPaint.setTextSize(mTextSize);
float y = startY - (1 - interpolation) * mTextHeight * 2;
float width = mPaint.measureText(mText.charAt(i) + "");
canvas.drawText(mText.charAt(i) + "", 0, 1, offset + (gaps[i] - width) / 2, y, mPaint);
}
offset += gaps[i];
}
}
if (percent > 0.3 && percent < 1) {
if (showSmoke) {
drawSmokes(canvas, startX + (offset - startX) / 2f, startY - 50, percent);
}
}
}
private void drawSmokes(Canvas canvas, float x, float y, float percent) {
Bitmap b = smokes[0];
try {
int index = (int) (50 * percent);
if (index < 0) index = 0;
if (index >= 50) index = 49;
b = smokes[index];
} catch (Exception e) {
e.printStackTrace();
}
if (b != null) {
float dx = (mHTextView.getWidth() - dstWidth) / 2 > 0 ? (mHTextView.getWidth() - dstWidth) / 2 : 0;
canvas.translate(dx, (mHTextView.getHeight() - b.getHeight()) / 2);
canvas.drawBitmap(b, mMatrix, bitmapPaint);
}
}
}