Machado
Machado

Reputation: 14499

Set Animation for a group of objects in Android

I have several TextView objects that I need to animate with the same Animation.

The way I'm doing it looks wrong, since I can only set Animation to each object at time

@Override
    public void onClick(View v) {

    Animation hideLeftBar, showLeftBar;

    hideLeftBar = new TranslateAnimation(195, 0, 0, 0);
    hideLeftBar.setDuration(1000);
    hideLeftBar.setFillAfter(true);

    showLeftBar = new TranslateAnimation(0, 195, 0, 0);
    showLeftBar.setDuration(1000);
    showLeftBar.setFillAfter(true);

    switch(v.getId())
    {
    case R.id.btGMG:

        if(img_GMG.getAlpha() == (float) 0.3)
        {
            img_GMG.setAlpha((float) 1);
            imgLeftBar.startAnimation(showLeftBar);
            txtRS.startAnimation(showLeftBar);
            txtST.startAnimation(showLeftBar);
            txtTR.startAnimation(showLeftBar);
            txtI1.startAnimation(showLeftBar);
            txtI2.startAnimation(showLeftBar);
            txtI3.startAnimation(showLeftBar);
            txtP1.startAnimation(showLeftBar);
            txtP2.startAnimation(showLeftBar);
            txtP3.startAnimation(showLeftBar);
            txtS1.startAnimation(showLeftBar);
            txtS2.startAnimation(showLeftBar);
            txtS3.startAnimation(showLeftBar);
            txtFP1.startAnimation(showLeftBar);
            txtFP2.startAnimation(showLeftBar);
            txtFP3.startAnimation(showLeftBar);
            textIndutive.startAnimation(showLeftBar);
            textCapacitive.startAnimation(showLeftBar);


        }
        else
        {
            img_GMG.setAlpha((float) 0.3);
            imgLeftBar.startAnimation(hideLeftBar);
            txtRS.startAnimation(hideLeftBar);
            txtST.startAnimation(hideLeftBar);
            txtTR.startAnimation(hideLeftBar);
            txtI1.startAnimation(hideLeftBar);
            txtI2.startAnimation(hideLeftBar);
            txtI3.startAnimation(hideLeftBar);
            txtP1.startAnimation(hideLeftBar);
            txtP2.startAnimation(hideLeftBar);
            txtP3.startAnimation(hideLeftBar);
            txtS1.startAnimation(hideLeftBar);
            txtS2.startAnimation(hideLeftBar);
            txtS3.startAnimation(hideLeftBar);
            txtFP1.startAnimation(hideLeftBar);
            txtFP2.startAnimation(hideLeftBar);
            txtFP3.startAnimation(hideLeftBar);
            textIndutive.startAnimation(hideLeftBar);
            textCapacitive.startAnimation(hideLeftBar);
        }


        break;
    }

}

How can I reduce this? Should I do it programatically or using XML resources?

Upvotes: 2

Views: 4445

Answers (3)

C0D3LIC1OU5
C0D3LIC1OU5

Reputation: 8680

You should use an AnimatorSet. It has a playTogether() function that will play all your animations at the same time. See a good example here.

Upvotes: 3

Ali Motameni
Ali Motameni

Reputation: 2787

It should implements programmatically. You can do this with ListView if all of your elements show same as each other. With ListView you can do more with your elements so easily. List View | Android Developers

But if you want change this code rapidly you can use List of TextViews and use it easily:

List<TextView> textViews = null;

public void onCreate(View v) {

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

    textViews.add(findViewById(R.id.txtRS));
    textViews.add(findViewById(R.id.txtST));
    //Initialize other textViews here.
}

public void onClick(View v) {

    for (int i = 0; i < textViews.size(); i++) {
        textViews.get(i).startAnimation(showLeftBar);
    }
}

Upvotes: 1

Niccol&#242; Passolunghi
Niccol&#242; Passolunghi

Reputation: 6024

In order to run multiple animations in parallel you could use the View's ViewPropertyAnimator like this:

for (int i = 0; i < parentView.getChildCount(); i++) { View childView = parentView.getChildAt(i); childView.animate().translationX(DELTA_X).setDuration(DURATION_ANIM); }

and for the other animation you could use .translationY()

Upvotes: 1

Related Questions