Site icon C1CTech

Android ConstraintLayout Animation:Part II

<p>In the previous article&comma; <strong><span style&equals;"color&colon; &num;0000ff&semi;"><a style&equals;"color&colon; &num;0000ff&semi;" href&equals;"https&colon;&sol;&sol;c1ctech&period;com&sol;android-constraintlayout-animation&sol;">Android ConstraintLayout Animation&colon; Part I<&sol;a> <&sol;span><&sol;strong>we have talked about how to implement keyframe animation with a simple example using <span style&equals;"color&colon; &num;008000&semi;"><strong>ConstraintLayout<&sol;strong><&sol;span> and <strong><span style&equals;"color&colon; &num;008000&semi;">ConstraintSet&period;<&sol;span><&sol;strong><span style&equals;"color&colon; &num;008000&semi;"><span style&equals;"color&colon; &num;000000&semi;">We also peeked into few methods which made our animation possible&period;<&sol;span><&sol;span><&sol;p>&NewLine;<p>Now In this article&comma; we’ll see another use-case and we’ll also learn how to implement other transition effects&period;<&sol;p>&NewLine;<p>Let’s see first what we’re going to build&colon; Our goal is to create an animation that shows-off the Hampi detail when the user presses the screen&comma; with seamless and fluid animation&period;<&sol;p>&NewLine;<p><span style&equals;"color&colon; &num;0000ff&semi;"><strong>Get GITHUB code from <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;AndroidConstraintAnimationDemo">here<&sol;a><&sol;span>&period;<&sol;strong><&sol;span><&sol;p>&NewLine;<p><amp-youtube layout&equals;"responsive" width&equals;"1200" height&equals;"675" data-videoid&equals;"aMdioi-LK6U" title&equals;"Android Constraint Layout Animation"><a placeholder href&equals;"https&colon;&sol;&sol;youtu&period;be&sol;aMdioi-LK6U"><img src&equals;"https&colon;&sol;&sol;i&period;ytimg&period;com&sol;vi&sol;aMdioi-LK6U&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 id&equals;"081e" class&equals;"ia ib cn ar ic b id ie if ig ih ii ij ik il im in" data-selectable-paragraph&equals;"">This animation is created with two different layout files&colon; <span style&equals;"color&colon; &num;008000&semi;"><strong>hampi&period;xml<&sol;strong><&sol;span> and <strong><span style&equals;"color&colon; &num;008000&semi;">hampi&lowbar;detail&period;xml&period;<&sol;span><&sol;strong><&sol;p>&NewLine;<h3 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;h3>&NewLine;<p>Let’s create our first layout of the <span style&equals;"color&colon; &num;008000&semi;"><strong>MainActivity<&sol;strong><&sol;span> which is <span style&equals;"color&colon; &num;008000&semi;"><b>hampi&period;xml&period;<&sol;b><&sol;span> In this layout&comma; the views are the same as the second layout&comma; but they are hidden outside the layout&period;<&sol;p>&NewLine;<p><img class&equals;" wp-image-1415 aligncenter" src&equals;"https&colon;&sol;&sol;c1ctech&period;com&sol;wp-content&sol;uploads&sol;2019&sol;12&sol;Screenshot-2019-12-05-19&period;47&period;14&period;png" alt&equals;"Screenshot 2019-12-05 19&period;47&period;14" width&equals;"605" height&equals;"527" &sol;><&sol;p>&NewLine;<p><span style&equals;"color&colon; &num;0000ff&semi;"><strong>hampi&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;id&equals;"&commat;&plus;id&sol;root"&NewLine; android&colon;layout&lowbar;width&equals;"match&lowbar;parent"&NewLine; android&colon;layout&lowbar;height&equals;"match&lowbar;parent"&NewLine; android&colon;background&equals;"&num;181818"&NewLine; tools&colon;context&equals;"&period;MainActivity"&gt&semi;&NewLine;&NewLine; &lt&semi;ImageView&NewLine; android&colon;id&equals;"&commat;&plus;id&sol;backgroundImage"&NewLine; android&colon;layout&lowbar;width&equals;"0dp"&NewLine; android&colon;layout&lowbar;height&equals;"0dp"&NewLine; android&colon;scaleType&equals;"centerCrop"&NewLine; android&colon;src&equals;"&commat;drawable&sol;hampi&lowbar;img"&NewLine; app&colon;layout&lowbar;constraintBottom&lowbar;toBottomOf&equals;"parent"&NewLine; app&colon;layout&lowbar;constraintLeft&lowbar;toLeftOf&equals;"parent"&NewLine; app&colon;layout&lowbar;constraintRight&lowbar;toRightOf&equals;"parent"&NewLine; app&colon;layout&lowbar;constraintTop&lowbar;toTopOf&equals;"parent" &sol;&gt&semi;&NewLine;&NewLine; &lt&semi;TextView&NewLine; android&colon;id&equals;"&commat;&plus;id&sol;location"&NewLine; android&colon;layout&lowbar;width&equals;"wrap&lowbar;content"&NewLine; android&colon;layout&lowbar;height&equals;"wrap&lowbar;content"&NewLine; android&colon;background&equals;"&num;d3d3d3"&NewLine; android&colon;paddingStart&equals;"16dp"&NewLine; android&colon;paddingTop&equals;"3dp"&NewLine; android&colon;paddingEnd&equals;"16dp"&NewLine; android&colon;paddingBottom&equals;"3dp"&NewLine; android&colon;text&equals;"Ballari district&comma; Karnataka"&NewLine; android&colon;textSize&equals;"12sp"&NewLine; app&colon;layout&lowbar;constraintBottom&lowbar;toBottomOf&equals;"&commat;&plus;id&sol;title"&NewLine; app&colon;layout&lowbar;constraintRight&lowbar;toRightOf&equals;"&commat;&plus;id&sol;title" &sol;&gt&semi;&NewLine;&NewLine; &lt&semi;TextView&NewLine; android&colon;id&equals;"&commat;&plus;id&sol;title"&NewLine; android&colon;layout&lowbar;width&equals;"wrap&lowbar;content"&NewLine; android&colon;layout&lowbar;height&equals;"wrap&lowbar;content"&NewLine; android&colon;layout&lowbar;marginTop&equals;"48dp"&NewLine; android&colon;background&equals;"&num;673AB7"&NewLine; android&colon;paddingStart&equals;"24dp"&NewLine; android&colon;paddingTop&equals;"8dp"&NewLine; android&colon;paddingEnd&equals;"24dp"&NewLine; android&colon;paddingBottom&equals;"8dp"&NewLine; android&colon;text&equals;"Hampi"&NewLine; android&colon;textColor&equals;"&num;FFFF"&NewLine; android&colon;textSize&equals;"45sp"&NewLine; app&colon;layout&lowbar;constraintRight&lowbar;toLeftOf&equals;"&commat;&plus;id&sol;backgroundImage"&NewLine; app&colon;layout&lowbar;constraintTop&lowbar;toTopOf&equals;"parent" &sol;&gt&semi;&NewLine;&NewLine; &lt&semi;View&NewLine; android&colon;id&equals;"&commat;&plus;id&sol;fadeView"&NewLine; android&colon;layout&lowbar;width&equals;"wrap&lowbar;content"&NewLine; android&colon;layout&lowbar;height&equals;"90dp"&NewLine; android&colon;foreground&equals;"&commat;drawable&sol;gradient"&NewLine; app&colon;layout&lowbar;constraintBottom&lowbar;toTopOf&equals;"&commat;&plus;id&sol;description"&NewLine; app&colon;layout&lowbar;constraintEnd&lowbar;toEndOf&equals;"parent"&NewLine; app&colon;layout&lowbar;constraintStart&lowbar;toStartOf&equals;"parent" &sol;&gt&semi;&NewLine;&NewLine; &lt&semi;TextView&NewLine; android&colon;id&equals;"&commat;&plus;id&sol;tap"&NewLine; android&colon;layout&lowbar;width&equals;"wrap&lowbar;content"&NewLine; android&colon;layout&lowbar;height&equals;"wrap&lowbar;content"&NewLine; android&colon;layout&lowbar;marginStart&equals;"8dp"&NewLine; android&colon;layout&lowbar;marginEnd&equals;"8dp"&NewLine; android&colon;layout&lowbar;marginBottom&equals;"12dp"&NewLine; android&colon;text&equals;"Tap for info"&NewLine; android&colon;textColor&equals;"&num;ffffff"&NewLine; android&colon;textSize&equals;"15sp"&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" &sol;&gt&semi;&NewLine;&NewLine; &lt&semi;TextView&NewLine; android&colon;id&equals;"&commat;&plus;id&sol;description"&NewLine; android&colon;layout&lowbar;width&equals;"wrap&lowbar;content"&NewLine; android&colon;layout&lowbar;height&equals;"wrap&lowbar;content"&NewLine; android&colon;background&equals;"&num;181818"&NewLine; android&colon;gravity&equals;"center"&NewLine; android&colon;paddingStart&equals;"8dp"&NewLine; android&colon;paddingEnd&equals;"8dp"&NewLine; android&colon;paddingBottom&equals;"8dp"&NewLine; android&colon;text&equals;"&commat;string&sol;hampi&lowbar;detail"&NewLine; android&colon;textColor&equals;"&num;FFFF"&NewLine; android&colon;textSize&equals;"22sp"&NewLine; app&colon;layout&lowbar;constraintLeft&lowbar;toLeftOf&equals;"parent"&NewLine; app&colon;layout&lowbar;constraintRight&lowbar;toRightOf&equals;"parent"&NewLine; app&colon;layout&lowbar;constraintTop&lowbar;toBottomOf&equals;"&commat;&plus;id&sol;backgroundImage" &sol;&gt&semi;&NewLine;&NewLine;&NewLine;&lt&semi;&sol;androidx&period;constraintlayout&period;widget&period;ConstraintLayout&gt&semi;<&sol;pre>&NewLine;<p data-selectable-paragraph&equals;"">Let’s create our final layout or the second frame — <span style&equals;"color&colon; &num;008000&semi;"><strong>hampi&lowbar;datail&period;xml<&sol;strong><&sol;span>&period; This layout consists of the final views position&period;<&sol;p>&NewLine;<p data-selectable-paragraph&equals;""><img class&equals;" wp-image-1416 aligncenter" src&equals;"https&colon;&sol;&sol;c1ctech&period;com&sol;wp-content&sol;uploads&sol;2019&sol;12&sol;Screenshot-2019-12-05-19&period;48&period;15&period;png" alt&equals;"Screenshot 2019-12-05 19&period;48&period;15" width&equals;"622" height&equals;"543" &sol;><&sol;p>&NewLine;<p data-selectable-paragraph&equals;""><span style&equals;"color&colon; &num;0000ff&semi;"><strong>hampi&lowbar;datail&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; android&colon;layout&lowbar;width&equals;"match&lowbar;parent"&NewLine; android&colon;layout&lowbar;height&equals;"match&lowbar;parent"&gt&semi;&NewLine;&NewLine; &lt&semi;ImageView&NewLine; android&colon;id&equals;"&commat;&plus;id&sol;backgroundImage"&NewLine; android&colon;layout&lowbar;width&equals;"0dp"&NewLine; android&colon;layout&lowbar;height&equals;"0dp"&NewLine; android&colon;scaleType&equals;"centerCrop"&NewLine; android&colon;src&equals;"&commat;drawable&sol;hampi&lowbar;img"&NewLine; app&colon;layout&lowbar;constraintBottom&lowbar;toTopOf&equals;"&commat;&plus;id&sol;description"&NewLine; app&colon;layout&lowbar;constraintLeft&lowbar;toLeftOf&equals;"parent"&NewLine; app&colon;layout&lowbar;constraintRight&lowbar;toRightOf&equals;"parent"&NewLine; app&colon;layout&lowbar;constraintTop&lowbar;toTopOf&equals;"parent" &sol;&gt&semi;&NewLine;&NewLine; &lt&semi;TextView&NewLine; android&colon;id&equals;"&commat;&plus;id&sol;title"&NewLine; android&colon;layout&lowbar;width&equals;"wrap&lowbar;content"&NewLine; android&colon;layout&lowbar;height&equals;"wrap&lowbar;content"&NewLine; android&colon;layout&lowbar;marginTop&equals;"48dp"&NewLine; android&colon;background&equals;"&num;673AB7"&NewLine; android&colon;paddingStart&equals;"24dp"&NewLine; android&colon;paddingTop&equals;"8dp"&NewLine; android&colon;paddingEnd&equals;"24dp"&NewLine; android&colon;paddingBottom&equals;"8dp"&NewLine; android&colon;text&equals;"Hampi"&NewLine; android&colon;textColor&equals;"&num;FFFF"&NewLine; android&colon;textSize&equals;"45sp"&NewLine; app&colon;layout&lowbar;constraintLeft&lowbar;toLeftOf&equals;"parent"&NewLine; app&colon;layout&lowbar;constraintTop&lowbar;toTopOf&equals;"parent" &sol;&gt&semi;&NewLine;&NewLine; &lt&semi;TextView&NewLine; android&colon;id&equals;"&commat;&plus;id&sol;location"&NewLine; android&colon;layout&lowbar;width&equals;"wrap&lowbar;content"&NewLine; android&colon;layout&lowbar;height&equals;"wrap&lowbar;content"&NewLine; android&colon;background&equals;"&num;d3d3d3"&NewLine; android&colon;paddingStart&equals;"16dp"&NewLine; android&colon;paddingTop&equals;"3dp"&NewLine; android&colon;paddingEnd&equals;"16dp"&NewLine; android&colon;paddingBottom&equals;"3dp"&NewLine; android&colon;text&equals;"Ballari district&comma; Karnataka"&NewLine; android&colon;textSize&equals;"12sp"&NewLine; app&colon;layout&lowbar;constraintRight&lowbar;toRightOf&equals;"&commat;&plus;id&sol;title"&NewLine; app&colon;layout&lowbar;constraintTop&lowbar;toBottomOf&equals;"&commat;&plus;id&sol;title" &sol;&gt&semi;&NewLine;&NewLine; &lt&semi;View&NewLine; android&colon;id&equals;"&commat;&plus;id&sol;fadeView"&NewLine; android&colon;layout&lowbar;width&equals;"wrap&lowbar;content"&NewLine; android&colon;layout&lowbar;height&equals;"30dp"&NewLine; android&colon;foreground&equals;"&commat;drawable&sol;gradient"&NewLine; app&colon;layout&lowbar;constraintBottom&lowbar;toTopOf&equals;"&commat;&plus;id&sol;description"&NewLine; app&colon;layout&lowbar;constraintEnd&lowbar;toEndOf&equals;"parent"&NewLine; app&colon;layout&lowbar;constraintStart&lowbar;toStartOf&equals;"parent" &sol;&gt&semi;&NewLine;&NewLine; &lt&semi;TextView&NewLine; android&colon;id&equals;"&commat;&plus;id&sol;tap"&NewLine; android&colon;layout&lowbar;width&equals;"wrap&lowbar;content"&NewLine; android&colon;layout&lowbar;height&equals;"wrap&lowbar;content"&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; android&colon;text&equals;"Tap for info"&NewLine; android&colon;textColor&equals;"&num;ffffff"&NewLine; android&colon;textSize&equals;"15sp"&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" &sol;&gt&semi;&NewLine;&NewLine; &lt&semi;TextView&NewLine; android&colon;id&equals;"&commat;&plus;id&sol;description"&NewLine; android&colon;layout&lowbar;width&equals;"wrap&lowbar;content"&NewLine; android&colon;layout&lowbar;height&equals;"wrap&lowbar;content"&NewLine; android&colon;background&equals;"&num;181818"&NewLine; android&colon;gravity&equals;"center"&NewLine; android&colon;paddingStart&equals;"8dp"&NewLine; android&colon;paddingEnd&equals;"8dp"&NewLine; android&colon;paddingBottom&equals;"8dp"&NewLine; android&colon;text&equals;"&commat;string&sol;hampi&lowbar;detail"&NewLine; android&colon;textColor&equals;"&num;FFFF"&NewLine; android&colon;textSize&equals;"22sp"&NewLine; app&colon;layout&lowbar;constraintBottom&lowbar;toBottomOf&equals;"parent"&NewLine; app&colon;layout&lowbar;constraintLeft&lowbar;toLeftOf&equals;"parent"&NewLine; app&colon;layout&lowbar;constraintRight&lowbar;toRightOf&equals;"parent" &sol;&gt&semi;&NewLine;&NewLine;&NewLine;&lt&semi;&sol;androidx&period;constraintlayout&period;widget&period;ConstraintLayout&gt&semi;<&sol;pre>&NewLine;<p id&equals;"9ff9" class&equals;"ia ib cn ar ic b id ie if ig ih ii ij ik il im in" data-selectable-paragraph&equals;"">As you can see&comma; the layouts are exactly the same&comma; the only difference is that in the first one &lpar;<span style&equals;"color&colon; &num;008000&semi;"><strong>hampi&period;xml<&sol;strong><&sol;span>&rpar; the elements are placed offscreen&comma; while in the second one they are in the desired position&period;<&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;<h3><span style&equals;"color&colon; &num;000080&semi;"><strong>Creating Animation<&sol;strong><&sol;span><&sol;h3>&NewLine;<p data-selectable-paragraph&equals;"">So&comma; let’s build the animation and let’s see how is incredibly easy to make animations with ConstraintLayout and ConstraintSet&period;<&sol;p>&NewLine;<p class&equals;"p1">First&comma; we need to create a new instance of ConstraintSet&comma; then we can clone the constraints of the second layout &lpar;<strong><span style&equals;"color&colon; &num;008000&semi;">hampi&lowbar;detail&period;xml<&sol;span><&sol;strong>&rpar; by calling the <strong><span style&equals;"color&colon; &num;0000ff&semi;">clone&lpar;&rpar;<&sol;span><&sol;strong> method&period;<&sol;p>&NewLine;<pre>ConstraintSet constraintSet &equals; new ConstraintSet&lpar;&rpar;&semi;&NewLine;constraintSet&period;clone&lpar;this&comma; R&period;layout&period;hampi&lowbar;detail&rpar;&semi;<&sol;pre>&NewLine;<p><b><span style&equals;"color&colon; &num;0000ff&semi;">clone&lpar;&rpar;<&sol;span> &colon; <&sol;b>It 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>Let’s add some transition effects&period;<&sol;p>&NewLine;<p class&equals;"p1">Now&comma; let’s create the transition object used to set the interpolator and the duration of animation&colon;<&sol;p>&NewLine;<pre>ChangeBounds transition &equals; new ChangeBounds&lpar;&rpar;&semi;&NewLine;transition&period;setInterpolator&lpar;new AnticipateOvershootInterpolator&lpar;1&period;0f&rpar;&rpar;&semi;&NewLine;transition&period;setDuration&lpar;1200&rpar;&semi;<&sol;pre>&NewLine;<p class&equals;"p1"><span style&equals;"color&colon; &num;0000ff&semi;"><strong>ChangeBounds&lpar;&rpar;<&sol;strong><&sol;span> captures the layout bounds of target views before and after the scene change and animates those changes during the transition&period;<&sol;p>&NewLine;<p class&equals;"p1">Then we define the type of interpolator — which defines the rate of change of an animation&period; This allows the basic animation effects &lpar;alpha&comma; scale&comma; translate&comma; rotate&rpar; to be accelerated&comma; decelerated&comma; repeated&comma; etc&period; Here’s it’s <strong><span style&equals;"color&colon; &num;008000&semi;">AnticipateOverShootInterpolator<&sol;span><&sol;strong>&period;<&sol;p>&NewLine;<p class&equals;"p1">And&comma; last but not least&comma; we need to call the <strong><span style&equals;"color&colon; &num;008000&semi;">TransitionManager&period;beginDelayedTransition&lpar;&rpar;<&sol;span><&sol;strong> used to create a new scene and to run the transition on the next rendering frame&period; Lastly&comma; we call <span style&equals;"color&colon; &num;008000&semi;"><strong>applyTo&lpar;&rpar;<&sol;strong><&sol;span> to finally start the animation&period;<&sol;p>&NewLine;<pre>TransitionManager&period;beginDelayedTransition&lpar;root&comma; transition&rpar;&semi;&NewLine;&NewLine;<span style&equals;"color&colon; &num;008000&semi;"><strong>&sol;&sol;here root is the name of view to which we are applying the constraintSet<&sol;strong><&sol;span>&NewLine;constraintSet&period;applyTo&lpar;root&rpar;&semi;<&sol;pre>&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 <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;AutoTransition&period;html"><span class&equals;"s1">AutoTransition<&sol;span><&sol;a><&sol;strong><&sol;span> 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 <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;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;strong> <&sol;span>with a value of null for the transition parameter&period;<&sol;p>&NewLine;<p><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;<p>Below you can see you have to write only these few lines of code to achieve this animation&period;<&sol;p>&NewLine;<pre>private void showComponents&lpar;&rpar; &lbrace;&NewLine;&NewLine; ConstraintSet constraintSet &equals; new ConstraintSet&lpar;&rpar;&semi;&NewLine; constraintSet&period;clone&lpar;this&comma; R&period;layout&period;hampi&lowbar;detail&rpar;&semi;&NewLine;&NewLine; ChangeBounds transition &equals; new ChangeBounds&lpar;&rpar;&semi;&NewLine; transition&period;setInterpolator&lpar;new AnticipateOvershootInterpolator&lpar;1&period;0f&rpar;&rpar;&semi;&NewLine; transition&period;setDuration&lpar;1200&rpar;&semi;&NewLine;&NewLine; TransitionManager&period;beginDelayedTransition&lpar;root&comma; transition&rpar;&semi;&NewLine;&NewLine;<strong><span style&equals;"color&colon; &num;008000&semi;"> &sol;&sol;here root is the name of view to which we are applying the constraintSet<&sol;span><&sol;strong>&NewLine; constraintSet&period;applyTo&lpar;root&rpar;&semi;&NewLine;&rcub;<&sol;pre>&NewLine;<p>In our <span style&equals;"color&colon; &num;008000&semi;"><strong>MainActivity&period;Java<&sol;strong><&sol;span>&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;androidconstraintdemo&semi;&NewLine;&NewLine;import android&period;os&period;Bundle&semi;&NewLine;import android&period;transition&period;ChangeBounds&semi;&NewLine;import android&period;transition&period;TransitionManager&semi;&NewLine;import android&period;view&period;View&semi;&NewLine;import android&period;view&period;animation&period;AnticipateOvershootInterpolator&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 backgroundImage&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;hampi&rpar;&semi;&NewLine;&NewLine; root &equals; findViewById&lpar;R&period;id&period;root&rpar;&semi;&NewLine; backgroundImage &equals; findViewById&lpar;R&period;id&period;backgroundImage&rpar;&semi;&NewLine;&NewLine; backgroundImage&period;setOnClickListener&lpar;new View&period;OnClickListener&lpar;&rpar; &lbrace;&NewLine; &commat;Override&NewLine; public void onClick&lpar;View view&rpar; &lbrace;&NewLine; if &lpar;show&rpar;&NewLine;<span style&equals;"color&colon; &num;008000&semi;"><strong> &sol;&sol; if the animation is shown&comma; we hide back the views<&sol;strong><&sol;span>&NewLine; hideComponents&lpar;&rpar;&semi; &NewLine; else&NewLine;<strong><span style&equals;"color&colon; &num;008000&semi;"> &sol;&sol; if the animation is NOT shown&comma; we animate the views<&sol;span><&sol;strong>&NewLine; showComponents&lpar;&rpar;&semi;&NewLine; &rcub;&NewLine; &rcub;&rpar;&semi;&NewLine; &rcub;&NewLine;&NewLine;&NewLine; private void showComponents&lpar;&rpar; &lbrace;&NewLine;&NewLine; show &equals; true&semi;&NewLine; ConstraintSet constraintSet &equals; new ConstraintSet&lpar;&rpar;&semi;&NewLine; constraintSet&period;clone&lpar;this&comma; R&period;layout&period;hampi&lowbar;detail&rpar;&semi;&NewLine;&NewLine; ChangeBounds transition &equals; new ChangeBounds&lpar;&rpar;&semi;&NewLine; transition&period;setInterpolator&lpar;new AnticipateOvershootInterpolator&lpar;1&period;0f&rpar;&rpar;&semi;&NewLine; transition&period;setDuration&lpar;1200&rpar;&semi;&NewLine;&NewLine; TransitionManager&period;beginDelayedTransition&lpar;root&comma; transition&rpar;&semi;&NewLine;&NewLine;<span style&equals;"color&colon; &num;008000&semi;"><strong> &sol;&sol;here root is the name of view to which we are applying the constraintSet<&sol;strong><&sol;span>&NewLine; constraintSet&period;applyTo&lpar;root&rpar;&semi;&NewLine; &rcub;&NewLine;&NewLine; private void hideComponents&lpar;&rpar; &lbrace;&NewLine; show &equals; false&semi;&NewLine;&NewLine; ConstraintSet constraintSet &equals; new ConstraintSet&lpar;&rpar;&semi;&NewLine; constraintSet&period;clone&lpar;this&comma; R&period;layout&period;hampi&rpar;&semi;&NewLine;&NewLine; ChangeBounds transition &equals; new ChangeBounds&lpar;&rpar;&semi;&NewLine; transition&period;setInterpolator&lpar;new AnticipateOvershootInterpolator&lpar;1&period;0f&rpar;&rpar;&semi;&NewLine; transition&period;setDuration&lpar;1200&rpar;&semi;&NewLine;&NewLine; TransitionManager&period;beginDelayedTransition&lpar;root&comma; transition&rpar;&semi;&NewLine;&NewLine; constraintSet&period;applyTo&lpar;root&rpar;&semi;&NewLine; &rcub;&NewLine;&NewLine;&NewLine;&rcub;<&sol;pre>&NewLine;<p>I hope this article will help you in understanding another use-case of how to implement ConstraintLayout animations with transition effect in android applications&period;&NewLine;

Exit mobile version