{"id":26652,"date":"2016-08-03T10:57:00","date_gmt":"2016-08-03T17:57:00","guid":{"rendered":"https:\/\/blog.xamarin.com\/?p=26652"},"modified":"2019-04-04T09:03:57","modified_gmt":"2019-04-04T16:03:57","slug":"creating-animations-with-xamarin-forms","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/xamarin\/creating-animations-with-xamarin-forms\/","title":{"rendered":"Creating Animations with Xamarin.Forms"},"content":{"rendered":"<p>\t\t\t\tAnimations are a great way to add polish to your user interface and make your app stand out. Xamarin.Forms includes its own animation infrastructure that allows for easy creation of simple animations, while also being versatile enough to create complex animations. The Xamarin.Forms animation classes target different properties of visual elements, with a typical animation progressively changing a property from one value to another over a period of time. Note that there is no XAML interface for the Xamarin.Forms animation classes. However, animations can be encapsulated in behaviors and then referenced from XAML.<\/p>\n<p>In this blog post, I\u2019m going to explore creating and cancelling animations using the <code>ViewExtensions<\/code> class in Xamarin.Forms to create compelling animations that run on iOS, Android, and Windows.<\/p>\n<h2>Introduction to Animations<\/h2>\n<p>The <code>ViewExtensions<\/code> class provides a number of extension methods that can be used to create simple animations that rotate, scale, translate, and fade <code>VisualElement<\/code> instances. By default, each animation takes 250 milliseconds. However, a duration for each animation can be specified when creating the animation.<\/p>\n<p>The extension methods provided by the <code>ViewExtensions<\/code> class are <code>TranslateTo<\/code>, <code>ScaleTo<\/code>, <code>RelScaleTo<\/code>, <code>RotateTo<\/code>, <code>RelRotateTo<\/code>, <code>RotateXTo<\/code>, <code>RotateYTo<\/code>, and <code>FadeTo<\/code>. Each method is asynchronous and returns a <code>Task&lt;bool&gt;<\/code> object. The return value is <code>false<\/code> if the animation completes, and <code>true<\/code> if the animation is cancelled. The animation methods should typically be used with the <code>await<\/code> operator, which makes it possible to easily determine when an animation has completed. However, if there&#8217;s a requirement to let an animation complete in the background, then the <code>await<\/code> operator can be omitted. In this scenario, the animation extension methods will quickly return after initiating the animation, with the animation occurring in the background.<\/p>\n<p>The <code>ViewExtensions<\/code> class also includes a <code>CancelAnimations<\/code> method that can be used to cancel any animations.<\/p>\n<h2>Performing Simple Animations<\/h2>\n<p>Each extension method in the <code>ViewExtensions<\/code> implements a single animation operation that progressively changes a property from one value to another value over a period of time.<\/p>\n<h3>Rotation<\/h3>\n<p>The following code example from the <a href=\"https:\/\/developer.xamarin.com\/samples\/xamarin-forms\/UserInterface\/Animation\/Basic\/\" target=\"_blank\">sample application<\/a> demonstrates using the <code>RotateTo<\/code> method to animate the <code>Rotation<\/code> property of an <code>Image<\/code>:<\/p>\n<pre class=\"lang:c# decode:true \" title=\"Rotation\">await image.RotateTo (360, 2000);<\/pre>\n<p>This code animates the <code>Image<\/code> instance by rotating up to 360 degrees over 2 seconds (2000 milliseconds). The <code>RotateTo<\/code> method obtains the current <code>Rotation<\/code> property value for the start of the animation, and then rotates from that value to its first argument (360).<\/p>\n<p>In addition, Xamarin.Forms supports relative rotation and rotation with anchors. For more information, see <a href=\"https:\/\/developer.xamarin.com\/guides\/xamarin-forms\/user-interface\/animation\/simple\/#Relative_Rotation\" target=\"_blank\">Relative Rotation<\/a> and <a href=\"https:\/\/developer.xamarin.com\/guides\/xamarin-forms\/user-interface\/animation\/simple\/#Scaling_and_Rotation_with_Anchors\" target=\"_blank\">Scaling and Rotation with Anchors<\/a>.<\/p>\n<h3>Scaling<\/h3>\n<p>The following code example from the <a href=\"https:\/\/developer.xamarin.com\/samples\/xamarin-forms\/UserInterface\/Animation\/Basic\/\" target=\"_blank\">sample application<\/a> demonstrates using the <code>ScaleTo<\/code> method to animate the <code>Scale<\/code> property of an <code>Image<\/code>:<\/p>\n<pre class=\"lang:c# decode:true \" title=\"Scaling\">await image.ScaleTo (2, 2000);<\/pre>\n<p>This code animates the <code>Image<\/code> instance by scaling up to twice its size over 2 seconds (2000 milliseconds). The <code>ScaleTo<\/code> method obtains the current <code>Scale<\/code> property value (default value of 1) for the start of the animation, and then scales from that value to its first argument (2). This has the effect of expanding the size of the image to twice its size.<\/p>\n<p>In addition, Xamarin.Forms supports relative scaling and scaling with anchors. For more information, see <a href=\"https:\/\/developer.xamarin.com\/guides\/xamarin-forms\/user-interface\/animation\/simple\/#Relative_Scaling\" target=\"_blank\">Relative Scaling<\/a> and <a href=\"https:\/\/developer.xamarin.com\/guides\/xamarin-forms\/user-interface\/animation\/simple\/#Scaling_and_Rotation_with_Anchors\" target=\"_blank\">Scaling and Rotation with Anchors<\/a>.<\/p>\n<h3>Translation<\/h3>\n<p>The following code example from the <a href=\"https:\/\/developer.xamarin.com\/samples\/xamarin-forms\/UserInterface\/Animation\/Basic\/\" target=\"_blank\">sample application<\/a> demonstrates using the <code>TranslateTo<\/code> method to animate the <code>TranslationX<\/code> and <code>TranslationY<\/code> properties of an <code>Image<\/code>:<\/p>\n<pre class=\"lang:c# decode:true\" title=\"Translation\">await image.TranslateTo (-100, -100, 1000);<\/pre>\n<p>This code animates the <code>Image<\/code> instance by translating it horizontally and vertically over 1 second (1000 milliseconds). The <code>TranslateTo<\/code> method simultaneously translates the image 100 pixels to the left, and 100 pixels upwards. This is because the first and second arguments are both negative numbers. Providing positive numbers would translate the image to the right, and down.<\/p>\n<h3>Fading<\/h3>\n<p>The following code example from the <a href=\"https:\/\/developer.xamarin.com\/samples\/xamarin-forms\/UserInterface\/Animation\/Basic\/\" target=\"_blank\">sample application<\/a> demonstrates using the <code>FadeTo<\/code> method to animate the <code>Opacity<\/code> property of an <code>Image<\/code>:<\/p>\n<pre class=\"lang:c# decode:true\" title=\"Fading\">await image.FadeTo (1, 4000);<\/pre>\n<p>This code animates the <code>Image<\/code> instance by fading it in over 4 seconds (4000 milliseconds). The <code>FadeTo<\/code> method obtains the current <code>Opacity<\/code> property value for the start of the animation, and then fades in from that value to its first argument (1).<\/p>\n<h2>Cancelling Animations<\/h2>\n<p>An application can cancel one or more animations with a call to the static <code>ViewExtensions.CancelAnimations<\/code> method, as demonstrated in the following code example from the <a href=\"https:\/\/developer.xamarin.com\/samples\/xamarin-forms\/UserInterface\/Animation\/Basic\/\" target=\"_blank\">sample application<\/a>:<\/p>\n<pre class=\"lang:c# decode:true \" title=\"Cancellation\">ViewExtensions.CancelAnimations (image);<\/pre>\n<p>This will immediately cancel all animations that are currently running on the <code>Image<\/code> instance.<\/p>\n<h2>Compound Animations<\/h2>\n<p>It\u2019s also possible to create sequential animations with subsequent animation methods executing after the previous method has completed. These can be created with the <code>await<\/code> operator, as demonstrated in the following code example from the <a href=\"https:\/\/developer.xamarin.com\/samples\/xamarin-forms\/UserInterface\/Animation\/Basic\/\" target=\"_blank\">sample application<\/a>:<\/p>\n<pre class=\"lang:c# decode:true\" title=\"Compound animation\">await image.TranslateTo (-100, 0, 1000);\u00a0\u00a0\u00a0 \/\/ Move image left\nawait image.TranslateTo (-100, -100, 1000); \/\/ Move image up\nawait image.TranslateTo (100, 100, 2000);\u00a0\u00a0 \/\/ Move image diagonally down and right\nawait image.TranslateTo (0, 100, 1000);\u00a0\u00a0\u00a0\u00a0 \/\/ Move image left\nawait image.TranslateTo (0, 0, 1000);\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/ Move image up<\/pre>\n<p>In this example, the <code>Image<\/code> is translated over 6 seconds (6000 milliseconds). The translation of the <code>Image<\/code> uses five translation animations, with the <code>await<\/code> operator indicating that each animation executes sequentially. Therefore, subsequent animation methods execute after the previous method has completed.<\/p>\n<h2>Composite Animations<\/h2>\n<p>Composite animations can be created by mixing awaited and non-awaited animations and using the <code>Task.WhenAny<\/code> and <code>Task.WhenAll<\/code> methods to run multiple asynchronous methods concurrently. Both methods return a <code>Task<\/code> object and accept a collection of methods that each return a <code>Task<\/code> object.<\/p>\n<p>The following code example, from the <a href=\"https:\/\/developer.xamarin.com\/samples\/xamarin-forms\/UserInterface\/Animation\/Basic\/\" target=\"_blank\">sample application<\/a>, demonstrates using the <code>Task.WhenAll<\/code> method to run multiple asynchronous animation methods concurrently:<\/p>\n<pre class=\"lang:c# decode:true \" title=\"Composite animation\">\/\/ 10 minute animation\nuint duration = 10 * 60 * 1000;\n\nawait Task.WhenAll (\n\u00a0 image.RotateTo (307 * 360, duration),\n\u00a0 image.RotateXTo (251 * 360, duration),\n\u00a0 image.RotateYTo (199 * 360, duration)\n);<\/pre>\n<p>In this example, the <code>Task.WhenAll<\/code> method call contains three tasks, each of which executes over 10 minutes. Each <code>Task<\/code> makes a different number of 360 degree rotations: 307 rotations for <code>RotateTo<\/code>, 251 rotations for <code>RotateXTo<\/code>, and 199 rotations for <code>RotateYTo<\/code>. These values are prime numbers, therefore ensuring that the rotations aren&#8217;t synchronized and hence won&#8217;t result in repetitive patterns.<\/p>\n<p>The following screenshots show the multiple rotations in progress on each platform:<img decoding=\"async\" class=\"aligncenter size-full wp-image-26654\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/44\/2019\/03\/multiple-rotations.png\" alt=\"multiple-rotations\" width=\"1000\" height=\"600\" \/><\/p>\n<h2>Controlling Velocity Changes<\/h2>\n<p>The animation extension methods in the <code>ViewExtensions<\/code> class allow an easing function to be specified as the final method parameter. The easing function, specified by the <code>Easing<\/code> class, controls how an animation speeds up or slows down as it runs.<\/p>\n<p>The following code example\u00a0demonstrates specifying different easing functions in a compound animation:<\/p>\n<pre class=\"lang:c# decode:true \" title=\"Compound animation with easing\">await image.TranslateTo(0, 200, 2000, Easing.BounceIn);\nawait image.ScaleTo(2, 2000, Easing.CubicIn);\nawait image.RotateTo(360, 2000, Easing.SinInOut);\nawait image.ScaleTo(1, 2000, Easing.CubicOut);\nawait image.TranslateTo(0, -200, 2000, Easing.BounceOut);<\/pre>\n<p>By specifying an easing function for an animation, the animation velocity becomes non-linear and produces the effect provided by the easing function. For example, the <code>BounceIn<\/code> easing function bounces the animation at the beginning, the<code> CubicIn<\/code> easing function slowly accelerates the animation, and the <code>SinInOut<\/code> easing function smoothly accelerates the animation at the beginning and smoothly decelerates the animation at the end.<\/p>\n<p>Omitting an easing function when creating an animation causes the animation to use the default <code>Linear<\/code> easing function, which produces a linear velocity.<\/p>\n<p>In addition, custom easing functions can be created. For more information, see <a href=\"https:\/\/developer.xamarin.com\/guides\/xamarin-forms\/user-interface\/animation\/easing\/#Custom_Easing_Functions\" target=\"_blank\">Custom Easing Functions<\/a>.<\/p>\n<h2>Wrapping Up<\/h2>\n<p>The <code>ViewExtensions<\/code> class provides extension methods that can be used to create simple animations that rotate, scale, translate, and fade <code>VisualElement<\/code> instances. In addition, the <code>ViewExtensions<\/code> class also includes a <code>CancelAnimations<\/code> method that can be used to cancel any animations.<\/p>\n<p>The animation extension methods in the <code>ViewExtensions<\/code> class also allow an easing function to be specified as the final method parameter. The easing function, specified by the <code>Easing<\/code> class, controls how an animation speeds up or slows down as it runs.<\/p>\n<p>For more information, see <a href=\"https:\/\/developer.xamarin.com\/guides\/xamarin-forms\/user-interface\/animation\/\" target=\"_blank\">Animation<\/a>.\t\t<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Animations are a great way to add polish to your user interface and make your app stand out. Xamarin.Forms includes its own animation infrastructure that allows for easy creation of simple animations, while also being versatile enough to create complex animations. The Xamarin.Forms animation classes target different properties of visual elements, with a typical animation [&hellip;]<\/p>\n","protected":false},"author":543,"featured_media":26654,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[2],"tags":[4,16],"class_list":["post-26652","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-developers","tag-xamarin-platform","tag-xamarin-forms"],"acf":[],"blog_post_summary":"<p>Animations are a great way to add polish to your user interface and make your app stand out. Xamarin.Forms includes its own animation infrastructure that allows for easy creation of simple animations, while also being versatile enough to create complex animations. The Xamarin.Forms animation classes target different properties of visual elements, with a typical animation [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/posts\/26652","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/users\/543"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/comments?post=26652"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/posts\/26652\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/media?parent=26652"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/categories?post=26652"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/tags?post=26652"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}