添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接

I am creating a Play/Pause button for a ValueAnimator animation. In the handler for the button, I need to determine the current state of the ValueAnimator to determine what method of the ValueAnimator to call. This is my first time using animation in an app. I am trying to determine what properties ( IsStarted & IsRunning ) to use to determine this, as well as exactly when & what causes them to change (the names sound very similar, so I am trying to understand the difference). I am also unsure when to use which methods to start & stop the ValueAnimator ( Resume vs Start , Cancel vs End vs Pause , etc.). I want the user to be able to start & stop at any time (unless the ValueAnimator has finished, another reason I need to know exactly when IsStarted & IsRunning change). I also need a Reset button (is this the purpose of the Cancel method?). Any help would be appreciated.

Your project is a Xamarin.Android project and you would like to control the state of animation, right? You could check the Android Doc - ValueAnimator |  Android Developers , IsStarted state is a superset of isRunning for reusable Animators. IsRunning means it having been started and gone past any initial startDelay period and not yet ended. isRunning() will return true only after the delay phase is complete. I'm not clear about your ValueAnimator , would you mind sharing more code snippets?

I haven't really written much code other than calling the Start() method, because I don't know which methods to use under what conditions. My app is a simple timer with a play/pause & reset button (I suppose you could call it a "video" of a clock). I have not set a value for StartDelay , so does this mean that in my scenario IsStarted & IsRunning will always be the same? I am assuming that the Cancel() method stops the animation & sets IsStarted & IsRunning & IsPaused all to false. I was also confused about what happens to CurrentPlayTime when Pause() is called. The documentation for CurrentPlayTime says:

which is equal to the current time minus the time that the animation started

For example, if I start the animation, call Pause() after 1 minute, and then wait 10 minutes, will CurrentPlayTime be 0:01:00 or 0:11:00?

Hello,

The difference between IsStarted and IsRunning is only that IsStarted is true immediately after calling the Start() method, while IsRunning is only true after the animation actually starts. And after IsRunning becomes True, CurrentPlayTime will start timing.

For example, if I start the animation, call Pause() after 1 minute, and then wait 10 minutes, will CurrentPlayTime be 0:01:00 or 0:11:00?

The Pause() method does not stop the CurrentPlayTime timer, therefore, the value of it will be 0:11:00. You could verify it by referring to the following code:

   var text = FindViewById<TextView>(Resource.Id.textView1);  
   var animator = ObjectAnimator.OfFloat(text, "translationY", 0, 900);  
   animator.SetDuration(5000);  
   animator.StartDelay = 2000;  
   Button Startbutton = FindViewById<Button>(Resource.Id.button1);  
   Button Pausebutton = FindViewById<Button>(Resource.Id.button2);  
   Button Resumebutton = FindViewById<Button>(Resource.Id.button3);  
   Startbutton.Click += (s, e) =>  
       animator.Start();  
   Pausebutton.Click += (s, e) =>  
       animator.Pause();  
       Console.WriteLine(animator.CurrentPlayTime);  
   Resumebutton.Click += (s, e) =>  
       animator.Resume();  
       Console.WriteLine(animator.CurrentPlayTime);  

Best Regards,

Alec Liu.

If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.