Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Apply one animation to multiple views at the same time

Tags:

So Id like to rotate a handful of views all at the same time, all using the same rotation specs. The issue is that for some reason the rotation acts differently for the second element. Apparently this has to do with the animation object actually changing state in between those two lines of code. Obviously I could just create a seperate Animation object and apply it, but I feel like there is an easier way (I have about 15 views)

Rotates only the first view correctly:

Animation rotateAnim = AnimationUtils.loadAnimation(this, R.anim.rotationtoportrait); target.startAnimation(rotateAnim); lightBtn.startAnimation(rotateAnim); 

Rotates both correctly

Animation rotateAnim = AnimationUtils.loadAnimation(this, R.anim.rotationtoportrait); Animation rotateAnim2 = AnimationUtils.loadAnimation(this, R.anim.rotationtoportrait); target.startAnimation(rotateAnim); lightBtn.startAnimation(rotateAnim2); 

XML:

<?xml version="1.0" encoding="utf-8"?> <rotate     xmlns:android="http://schemas.android.com/apk/res/android"     android:fromDegrees="-90"     android:toDegrees="0"     android:pivotX="50%"     android:pivotY="50%"     android:duration="500" android:fillAfter="true"> 

Anyone have any ideas?

like image 813
Jameo Avatar asked Feb 12 '13 20:02

Jameo


People also ask

How do you do simultaneous animations in PowerPoint?

Select the object on the slide that you want to animate. On the Animations tab, click Animation Pane. Click Add Animation, and pick an animation effect. To apply additional animation effects to the same object, select it, click Add Animation and pick another animation effect.

How do you link two animations in PowerPoint?

To make the animations run simultaneously, click on the motion-path animation within the task pane. Click the drop-down arrow appears to the right of the animation name and choose Start With Previous from the subsequent menu. Click Play again (bottom of task pane) and the two animations will occur together.


2 Answers

Do it like this:

ObjectAnimator anim = ObjectAnimator.ofFloat(view, "y", 100f); arrayListObjectAnimators.add(anim);  ObjectAnimator anim1 = ObjectAnimator.ofFloat(view, "x", 0f); arrayListObjectAnimators.add(anim1);  ObjectAnimator[] objectAnimators = arrayListObjectAnimators.toArray(new ObjectAnimator[arrayListObjectAnimators.size()]); AnimatorSet animSetXY = new AnimatorSet(); animSetXY.playTogether(objectAnimators); animSetXY.duration(1000); animSetXY.start(); 
like image 180
Leandros Avatar answered Nov 09 '22 02:11

Leandros


So I guess this just isn't possible, so I created a helper method to just apply the same animation to a list of views:

public void doRotations(ArrayList<View> views, int start, int end, int xprop, float xscale, int yprop, float yscale, int duration, Boolean fillAfter){      for(int i = 0; i < views.size(); i++){         RotateAnimation temp = new RotateAnimation(start, end, xprop, xscale, yprop, yscale);         temp.setDuration(duration);         temp.setFillAfter(fillAfter);         views.get(i).startAnimation(temp);     } } 

Definitely a hack, but I guess thats all I'm able to do right now

like image 45
Jameo Avatar answered Nov 09 '22 03:11

Jameo