android 中的 setAnimation 与 startAnimation

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10909865/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 05:25:24  来源:igfitidea点击:

setAnimation vs startAnimation in android

androidanimationlayout

提问by Programmer

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.

我基本上想将视图从一个位置移动到另一个位置,另外我还想逐渐增加它的高度,那么我应该使用 setAnimation 还是 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?

我试过myview.scrollTo(x,y)但没有用。是否可以逐渐增加视图高度?

回答by Vipul Shah

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.

如果要将它们组合成单个文件,请使用 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:

将下面的代码放在java文件中:

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

如果您希望动画立即播放,请使用 startAnimation。此方法允许对开始时间和失效进行细粒度控制,但您必须确保

1) the animation has a start time set,

1)动画有一个开始时间设置,

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

2) 当动画应该开始时,视图将失效。

回答by user2024270

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.

即使添加了视图,动画也会一直被调用。