如何在使用动画对象进行平移后使视图闪烁?

时间:2014-08-13 21:27:54

标签: android animation android-view

我想在一个View上制作2个动画,第二个在第一个动画之后开始。第一个是翻译,我使用TranslateAnimation用于此目的,第二个是AlphaAnimation,因为我希望View在翻译后开始闪烁。问题是无论我在做什么,这些动画同时开始。就我而言,我应该为此目的使用AnimationSet对象但是如何准确地执行此操作?我甚至尝试使用setStartTime()方法在将AlphaAnimation放入AnimationSet之前使用一些疯狂的值,但动画仍然会立即开始同时启动。那么我应该怎么做以防止它发生?

PS这是我尝试做的和不起作用的事情:

    TextView cursor = (TextView) findViewById(R.id.cursor);
    Animation tAnim = new TranslateAnimation(-1000.f,200.0f,0.0f,0.0f);
    tAnim.setStartTime(0);
    tAnim.setDuration(3000);
    tAnim.setStartOffset(0);
    Animation bAnim = new AlphaAnimation(1.0f, 0.0f);
    bAnim.setStartTime(tAnim.getDuration());
    bAnim.setDuration(300);
    bAnim.setStartOffset(30);
    bAnim.setRepeatCount(Animation.INFINITE);
    bAnim.setRepeatMode(Animation.REVERSE);
    AnimationSet s = new AnimationSet(false);
    s.addAnimation(tAnim);
    s.addAnimation(bAnim);
    cursor.startAnimation(s);

2 个答案:

答案 0 :(得分:1)

你可以用这个

   TextView cursor = (TextView) findViewById(R.id.cursor);
    Animation tAnim = new TranslateAnimation(-1000.f,200.0f,0.0f,0.0f);
    tAnim.setStartTime(0);
    tAnim.setDuration(3000);
    tAnim.setStartOffset(0);

    Animation bAnim = new AlphaAnimation(1.0f, 0.0f);
    bAnim.setStartTime(tAnim.getDuration());
    bAnim.setDuration(300);
    bAnim.setStartOffset(30);
    bAnim.setRepeatCount(Animation.INFINITE);
    bAnim.setRepeatMode(Animation.REVERSE);

    tAnim.setAnimationListener(new AnimationListener() {

        @Override
        public void onAnimationStart(Animation arg0) { }

        @Override
        public void onAnimationRepeat(Animation arg0) { }

        @Override
        public void onAnimationEnd(Animation arg0) {
            cursor.startAnimation(bAnim);
        }
    });

    cursor.post(new Runnable() {

        @Override
        public void run() {
            cursor.startAnimation(tAnim);
        }
    });

确保您的班级中的光标 tAnim instance/global变量,并在post method TextView中启动动画1}}使用textView

同步中运行它

答案 1 :(得分:0)

使用动画侦听器。希望这项工作

Animation tAnim = new TranslateAnimation(-1000.f,200.0f,0.0f,0.0f);
tAnim.setStartTime(0);
tAnim.setDuration(3000);
tAnim.setStartOffset(0);
Animation bAnim = new AlphaAnimation(1.0f, 0.0f);
bAnim.setStartTime(tAnim.getDuration());
bAnim.setDuration(300);
bAnim.setStartOffset(30);
bAnim.setRepeatCount(Animation.INFINITE);
bAnim.setRepeatMode(Animation.REVERSE);
AnimationListener AnimationListener = new AnimationListener(){

        @Override
        public void onAnimationStart(Animation animation) {
            // TODO Animation start and end

        }
        @Override
        public void onAnimationEnd(Animation animation) {

            //start the second animation here
            yourview.startAnimation(bAnim);
        }

        @Override
        public void onAnimationRepeat(Animation animation) {
            // TODO Auto-generated method stub

        }};
        tAnim.setAnimationListener(AnimationListener);
        yourview.startAnimation(tAnim);
        tAnim.hasEnded();