Это работает:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
protected void onResume() {
super.onResume();
Animator anim = bouncingStringIntoViewGroup((ViewGroup)findViewById(R.id.bouncer), Color.RED, 30, "A bouncy string.", 3000);
//here you can add a another listener to anim if you want (a listener could manipulate the views by set ids).
anim.start();
}
private Animator bouncingStringIntoViewGroup(ViewGroup bouncingTextContainer, final int firstLetterColor, final float size, final String vls, int duration){
Context context = bouncingTextContainer.getContext();
final FrameLayout textViewHolder = new FrameLayout(context);
final TextView textView = new TextView(context);
final TextView helper = new TextView(context);
textViewHolder.addView(helper, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
textViewHolder.addView(textView);
final int length = vls.length();
SpannableString textViewString = new SpannableString(vls);
textViewString.setSpan(new ForegroundColorSpan(Color.TRANSPARENT), 0, length, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
SpannableString helperString = new SpannableString(vls);
helperString.setSpan(new ForegroundColorSpan(Color.TRANSPARENT), 1, length, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
helperString.setSpan(new ForegroundColorSpan(firstLetterColor), 0, 1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
textView.setTextSize(size);
helper.setTextSize(0);
helper.setText(helperString);
setupCreatedViews(bouncingTextContainer, textViewHolder, textView, helper);
final int intSize = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, size, getResources().getDisplayMetrics());
final int stepDuration = duration/length;
ObjectAnimator anim = ObjectAnimator.ofFloat(helper, "textSize", 0, size).setDuration(stepDuration);
anim.setRepeatCount(length);
anim.addListener(new Animator.AnimatorListener() {
int at = 1;
@Override
public void onAnimationStart(Animator animation) {}
@Override
public void onAnimationEnd(Animator animation) {}
@Override
public void onAnimationCancel(Animator animation) {}
@Override
public void onAnimationRepeat(Animator animation) {
SpannableString textViewString = new SpannableString(vls);
textViewString.setSpan(new ForegroundColorSpan(firstLetterColor), 0, 1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
textViewString.setSpan(new ForegroundColorSpan(Color.TRANSPARENT), at, length, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
textView.setText(textViewString);
helper.setTextSize(0);
SpannableString helperString = new SpannableString(vls);
helperString.setSpan(new ForegroundColorSpan(Color.TRANSPARENT), 0, at, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
helperString.setSpan(new TextAppearanceSpan(null, 10, intSize, null, null), 0, at, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
if(at!=length) helperString.setSpan(new ForegroundColorSpan(Color.TRANSPARENT), at + 1, length, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
helper.setText(helperString);
at++;
}
});
return anim;
}
private void setupCreatedViews(ViewGroup containerForAnimatedText, FrameLayout subcontainer, TextView textView, TextView textViewFadeinLetter){
containerForAnimatedText.addView(subcontainer, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
//here you can set colors, ids, padding etc...
}