qiubit
qiubit

Reputation: 4816

How to make a View blink after translation using Animation objects?

I want to make 2 animations on one View, with the second one starting after the first one. The first one is translating and I use TranslateAnimation for that purpose and the second one is AlphaAnimation as I want the View to start blinking after translation. The problem is no matter what I'm doing these animations start simultaneously. As far as I'm concerned I should use AnimationSet object for this purpose but how to do this exactly? I even tried to use setStartTime() method to use some crazy value for the AlphaAnimation before putting it into AnimationSet but still the animations start immediately and simultaneously. So what should I do to prevent it from happening?

PS Here is what I tried to do and what didn't work:

    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);

Upvotes: 0

Views: 336

Answers (2)

SaravanaRaja
SaravanaRaja

Reputation: 3406

Use animation listener. hope this works

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();

Upvotes: 0

Rod_Algonquin
Rod_Algonquin

Reputation: 26198

you can use this

   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);
        }
    });

Make sure that cursor and tAnim are instance/global variable in your class, and also start the animation in the post method of the TextView to run it in synchronize with the textView

Upvotes: 1

Related Questions