简体   繁体   English

为什么不直接在animatorset上调用cancel方法,而该animator到底是什么呢?

[英]Why not called the cancel method on animatorset directly and what exactly is the animator?

I have three questions that I really need to clarify one them is in the code provided by the android developer at this link: http://developer.android.com/training/animation/zoom.html#animate (I didn't want to overwhelm you with the code so I didn't copy and paste the code here and the code is under the section called "Zoom the View") 我确实需要澄清三个问题,这是在此链接的android开发人员提供的代码中: http : //developer.android.com/training/animation/zoom.html#animate (我不想使您的代码不知所措,因此我没有在此处复制和粘贴代码,并且代码位于“缩放视图”部分下)

1) So my question is why does the programmer do the following? 1)所以我的问题是程序员为什么要执行以下操作? mCurrentAnimator = set and then write mCurrentAnimator = set然后写

if (mCurrentAnimator != null) {
            mCurrentAnimator.cancel();
        }

Let me quickly mention that mCurrentAnimator is an Animator object and set is an AnimatorSet object. 让我快速提及一下,mCurrentAnimator是Animator对象,set是AnimatorSet对象。 My confusion lies in the fact that why does the developer feel the need to make the an Animator object = to a AnimatorSet object before calling the cancel() because from my perspective, since AnimatorSet extends Animator then shouldn't I be able to call the cancel method directly. 我的困惑在于,为什么开发人员感到需要在调用cancel()之前使Animator对象=成为AnimatorSet对象,因为从我的角度来看,因为AnimatorSet extends Animator ,所以我不应该调用直接取消方法。 Why feel the need to make a superclass = to the subclass to call a method when the subclass already can call that method? 为什么当子类已经可以调用该方法时,为什么需要为该子类创建一个超类=呢?

2) My question is what exactly is an Animator because when I check at the documentation there are some method of this class such as public void setTarget (Object target) which made me confuse. 2)我的问题是什么是Animator因为当我查看文档时,有一些此类的方法,例如public void setTarget (Object target) ,这使我感到困惑。 What made me confuse was the fact that the description stated this method "Sets the target object whose property will be animated by this animation". 令我感到困惑的是,描述中说明了此方法“设置目标对象,该对象的属性将通过此动画进行动画处理”。 Precisely how can an animator tells what property to animate just simply by providing a target? 精确地讲,动画制作者如何仅通过提供目标就能告诉要进行动画处理的属性?

3) Also, one of the animator method was public void setupStartValues () and the description stated "This method tells the object to use appropriate information to extract starting values for the animation. For example, a AnimatorSet object will pass this call to its child objects to tell them to set up the values." 3)此外,动画师方法之一是public void setupStartValues () ,描述内容为“此方法告诉对象使用适当的信息来提取动画的起始值。例如,AnimatorSet对象会将此调用传递给它的子对象对象告诉他们设置值。” However, what I don't understand is how can a method be pass into a child object and how does the method tells the object to use what information to use so that it is appropriate?? 但是,我不了解的是如何将方法传递给子对象,以及该方法如何告诉对象使用什么信息以便合适?

For your first question, the main function of that example is to zoom the image thumbnail when the user clicks on it. 对于第一个问题,该示例的主要功能是在用户单击图像缩略图时对其进行缩放。 You'll notice that the mCurrentAnimator.cancel() method is called at the very beginning of the onClick method, and mCurrentAnimator = set is at the end. 您会注意到,在onClick方法的开始处调用了mCurrentAnimator.cancel()方法,而在结尾处调用了mCurrentAnimator = set。 What this is doing is, once an animation is created and started, the program is saving a reference to it. 这是在创建并启动动画后,程序会保存对其的引用。 This is useful because what if, say, the user clicks the thumbnail, the image starts to animate, and while the animation is running, the user clicks the image again? 这很有用,因为如果用户单击缩略图,图像开始制作动画,并且在动画运行时,用户再次单击图像,该怎么办? the mCurrentAnimator will hold the reference to the animation currently in progress, and the mCurrentAnimator.cancel() stops that animation so that the onClick method can create another animator in response to the most recent click. mCurrentAnimator将保留对当前正在进行的动画的引用,并且mCurrentAnimator.cancel()停止该动画,以便onClick方法可以响应于最近的单击而创建另一个动画器。

For the second question, simply put, an Animator is used to animate a given view by changing its properties. 对于第二个问题,简单地说,一个Animator用于通过更改给定视图的属性来为其动画。 To address the confusion around the setTarget method, that method is not a constructor. 为了解决setTarget方法周围的困惑,该方法不是构造函数。 If you want to create a new animator, you can use the ofFloat, ofArgb, or similar static methods from the ObjectAnimator class. 如果要创建新的动画师,则可以使用ObjectAnimator类中的ofFloat,ofArgb或类似的静态方法。 You can read about them here . 您可以在这里阅读有关它们的信息 Using those methods, you tell the animator what properties it needs to change. 使用这些方法,您可以告诉动画师需要更改哪些属性。 The setTarget method is only to change what object you are animating in an existing animator , which can be useful if you would like to apply the same animator to multiple views. setTarget方法仅用于更改要在现有动画器中进行动画处理的对象,如果要将同一动画器应用于多个视图,则该方法很有用。

Finally, an AnimatorSet is essentially a container to hold different animations you want to play either together or in sequence. 最后,AnimatorSet本质上是一个容器,用于容纳您想要一起播放或顺序播放的不同动画。 Very simply, "passing the call" means that, when you call the setupStartValues() on the AnimatorSet, it calls the setupStartValues() method on each of the individual Animators belonging to that set. 非常简单,“通过调用”意味着,当您在AnimatorSet上调用setupStartValues()时,它将在属于该集合的每个单独Animator上调用setupStartValues()方法。

I hope this helps! 我希望这有帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM