setAnimation vs startAnimation in android

2019-01-27 19:45发布

问题:

I basically want to move a view from 1 location to another, plus I also want to increase its height gradually, So what should I use setAnimation or startAnimation.

TranslateAnimation ta = new TranslateAnimation(0, 0, Animation.RELATIVE_TO_SELF, -otherview.getHeight());
ta.setDuration(1000);
ta.setFillAfter(true);

myview.startAnimation(ta); //or, which one to use and what is the difference. 

myview.setAnimation(ta);

question: how to move this relative layout?

I tried myview.scrollTo(x,y) but no use. Is it possible to gradually increase the view height gradually?

回答1:

Use startAnimation.

Below is sample Snippet

trans = new TranslateAnimation(0, 100, 0, 100);
trans.setDuration(250);
trans.setInterpolator(new AccelerateInterpolator(1.0f));
someView.startAnimation(trans);

plus i also want to increase its height gradually,

For this you will Scale animation.

If you want to combine them into single file use Set.

<?xml version="1.0" encoding="utf-8"?> 
<set xmlns:android="schemas.android.com/apk/res/android"
android:interpolator="@android:anim/linear_interpolator">
   <scale android:fromXScale="0.0" android:fromYScale="0.0"
          android:toXScale="1.0" android:toYScale="1.0" 
          android:duration="700" android:fillBefore="false" />
   <translate android:fromXDelta="-200" android:fromYDelta="-200"
          android:duration="700" />
</set>

Place the below code inside the java file:

Animation logoMoveAnimation = AnimationUtils.loadAnimation(this, R.anim.logoanimation); 
logoIV.startAnimation(logoMoveAnimation);

setAnimation

Sets the next animation to play for this view.But view animation does not start yet.

startAnimation

If you want the animation to play immediately, use startAnimation. This method provides allows fine-grained control over the start time and invalidation, but you must make sure that

1) the animation has a start time set,

2) the view will be invalidated when the animation is supposed to start.



回答2:

This is my understanding.

SetAnimation

when the view is added to the viewGroup,animation will be called.when the view has been added,the animation will not be called

StartAnimation

animation will be called all the time even though the view has been added.