user1282637
user1282637

Reputation: 1867

Animations are (occasionally) laggy

I have 4 TextViews. They all start off invisible and I am animating them onto the screen (sliding in from the bottom). I start a thread to animate each one in, starting 200 milliseconds after each other. The problem is: the FIRST time I animate the TextViews after starting the activity, they lag. But if I animate them again afterwards, they do not. And occasionally they will lag randomly, but this isn't very frequent. I'm just wondering if there is a way to prevent this. I have only tested on one phone so it might be that, but it's weird it only happens on the first animation after starting the activity.

    animatingGroup = 0;
    final Handler handler = new Handler();
    Runnable runnable = new Runnable() {
        public void run() {
            while (animate) {

                try {
                    Thread.sleep(200);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                handler.post(new Runnable() {
                    public void run() {
                        Animation slideInAnim = AnimationUtils
                                .loadAnimation(getApplicationContext(),
                                        R.anim.slide_in_from_bottom);
                        switch (animatingGroup) {
                        case 1:
                            tv1.startAnimation(slideInAnim);
                            tv1.setVisibility(View.VISIBLE);
                            break;
                        case 2:
                            tv2.startAnimation(slideInAnim);
                            tv2.setVisibility(View.VISIBLE);
                            break;
                        case 3:
                            tv3.startAnimation(slideInAnim);
                            tv3.setVisibility(View.VISIBLE);
                            break;
                        case 4:
                            tv4.startAnimation(slideInAnim);
                            tv4.setVisibility(View.VISIBLE);
                            animate = false;
                            break;
                        }
                        animatingGroup++;
                    }
                });
            }

        }
    };
    new Thread(runnable).start();

and here is the animation:

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

    <translate
       android:duration="800"
       android:toYDelta="0%"
       android:fromYDelta="100%p" />

</set>  

Upvotes: 0

Views: 231

Answers (1)

A Honey Bustard
A Honey Bustard

Reputation: 3493

Dont create your slideInAnim Insider the handler,it will eat up resources like hell, try creating it once ... in onCreate() perhaps

EDIT : This is in my opinion a clean and simple way to do what you want to achieve :

Put your Textviews in an Array :

TextView[] textViews = new TextView[]{tv1,tv2,tv3,tv4};

Make a for loop that handles the animations startOffset :

int startOffset = 200;

for(int i=0; i < textViews.length(); i++){
    animSlideIn.setStartOffset(startOffset);
    textViews[i].setVisibility(View.Visible);
    textViews[i].startAnimation(animSlideIn);
    startOffset = startOffset + 200;
}

That way you can cut the new Thread and Handler Runnable stuff, and save a lot of code.

Upvotes: 1

Related Questions