Site icon C1CTech

Android ConstraintLayout Animation:Part I

<p>In this tutorial&comma; we will talk about how to implement ConstraintLayout Animations in our Android Application using a simple example&period;<&sol;p>&NewLine;<h3><span style&equals;"color&colon; &num;000080&semi;"><strong>ConstraintLayout Animations<&sol;strong><&sol;span><&sol;h3>&NewLine;<p>Animations make your app look polished&comma; improve engagement&comma; and are fun to build&period; With ConstraintLayout it’s easy to build complex animations involving several views at once and to do so&comma; we need to just change constraints&excl;<&sol;p>&NewLine;<p>By specifying your animations via keyframes&comma; it is easy to fluidly animate complex scenes without a lot of code&period;<&sol;p>&NewLine;<p class&equals;"p1">Within a <span class&equals;"s1">ConstraintLayout<&sol;span>&comma; you can animate changes to the size and position of elements by using <strong><span style&equals;"color&colon; &num;0000ff&semi;"><a style&equals;"color&colon; &num;0000ff&semi;" href&equals;"https&colon;&sol;&sol;developer&period;android&period;com&sol;reference&sol;androidx&sol;constraintlayout&sol;widget&sol;ConstraintSet"><span class&equals;"s2">ConstraintSet<&sol;span><&sol;a><&sol;span><&sol;strong> and <strong><span style&equals;"color&colon; &num;0000ff&semi;"><a style&equals;"color&colon; &num;0000ff&semi;" href&equals;"https&colon;&sol;&sol;developer&period;android&period;com&sol;reference&sol;android&sol;transition&sol;TransitionManager"><span class&equals;"s2">TransitionManager<&sol;span><&sol;a><&sol;span><&sol;strong>&period;<&sol;p>&NewLine;<p>The most common and easy way to build an animation using ConstraintSets&comma; specify two layout files which act as a start and end keyframe for the animation&period; You can then load a <span class&equals;"s1">ConstraintSet<&sol;span> from the second keyframe file and apply it to the displayed <span class&equals;"s1">ConstraintLayout<&sol;span>&period;<&sol;p>&NewLine;<p><b><span style&equals;"color&colon; &num;0000ff&semi;">Note<&sol;span>&colon; <&sol;b><strong><span style&equals;"color&colon; &num;008000&semi;">ConstraintLayout<&sol;span><&sol;strong> and <strong><span style&equals;"color&colon; &num;008000&semi;">ConstraintSet<&sol;span><&sol;strong> supports from API 9 onwards and  <strong><span class&equals;"s1" style&equals;"color&colon; &num;008000&semi;">TransitionManager<&sol;span><&sol;strong> is available from API level 14 onwards&period;<&sol;p>&NewLine;<h3><span style&equals;"color&colon; &num;000080&semi;"><strong>ConstraintSet<&sol;strong><&sol;span><&sol;h3>&NewLine;<p class&equals;"p1"><span class&equals;"s1"><strong><span style&equals;"color&colon; &num;008000&semi;">ConstraintSet<&sol;span><&sol;strong> is a <&sol;span>class that allows you to define programmatically a set of constraints to be used with <strong><span style&equals;"color&colon; &num;0000ff&semi;"><a style&equals;"color&colon; &num;0000ff&semi;" href&equals;"https&colon;&sol;&sol;developer&period;android&period;com&sol;reference&sol;androidx&sol;constraintlayout&sol;widget&sol;ConstraintLayout&period;html"><span class&equals;"s1">ConstraintLayout<&sol;span><&sol;a><&sol;span><&sol;strong>&period;<&sol;p>&NewLine;<p>In other words&comma; we can say <span style&equals;"color&colon; &num;008000&semi;"><strong><span class&equals;"s1">ConstraintSet<&sol;span><&sol;strong><&sol;span> is a lightweight object that represents the constraints&comma; margins&comma; and padding of all child elements within a <span class&equals;"s1">ConstraintLayout<&sol;span>&period;<&sol;p>&NewLine;<p>It lets you create and save constraints&comma; and apply them to an existing ConstraintLayout&period;<&sol;p>&NewLine;<p>When you apply a <strong><span class&equals;"s1" style&equals;"color&colon; &num;008000&semi;">ConstraintSet<&sol;span><&sol;strong> to a displayed <span class&equals;"s1">ConstraintLayout<&sol;span>&comma; the layout updates the constraints of all of its children&period;<&sol;p>&NewLine;<p>A ConstraintSet is created just like any other Java object&colon;<&sol;p>&NewLine;<pre>ConstraintSet constraint &equals; new ConstraintSet&lpar;&rpar;&semi;<&sol;pre>&NewLine;<p><b><span style&equals;"color&colon; &num;0000ff&semi;">Important<&sol;span>&colon;<&sol;b> ConstraintSet animations animate only the size and position of child elements&period; They do not animate other attributes &lpar;such as color&rpar;&period;<&sol;p>&NewLine;<h4><span style&equals;"color&colon; &num;000080&semi;"><strong>ConstraintSet Methods<&sol;strong><&sol;span><&sol;h4>&NewLine;<p><strong><span style&equals;"color&colon; &num;0000ff&semi;">connect&lpar;&rpar;<&sol;span><&sol;strong>&colon; This method is used to set constraints on the views programmatically&period; It establishes connections between sibling views or a view and the parent view&period;<&sol;p>&NewLine;<p>Both <span style&equals;"color&colon; &num;0000ff&semi;"><strong>clone&lpar;&rpar; <&sol;strong><span style&equals;"color&colon; &num;000000&semi;">and<&sol;span><strong> load&lpar;&rpar;<&sol;strong> <span style&equals;"color&colon; &num;000000&semi;">methods<&sol;span> <span style&equals;"color&colon; &num;000000&semi;">of <strong><span style&equals;"color&colon; &num;008000&semi;">ConstrainSet<&sol;span><&sol;strong> class <&sol;span><&sol;span>do the operation similarly with a slight difference&period;<&sol;p>&NewLine;<p><span style&equals;"color&colon; &num;0000ff&semi;"><b>clone&lpar;&rpar; &colon; <&sol;b><span style&equals;"color&colon; &num;000000&semi;">It <&sol;span><&sol;span>will inflate the layout and absorb all the layout related constraint mappings &lpar;<span style&equals;"color&colon; &num;008000&semi;"><strong>layout&lowbar;width&comma;layout&lowbar;height&comma;layout&lowbar;marginTop etc&period; and not styling ie&period; textSize&comma; color etc<&sol;strong><&sol;span>&rpar; of the particular views within the constraint layout&period;<&sol;p>&NewLine;<p><strong><span style&equals;"color&colon; &num;0000ff&semi;">load&lpar;&rpar;<&sol;span><&sol;strong>&colon; It will parse the XML and then absorb all the layout related constraint mappings of the views within the constraint layout&period;<&sol;p>&NewLine;<h4><span style&equals;"color&colon; &num;000080&semi;"><strong>To create a ConstraintLayout animation we need&colon;<&sol;strong><&sol;span><&sol;h4>&NewLine;<p class&equals;"p1"><span style&equals;"color&colon; &num;0000ff&semi;"><b><a style&equals;"color&colon; &num;0000ff&semi;" href&equals;"https&colon;&sol;&sol;developer&period;android&period;com&sol;reference&sol;android&sol;transition&sol;TransitionManager&period;html">TransitionManager<&sol;a><&sol;b><&sol;span> — This class manages the set of transitions that fire when there is a change of <span style&equals;"color&colon; &num;008000&semi;"><strong><a style&equals;"color&colon; &num;008000&semi;" href&equals;"https&colon;&sol;&sol;developer&period;android&period;com&sol;reference&sol;android&sol;transition&sol;Scene&period;html"><span class&equals;"s1">Scene<&sol;span><&sol;a><&sol;strong><&sol;span>&period; Setting specific transitions for scene changes is not required&semi; by default&comma; a Scene change will use <strong><span style&equals;"color&colon; &num;008000&semi;"><a style&equals;"color&colon; &num;008000&semi;" href&equals;"https&colon;&sol;&sol;developer&period;android&period;com&sol;reference&sol;android&sol;transition&sol;AutoTransition&period;html"><span class&equals;"s1">AutoTransition<&sol;span><&sol;a><&sol;span><&sol;strong> to do something reasonable for most situations&period; Specifying other transitions for particular scene changes is only necessary if the application wants different transition behavior in these situations&period;<&sol;p>&NewLine;<p class&equals;"p1"><span style&equals;"color&colon; &num;0000ff&semi;"><b><a style&equals;"color&colon; &num;0000ff&semi;" href&equals;"https&colon;&sol;&sol;developer&period;android&period;com&sol;reference&sol;android&sol;transition&sol;TransitionManager&period;html&num;beginDelayedTransition&lpar;android&period;view&period;ViewGroup&comma;&percnt;20android&period;transition&period;Transition&rpar;">beginDelayedTransition&lpar;&rpar;<&sol;a><&sol;b> <&sol;span>—  Convenience method to animate&comma; using the default transition&comma; to start the transition from the first scene to the second scene&lpar;first layout to the second layout&rpar;&period;Equivalent to calling <strong><span style&equals;"color&colon; &num;008000&semi;"><a style&equals;"color&colon; &num;008000&semi;" href&equals;"https&colon;&sol;&sol;developer&period;android&period;com&sol;reference&sol;android&sol;transition&sol;TransitionManager&period;html&num;beginDelayedTransition&lpar;android&period;view&period;ViewGroup&comma;&percnt;20android&period;transition&period;Transition&rpar;"><span class&equals;"s1">beginDelayedTransition&lpar;ViewGroup&comma; Transition&rpar;<&sol;span><&sol;a><&sol;span><&sol;strong> with a value of null for the transition parameter&period;<&sol;p>&NewLine;<p class&equals;"p1"><span style&equals;"color&colon; &num;0000ff&semi;"><b>applyTo&lpar;&rpar;<&sol;b> <&sol;span>— sets or applies the new or requested constraints to the view specified&period;<&sol;p>&NewLine;<h3><span style&equals;"color&colon; &num;000080&semi;"><strong>A Simple Example<&sol;strong><&sol;span><&sol;h3>&NewLine;<p>The goal of our example is the following animation — which we want to achieve&period;<&sol;p>&NewLine;<p><strong><span style&equals;"color&colon; &num;008000&semi;">Get GITHUB code from<&sol;span> <span style&equals;"color&colon; &num;0000ff&semi;"><a style&equals;"color&colon; &num;0000ff&semi;" href&equals;"https&colon;&sol;&sol;github&period;com&sol;arunk7839&sol;ConstraintAnimationDemo">here<&sol;a><&sol;span><&sol;strong>&period;<&sol;p>&NewLine;<p><amp-youtube layout&equals;"responsive" width&equals;"1200" height&equals;"675" data-videoid&equals;"ig3pJ9NZH1s" title&equals;"Android Constraint Layout Animation"><a placeholder href&equals;"https&colon;&sol;&sol;youtu&period;be&sol;ig3pJ9NZH1s"><img src&equals;"https&colon;&sol;&sol;i&period;ytimg&period;com&sol;vi&sol;ig3pJ9NZH1s&sol;hqdefault&period;jpg" layout&equals;"fill" object-fit&equals;"cover" alt&equals;"Android Constraint Layout Animation"><&sol;a><&sol;amp-youtube><&sol;p>&NewLine;<p>&nbsp&semi;<&sol;p>&NewLine;<p>As you can see&comma; we have our <span style&equals;"color&colon; &num;008000&semi;"><strong>ic&lowbar;launcher&lowbar;round<&sol;strong><&sol;span> logo at the top of our screen&period; On <span style&equals;"color&colon; &num;008000&semi;"><strong class&equals;"id jq">tapping<&sol;strong><&sol;span> the image&comma; it has to come to the bottom of the screen&period; Also&comma; notice that when the image reaches the bottom of the screen&comma; it’s size is increased&period;<&sol;p>&NewLine;<h4 id&equals;"31dd" class&equals;"lq iq cn ar aq do lr ls lt lu lv lw lx ly lz ma mb"><span style&equals;"color&colon; &num;000080&semi;"><strong>Creating Our Layouts<&sol;strong><&sol;span><&sol;h4>&NewLine;<p>Let’s create our first layout of the <span style&equals;"color&colon; &num;000000&semi;">MainActivity which is <strong><span style&equals;"color&colon; &num;008000&semi;">activity&lowbar;main&period;xml<&sol;span><&sol;strong><&sol;span>&period; Pay close attention to the constraints&period;<&sol;p>&NewLine;<p><span style&equals;"color&colon; &num;0000ff&semi;"><strong>activity&lowbar;main&period;xml<&sol;strong><&sol;span><&sol;p>&NewLine;<&excl;-- WP QUADS Content Ad Plugin v&period; 2&period;0&period;98&period;1 -->&NewLine;<div class&equals;"quads-location quads-ad2" id&equals;"quads-ad2" style&equals;"float&colon;none&semi;margin&colon;0px&semi;">&NewLine;&NewLine;<&sol;div>&NewLine;&NewLine;<pre>&lt&semi;&quest;xml version&equals;"1&period;0" encoding&equals;"utf-8"&quest;&gt&semi;&NewLine;&lt&semi;androidx&period;constraintlayout&period;widget&period;ConstraintLayout xmlns&colon;android&equals;"http&colon;&sol;&sol;schemas&period;android&period;com&sol;apk&sol;res&sol;android"&NewLine; xmlns&colon;app&equals;"http&colon;&sol;&sol;schemas&period;android&period;com&sol;apk&sol;res-auto"&NewLine; xmlns&colon;tools&equals;"http&colon;&sol;&sol;schemas&period;android&period;com&sol;tools"&NewLine; <strong><span style&equals;"color&colon; &num;008000&semi;"> android&colon;id&equals;"&commat;&plus;id&sol;root"<&sol;span><&sol;strong>&NewLine; android&colon;layout&lowbar;width&equals;"match&lowbar;parent"&NewLine; android&colon;layout&lowbar;height&equals;"match&lowbar;parent"&NewLine; tools&colon;context&equals;"&period;MainActivity"&gt&semi;&NewLine;&NewLine; &lt&semi;ImageView&NewLine; android&colon;id&equals;"&commat;&plus;id&sol;imageView"&NewLine; <strong><span style&equals;"color&colon; &num;008000&semi;"> android&colon;layout&lowbar;width&equals;"100dp"&NewLine; android&colon;layout&lowbar;height&equals;"100dp"<&sol;span><&sol;strong>&NewLine; android&colon;layout&lowbar;marginStart&equals;"8dp"&NewLine; android&colon;layout&lowbar;marginTop&equals;"8dp"&NewLine; android&colon;layout&lowbar;marginEnd&equals;"8dp"&NewLine; app&colon;layout&lowbar;constraintEnd&lowbar;toEndOf&equals;"parent"&NewLine; app&colon;layout&lowbar;constraintStart&lowbar;toStartOf&equals;"parent"&NewLine; app&colon;layout&lowbar;constraintTop&lowbar;toTopOf&equals;"parent"&NewLine; app&colon;srcCompat&equals;"&commat;mipmap&sol;ic&lowbar;launcher&lowbar;round" &sol;&gt&semi;&NewLine;&NewLine;&lt&semi;&sol;androidx&period;constraintlayout&period;widget&period;ConstraintLayout&gt&semi;<&sol;pre>&NewLine;<p><img class&equals;"alignnone size-full wp-image-1406" src&equals;"https&colon;&sol;&sol;c1ctech&period;com&sol;wp-content&sol;uploads&sol;2019&sol;12&sol;Screenshot-2019-12-04-18&period;37&period;24-3794724038-1575548647876&period;png" alt&equals;"Screenshot 2019-12-04 18&period;37&period;24" width&equals;"884" height&equals;"739" &sol;><&sol;p>&NewLine;<p class&equals;"p1">Let’s create our final layout or the second frame — <strong><span style&equals;"color&colon; &num;008000&semi;">activity&lowbar;main&lowbar;animation&period;xml<&sol;span><&sol;strong>&period; In this second layout&comma; to make our image bigger&comma; I’ve increased it to 250dp&period;<&sol;p>&NewLine;<p><span style&equals;"color&colon; &num;0000ff&semi;"><strong>activity&lowbar;main&lowbar;animation&period;xml<&sol;strong><&sol;span><&sol;p>&NewLine;<pre>&lt&semi;&quest;xml version&equals;"1&period;0" encoding&equals;"utf-8"&quest;&gt&semi;&NewLine;&lt&semi;androidx&period;constraintlayout&period;widget&period;ConstraintLayout xmlns&colon;android&equals;"http&colon;&sol;&sol;schemas&period;android&period;com&sol;apk&sol;res&sol;android"&NewLine; xmlns&colon;app&equals;"http&colon;&sol;&sol;schemas&period;android&period;com&sol;apk&sol;res-auto"&NewLine; xmlns&colon;tools&equals;"http&colon;&sol;&sol;schemas&period;android&period;com&sol;tools"&NewLine; android&colon;layout&lowbar;width&equals;"match&lowbar;parent"&NewLine; android&colon;layout&lowbar;height&equals;"match&lowbar;parent"&NewLine; tools&colon;context&equals;"&period;MainActivity"&gt&semi;&NewLine;&NewLine;&NewLine; &lt&semi;ImageView&NewLine; android&colon;id&equals;"&commat;&plus;id&sol;imageView"&NewLine; <span style&equals;"color&colon; &num;008000&semi;"><strong> android&colon;layout&lowbar;width&equals;"250dp"&NewLine; android&colon;layout&lowbar;height&equals;"250dp"<&sol;strong><&sol;span>&NewLine; android&colon;layout&lowbar;marginStart&equals;"8dp"&NewLine; android&colon;layout&lowbar;marginEnd&equals;"8dp"&NewLine; android&colon;layout&lowbar;marginBottom&equals;"8dp"&NewLine; app&colon;layout&lowbar;constraintBottom&lowbar;toBottomOf&equals;"parent"&NewLine; app&colon;layout&lowbar;constraintEnd&lowbar;toEndOf&equals;"parent"&NewLine; app&colon;layout&lowbar;constraintStart&lowbar;toStartOf&equals;"parent"&NewLine; app&colon;srcCompat&equals;"&commat;mipmap&sol;ic&lowbar;launcher&lowbar;round" &sol;&gt&semi;&NewLine;&lt&semi;&sol;androidx&period;constraintlayout&period;widget&period;ConstraintLayout&gt&semi;<&sol;pre>&NewLine;<p><img class&equals;"alignnone size-full wp-image-1407" src&equals;"https&colon;&sol;&sol;c1ctech&period;com&sol;wp-content&sol;uploads&sol;2019&sol;12&sol;Screenshot-2019-12-04-18&period;37&period;57-879109051-1575548754491&period;png" alt&equals;"Screenshot 2019-12-04 18&period;37&period;57" width&equals;"846" height&equals;"726" &sol;><&sol;p>&NewLine;<p>&nbsp&semi;<&sol;p>&NewLine;<h4 id&equals;"3828" class&equals;"lq iq cn ar aq do lr ls lt lu lv lw lx ly lz ma mb"><span style&equals;"color&colon; &num;000080&semi;"><strong>Creating Animation<&sol;strong><&sol;span><&sol;h4>&NewLine;<p class&equals;"p1">Now&comma; we need to make this transition smooth by defining the animation&comma; with the starting point as <span style&equals;"color&colon; &num;008000&semi;"><strong>activity&lowbar;main&period;xml<&sol;strong> <&sol;span>and ending point as <span style&equals;"color&colon; &num;008000&semi;"><strong>activity&lowbar;main&lowbar;animation&period;xml<&sol;strong><&sol;span>&period;<&sol;p>&NewLine;<pre>ConstraintLayout root&semi;&NewLine;ImageView imageView&semi;&NewLine;&NewLine;&commat;Override&NewLine;protected void onCreate&lpar;Bundle savedInstanceState&rpar; &lbrace;&NewLine; super&period;onCreate&lpar;savedInstanceState&rpar;&semi;&NewLine; setContentView&lpar;R&period;layout&period;activity&lowbar;main&rpar;&semi;&NewLine;&NewLine; root &equals; findViewById&lpar;R&period;id&period;root&rpar;&semi;&NewLine;&NewLine; imageView &equals; findViewById&lpar;R&period;id&period;imageView&rpar;&semi;&NewLine;&NewLine;&rcub;&NewLine;&NewLine;private void showAnimation&lpar;&rpar; &lbrace;&NewLine; show &equals; true&semi;&NewLine;&NewLine; ConstraintSet constraint1 &equals; new ConstraintSet&lpar;&rpar;&semi;&NewLine;&NewLine; constraint1&period;clone&lpar;this&comma; R&period;layout&period;activity&lowbar;main&lowbar;animation&rpar;&semi;&NewLine;&NewLine;&NewLine; TransitionManager&period;beginDelayedTransition&lpar;root&rpar;&semi;&NewLine; constraint1&period;applyTo&lpar;root&rpar;&semi;&NewLine;&rcub;<&sol;pre>&NewLine;<p class&equals;"p1">In our <strong><span style&equals;"color&colon; &num;008000&semi;">MainActivity&period;Java<&sol;span><&sol;strong>&comma; let’s create the click listener for our image and add the following code&period;<&sol;p>&NewLine;<p><span style&equals;"color&colon; &num;0000ff&semi;"><strong>MainActivity&period;java<&sol;strong><&sol;span><&sol;p>&NewLine;<pre>package com&period;example&period;constraintanimationdemo&semi;&NewLine;&NewLine;import android&period;os&period;Build&semi;&NewLine;import android&period;os&period;Bundle&semi;&NewLine;import android&period;transition&period;TransitionManager&semi;&NewLine;import android&period;view&period;View&semi;&NewLine;import android&period;widget&period;ImageView&semi;&NewLine;&NewLine;import androidx&period;appcompat&period;app&period;AppCompatActivity&semi;&NewLine;import androidx&period;constraintlayout&period;widget&period;ConstraintLayout&semi;&NewLine;import androidx&period;constraintlayout&period;widget&period;ConstraintSet&semi;&NewLine;&NewLine;public class MainActivity extends AppCompatActivity &lbrace;&NewLine;&NewLine; ConstraintLayout root&semi;&NewLine; ImageView imageView&semi;&NewLine; boolean show &equals; false&semi;&NewLine;&NewLine; &commat;Override&NewLine; protected void onCreate&lpar;Bundle savedInstanceState&rpar; &lbrace;&NewLine; super&period;onCreate&lpar;savedInstanceState&rpar;&semi;&NewLine; setContentView&lpar;R&period;layout&period;activity&lowbar;main&rpar;&semi;&NewLine;&NewLine; root &equals; findViewById&lpar;R&period;id&period;root&rpar;&semi;&NewLine;&NewLine; imageView &equals; findViewById&lpar;R&period;id&period;imageView&rpar;&semi;&NewLine;&NewLine; imageView&period;setOnClickListener&lpar;new View&period;OnClickListener&lpar;&rpar; &lbrace;&NewLine; &commat;Override&NewLine; public void onClick&lpar;View view&rpar; &lbrace;&NewLine;&NewLine; if &lpar;Build&period;VERSION&period;SDK&lowbar;INT &gt&semi;&equals; Build&period;VERSION&lowbar;CODES&period;KITKAT&rpar; &lbrace;&NewLine; if &lpar;show&rpar;&NewLine; revertAnimation&lpar;&rpar;&semi;&NewLine; else&NewLine; showAnimation&lpar;&rpar;&semi;&NewLine; &rcub;&NewLine; &rcub;&NewLine; &rcub;&rpar;&semi;&NewLine;&NewLine; &rcub;&NewLine;&NewLine; private void showAnimation&lpar;&rpar; &lbrace;&NewLine; show &equals; true&semi;&NewLine;&NewLine; ConstraintSet constraint1 &equals; new ConstraintSet&lpar;&rpar;&semi;&NewLine; constraint1&period;clone&lpar;this&comma; R&period;layout&period;activity&lowbar;main&lowbar;animation&rpar;&semi;&NewLine;&NewLine;&NewLine; TransitionManager&period;beginDelayedTransition&lpar;root&rpar;&semi;&NewLine; constraint1&period;applyTo&lpar;root&rpar;&semi;&NewLine; &rcub;&NewLine;&NewLine; private void revertAnimation&lpar;&rpar; &lbrace;&NewLine; show &equals; false&semi;&NewLine;&NewLine; ConstraintSet constraint2 &equals; new ConstraintSet&lpar;&rpar;&semi;&NewLine; constraint2&period;clone&lpar;this&comma; R&period;layout&period;activity&lowbar;main&rpar;&semi;&NewLine;&NewLine; TransitionManager&period;beginDelayedTransition&lpar;root&rpar;&semi;&NewLine; constraint2&period;applyTo&lpar;root&rpar;&semi;&NewLine;&NewLine; &rcub;&NewLine;&NewLine;&rcub;<&sol;pre>&NewLine;<h4 class&equals;"p1"><span style&equals;"color&colon; &num;000080&semi;"><b>Exploring MainActivity&period;Java<&sol;b><&sol;span><&sol;h4>&NewLine;<ul class&equals;"ul1">&NewLine;<li class&equals;"li2">We created 2 ConstraintSet&lpar;&rpar; — constraint1 and constraint2&period;<&sol;li>&NewLine;<li class&equals;"li2">We got the constraints from <strong><span style&equals;"color&colon; &num;008000&semi;">activity&lowbar;main&period;xml<&sol;span><&sol;strong> using <strong><span style&equals;"color&colon; &num;0000ff&semi;">clone&lpar;&rpar;<&sol;span><&sol;strong> and mapped it to constraint1&period;<&sol;li>&NewLine;<li class&equals;"li2">We got the constraints from<strong><span style&equals;"color&colon; &num;008000&semi;"> activity&lowbar;main&lowbar;animation&period;xml<&sol;span><&sol;strong> using <strong><span style&equals;"color&colon; &num;0000ff&semi;">clone&lpar;&rpar;<&sol;span><&sol;strong> and mapped it to constraint2&period;<&sol;li>&NewLine;<li class&equals;"li2">Then with the help of our <strong><span style&equals;"color&colon; &num;008000&semi;">TansitionManager<&sol;span><&sol;strong>&comma; we initiated a default transition using <strong><span style&equals;"color&colon; &num;008000&semi;">beginDelayedTransition&lpar;&rpar;<&sol;span><&sol;strong> to our root ConstraintLayout&period;<&sol;li>&NewLine;<li class&equals;"li2">Based on the set value&comma; we changed and applied the Constraints using the method <strong><span style&equals;"color&colon; &num;008000&semi;">applyTo&lpar;&rpar;&period;<&sol;span><&sol;strong><&sol;li>&NewLine;<&sol;ul>&NewLine;<p class&equals;"p1">Now&comma; that’s all&excl; no more additional steps&period; Run the app and check it yourself&period; Tap the image and you will see the keyframe animation in place&period;<&sol;p>&NewLine;<h4><strong><span style&equals;"color&colon; &num;000080&semi;">Duplicating XML layout&quest;<&sol;span><&sol;strong><&sol;h4>&NewLine;<p class&equals;"p1">As you can see&comma; we need to duplicate the layouts to achieve the animation or transition&period; But nobody would like to duplicate anything&period;<span style&equals;"color&colon; &num;008000&semi;"><strong> But when you clone the constraints&comma; it takes only the layout related constraints and attributes such as layout width and height&period; But not the styling attributes — such as textSize&comma; color&comma; etc&period;&period;<&sol;strong><&sol;span> So you need not replicate the exact styling in all the alternate layouts&period;<&sol;p>&NewLine;<h4><span style&equals;"color&colon; &num;000080&semi;"><strong>You can achieve the same result using transition framework&excl;<&sol;strong><&sol;span><&sol;h4>&NewLine;<p class&equals;"p1">You are right in that none of this is new&period; You can achieve the same result with the transition framework&period; However&comma; this becomes powerful when the animation you are trying to implement can be nicely specified using specific constraints &lpar;e&period;g&period; chained elements&comma; guidelines&comma; etc&period;&rpar; that would otherwise take a lot of work to achieve&period;<&sol;p>&NewLine;<p class&equals;"p1">ConstraintLayout will only perform animation on its direct children since it only knows when you change layout parameters and constraints on the children that it handles&period; It means that it won’t handle nested ViewGroups very nicely&period; But nested ConstraintLayout may solve this problem&period;<&sol;p>&NewLine;<p>I hope this article will help you in understanding the basics of how to implement  ConstraintLayout animations in android applications&period; Next&comma; we’ll see another use-case and we’ll also learn how to implement other transition effects&period;<&sol;p>&NewLine;<p>&nbsp&semi;<&sol;p>&NewLine;<p>&nbsp&semi;<&sol;p>&NewLine;<p>&nbsp&semi;<&sol;p>&NewLine;&NewLine;

Exit mobile version