你能给我一个提示如何在文本字符串中设置字母动画吗?例如,对于字符串“Hello World”,首先将H从0缩放到大小,然后是e,然后是l等?如果你知道我的意思,就像一个弹跳的文字效果。
我会将字符串中的第一个字母用不同的颜色。
我确实知道如何为整个视图设置动画或在画布上绘制文本,但这样可以为整个文本字符串设置动画,而不是字母。
答案 0 :(得分:1)
我知道这可能不是最好的解决方案,但我们走了。因为你不能添加任何文本而不是文本。你需要插入一个CharSequence,你应该看一下SpannableString。
Displaying emoticons in Android
考虑到上述问题,您可能希望使用某些css样式为每个字母添加Html标记。然后使用fromHtml你可以转换它并将它放在textView中。
答案 1 :(得分:1)
通过使用两个TextView控件找到了一种简单的方法。首先将mytext.substring(0,1)设置为单词的第一个后面,将第二个设置为mytext.substring(1)。
为两者执行startAnimation时,您可以为第一个视图创建一个动画,为第二个视图创建另一个动画。
在布局文件上,我把它们放在RelativeLayout上彼此相邻:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/MainLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:id="@+id/txtCaption"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="80dp"
android:textColor="#FFFFFF"
android:text="est"
android:layout_centerHorizontal="true"
android:layout_alignParentBottom="true"
/>
<TextView
android:id="@+id/txtCaptionFirstLetter"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="70dp"
android:textColor="#00FF00"
android:textStyle="bold"
android:text="T"
android:layout_toLeftOf="@+id/txtImageCaption"
android:layout_alignBottom="@+id/txtImageCaption"
/>
</RelativeLayout>
答案 2 :(得分:0)
这有效:
@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...
}