不断在TextView上设置动画并在TextView上不断更改文本使用“运行”方法和处理程序

时间:2015-09-18 11:34:00

标签: android textview android-animation

我有一个包含字符串值的字符串数组,它会在一段时间后逐一显示在TextView上。它停在数组的最后一个索引上。我想再次开始将TextView的文本从索引零更改为最后一个索引。

我有一个动画应用于上面提到的TextView。动画仅在开始时间显示其结果一次,之后它不显示其结果。 这是我的Java代码。

Animation animMove;

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    intelStoryTextView = (TextView) findViewById(R.id.intel_story_textview);

    doTask();

    animMove = AnimationUtils.loadAnimation(getApplicationContext(),
            R.anim.move);
}

public void doTask() {

    final String[] my_string = {"Relief Rally", "OEDC Upgrades ", "US Fed to HIKE ", "Probability "};
    final android.os.Handler handler = new android.os.Handler();
    handler.post(new Runnable() {

        int i = 0;

        @Override
        public void run() {
            animMove.setRepeatCount(Animation.INFINITE);
            intelStoryTextView.setAnimation(animMove);
            intelStoryTextView.setText(my_string[i++]);

            if (i == my_string.length) {
                i = 0;
                handler.removeCallbacks(this);

            } else {

                handler.postDelayed(this, 1000 * 5);
            }

        }
    });
}

这是我的动画代码。

<?xml version="1.0" encoding="utf-8"?>
<set
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:interpolator="@android:anim/linear_interpolator"
    android:fillAfter="true">

    <alpha
        android:duration="1000"
        android:fromAlpha="0.0"
        android:interpolator="@android:anim/accelerate_interpolator"
        android:toAlpha="1.0" />

    <translate
        android:duration="2000"
        android:fromXDelta="70%p"
        android:toXDelta="0%p"
        />

</set>

2 个答案:

答案 0 :(得分:1)

尝试下面的代码,我已经测试了它,它应该可以正常工作:

您的 move.xml

<?xml version="1.0" encoding="utf-8"?>
<set
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:interpolator="@android:anim/linear_interpolator"
    android:fillAfter="true">

    <alpha
        android:duration="2000"
        android:fromAlpha="0.0"
        android:interpolator="@android:anim/accelerate_interpolator"
        android:toAlpha="1.0"
        android:repeatCount="infinite"
        android:repeatMode="restart" /> <!-- i've added repeatCount and repeatMode attr -->

    <translate
        android:duration="2000"
        android:fromXDelta="70%p"
        android:toXDelta="0%p"
        android:repeatCount="infinite"
        android:repeatMode="restart"/> <!-- i've added repeatCount and repeatMode attr -->
</set>

您的活动:

    AnimationSet animMove; // i changed Animation to AnimationSet because i need the list of animations
    TextView intelStoryTextView;
    String[] my_string = {"Relief Rally", "OEDC Upgrades ", "US Fed to HIKE ", "Probability "};
    int position = 0; // a counter for your list of strings

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        intelStoryTextView = (TextView) findViewById(R.id.intel_story_textview);

        animMove = (AnimationSet) AnimationUtils.loadAnimation(getApplicationContext(),
                R.anim.move);

        // I get an animation from the animations list (i choose randomly the first one)
        Animation alphaAnim = animMove.getAnimations().get(0);
        // I add a listener to this animation to update text when repeating
        alphaAnim.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {
            }

            @Override
            public void onAnimationEnd(Animation animation) {
            }

            @Override
            public void onAnimationRepeat(Animation animation) {
                // here i do the update
                if (position >= my_string.length) {
                    position = 0;
                }

                intelStoryTextView.setText(my_string[position]);
                position++;
            }
        });

        // I start the animation
        intelStoryTextView.startAnimation(animMove);

    }

<强>更新

如果您想在重复动画之前暂停一段时间,可以使用Handler代替repeartCount标记:

move.xml

<?xml version="1.0" encoding="utf-8"?>
<set
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:interpolator="@android:anim/linear_interpolator"
    android:fillAfter="true">

    <alpha
        android:duration="2000"
        android:fromAlpha="0.0"
        android:interpolator="@android:anim/accelerate_interpolator"
        android:toAlpha="1.0" />

    <translate
        android:duration="2000"
        android:fromXDelta="70%p"
        android:toXDelta="0%p"/>
</set>

您的活动

    Animation mAnimMove;
    TextView mIntelStoryTextView;
    String[] mMy_string = {"Relief Rally", "OEDC Upgrades ", "US Fed to HIKE ", "Probability "};
    int mPosition = 0;
    int mInterval = 5000;
    Handler mHandler = new Handler();
    Runnable mRunnable;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mIntelStoryTextView = (TextView) findViewById(R.id.intel_story_textview);

        mAnimMove = AnimationUtils.loadAnimation(getApplicationContext(),
                R.anim.move);

        mRunnable = new Runnable() {
            @Override
            public void run() {

                if (mPosition >= mMy_string.length) {
                    mPosition = 0;
                }

                mIntelStoryTextView.setText(mMy_string[mPosition]);
                mPosition++;
                mIntelStoryTextView.startAnimation(mAnimMove);

                mHandler.postDelayed(mRunnable, mInterval);
            }
        };

        mRunnable.run();
    }

答案 1 :(得分:0)

我建议不要使用“移动”作为动画名称。这可能会覆盖 android 默认的移动动画并破坏默认的底部表单对话框动画。