Eclipse Android:我希望首先出现的字母更大,然后变小

时间:2014-05-23 15:10:53

标签: android eclipse animation

在我的活动中是一个内容为“Ecl pse”的TextView,其中缺少“i”。单击按钮后,我希望字母“i”出现在正确的位置。但是“i”应该是如此生动,它首先看起来有点大,然后变小,直到它点击按钮后的其他字母一样大。但是在我的代码中,整个单词“Ecl pse”受到ValueAnimation的影响。点击按钮后不仅“i”更大而且变小了,而且整个单词“Ecl pse”的位置也发生变化:“Ecl pse”下降然后返回到原点位置。我该如何解决这个问题?再一次,我想只有字母“i”看起来有点大一点,然后变小,直到它点击按钮后它与“Ecl pse”这个单词的其他字母一样大......

XML:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >



    <TextView
        android:id="@+id/tvhallo"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="87dp"
        android:text="Ecl pse"
        android:textSize="70sp" />

    <Button
        android:id="@+id/BtnKlick"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:text="Klick" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/tvhallo"
        android:layout_alignBottom="@+id/tvhallo"
        android:layout_alignLeft="@+id/BtnKlick"
        android:layout_toLeftOf="@+id/BtnKlick"
        android:text="i"
        android:textSize="70sp" />

</RelativeLayout>

CODE:

public Button btn;
    public TextView tw;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.pagetwo);

        btn = (Button) findViewById(R.id.BtnKlick);
        tw = (TextView) findViewById(R.id.tvhallo);

        btn.setOnClickListener(this);}

        @SuppressLint("NewApi")
    public void onClick(View v) {
        // TODO Auto-generated method stub

        ValueAnimator animator = new ValueAnimator();
        animator.setDuration(2000);
        animator.setObjectValues("Ecl pse", "Eclipse");

        animator.setEvaluator(new TypeEvaluator<CharSequence>()
        {
            @Override
            public CharSequence evaluate(float fraction, CharSequence startValue, CharSequence endValue)
            {
                float relativeSize = 4 - 3 * fraction;
                Spannable span = new SpannableString("Eclipse");
                span.setSpan(new RelativeSizeSpan(relativeSize), 3, 4, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                return span;
            }
        });

        animator.addUpdateListener(new AnimatorUpdateListener()
        {
            @Override
            public void onAnimationUpdate(ValueAnimator animation)
            {
                tw.setText((CharSequence)animation.getAnimatedValue());
            }
        });

        animator.start();}}

1 个答案:

答案 0 :(得分:1)

我真的怀疑TextView是你正在努力实现的目标的正确工具。对于这项工作来说,这是过度杀伤和不足。它可以显示具有各种格式的所有文本内容,并且有充分的理由描述为“完整的文本编辑器”。它的主要目的是提供一种显示文本的方法,可能还有很多文本(这部分是过度杀伤)。插入和(可能)缩放'i'将导致其余字母移动,因为这是该类的用途。它旨在布局文本,而不是以一种导致内容更改尽可能少的方式静态布局(这是不充分的部分)

但你真正想要的不是显示文字,而是动画。我建议您在任何具有各种状态的成像程序中创建所需的文本:一个没有'i',一个太大而且缩小,以及一个正确比例的最终图像。现在使用这些图像显示按下按钮触发的动画。我相信这个任务的正确类是AnimationDrawable,尽管我自己没有使用它。如何获得你想要的确切行为肯定有很多方法,但我希望这能让你开始!