【发布时间】:2011-01-05 21:07:37
【问题描述】:
你能告诉我如何在文本字符串中为字母设置动画吗?例如,对于字符串“Hello World”,首先将 H 从 0 缩放到 size,然后是 e,然后是 l 等等?如果您知道我的意思,就像弹跳文本效果一样。
我希望字符串中的第一个字母使用不同的颜色。
我确实知道如何为整个视图设置动画或在画布上绘制文本,但这样可以为整个文本字符串而不是字母设置动画。
【问题讨论】:
你能告诉我如何在文本字符串中为字母设置动画吗?例如,对于字符串“Hello World”,首先将 H 从 0 缩放到 size,然后是 e,然后是 l 等等?如果您知道我的意思,就像弹跳文本效果一样。
我希望字符串中的第一个字母使用不同的颜色。
我确实知道如何为整个视图设置动画或在画布上绘制文本,但这样可以为整个文本字符串而不是字母设置动画。
【问题讨论】:
我知道这可能不是最好的解决方案,但我们开始吧。由于您不能添加文本以外的任何内容来让我们说一个 textView。你需要插入一个 CharSequence,你应该看看 SpannableString。
Displaying emoticons in Android
考虑到上面的问题,您可能希望使用一些 css 样式为每个字母添加一个 Html 标记。然后使用 fromHtml 您可以将其转换并放入您的 textView 中。
【讨论】:
通过使用两个 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>
【讨论】:
这行得通:
@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...
}
【讨论】: