Skip to content

Add text alpha support for EvaporateText and fixed a bug #72

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import android.animation.Animator;
import android.animation.ValueAnimator;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Rect;
import android.text.Layout;
import android.util.AttributeSet;
Expand All @@ -27,6 +28,7 @@ public class EvaporateText extends HText {
float charTime = 300;
int mostCount = 20;
private int mTextHeight;
private int paintColor = Color.BLACK;

private List<CharacterDiffResult> differentList = new ArrayList<>();
private long duration;
Expand Down Expand Up @@ -111,16 +113,19 @@ protected void drawFrame(Canvas canvas) {
//
float pp = progress * duration / (charTime + charTime / mostCount * (mText.length() - 1));

mOldPaint.setColor(paintColor);
mPaint.setColor(paintColor);

mOldPaint.setTextSize(mTextSize);
int move = CharacterUtils.needMove(i, differentList);
if (move != -1) {
mOldPaint.setAlpha(255);
mOldPaint.setAlpha(Color.alpha(paintColor));
float p = pp * 2f;
p = p > 1 ? 1 : p;
float distX = CharacterUtils.getOffset(i, move, p, startX, oldStartX, gapList, oldGapList);
canvas.drawText(mOldText.charAt(i) + "", 0, 1, distX, startY, mOldPaint);
} else {
mOldPaint.setAlpha((int) ((1 - pp) * 255));
mOldPaint.setAlpha((int) ((1 - pp) * Color.alpha(paintColor)));
float y = startY - pp * mTextHeight;
float width = mOldPaint.measureText(mOldText.charAt(i) + "");
canvas.drawText(mOldText.charAt(i) + "", 0, 1, oldOffset + (oldGapList.get(i) - width) / 2, y, mOldPaint);
Expand All @@ -133,8 +138,8 @@ protected void drawFrame(Canvas canvas) {

if (!CharacterUtils.stayHere(i, differentList)) {

int alpha = (int) (255f / charTime * (progress * duration - charTime * i / mostCount));
alpha = alpha > 255 ? 255 : alpha;
int alpha = (int) ((float) Color.alpha(paintColor) / charTime * (progress * duration - charTime * i / mostCount));
alpha = alpha > Color.alpha(paintColor) ? Color.alpha(paintColor) : alpha;
alpha = alpha < 0 ? 0 : alpha;

mPaint.setAlpha(alpha);
Expand All @@ -151,4 +156,8 @@ protected void drawFrame(Canvas canvas) {
}
}

}
public void setTextColor(int color) {
this.paintColor = color;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,14 @@ public void animateText(CharSequence text) {
evaporateText.animateText(text);
}

@Override
public void setTextColor(int color) {
evaporateText.setTextColor(color);
}

@Override
protected void onDraw(Canvas canvas) {
// super.onDraw(canvas);
evaporateText.onDraw(canvas);
}
}
}