77
88namespace SharpConstraintLayout . Maui . Widget
99{
10+ /// <summary>
11+ /// If you need more complex animation, you can learn form here.
12+ /// </summary>
1013 public static class ConstraintLayoutAnimationExtension
1114 {
1215 public static void LayoutToWithAnim ( this ConstraintLayout layout , ConstraintSet finishSet , string animName , uint rate = 16 , uint length = 250 , Easing easing = null , Action < double , bool > finished = null , Func < bool > repeat = null )
@@ -20,19 +23,22 @@ public static void LayoutToWithAnim(this ConstraintLayout layout, ConstraintSet
2023
2124 public static Animation CreateAnimation ( this ConstraintLayout layout , ConstraintSet finish , Easing easing )
2225 {
23- var startLayoutTreeInfo = layout . CaptureLayoutTreeInfo ( ) ;
26+ var startLayoutTreeInfo = layout . CaptureLayoutTreeInfo ( true ) ;
2427 finish . ApplyToForAnim ( layout ) ;
2528 var finfishLayoutTreeInfo = layout . CaptureLayoutTreeInfo ( true ) ;
26- return GenerateAnimation ( layout , startLayoutTreeInfo , finfishLayoutTreeInfo , easing ) ;
29+ var anim = GenerateAnimation ( layout , startLayoutTreeInfo , finfishLayoutTreeInfo , easing ) ;
30+ return anim ;
2731 }
2832
29- public static Animation CreateAnimation ( this ConstraintLayout layout , ConstraintSet start , ConstraintSet finish )
33+ public static Animation CreateAnimation ( this ConstraintLayout layout , ConstraintSet start , ConstraintSet finish , Easing easing )
3034 {
3135 start . ApplyToForAnim ( layout ) ;
3236 var startLayoutTreeInfo = layout . CaptureLayoutTreeInfo ( true ) ;
3337 finish . ApplyToForAnim ( layout ) ;
3438 var finfishLayoutTreeInfo = layout . CaptureLayoutTreeInfo ( true ) ;
35- return GenerateAnimation ( layout , startLayoutTreeInfo , finfishLayoutTreeInfo ) ;
39+ var anim = GenerateAnimation ( layout , startLayoutTreeInfo , finfishLayoutTreeInfo , easing ) ;
40+ //start.ApplyToForAnim(layout);//restore start state
41+ return anim ;
3642 }
3743
3844 static Animation GenerateAnimation ( ConstraintLayout layout , Dictionary < int , ViewInfo > startLayoutTreeInfo , Dictionary < int , ViewInfo > finfishLayoutTreeInfo , Easing easing = null )
@@ -43,34 +49,40 @@ static Animation GenerateAnimation(ConstraintLayout layout, Dictionary<int, View
4349 var view = layout . FindElementById ( item . Key ) ;
4450 var startInfo = item . Value ;
4551 var finishInfo = finfishLayoutTreeInfo [ item . Key ] ;
52+ if ( startInfo . Equals ( finishInfo ) ) continue ;
53+ var diffInfo = startInfo . Diff ( finishInfo ) ;
4654 animation . Add ( 0 , 1 , new Animation ( ( v ) =>
4755 {
48- var rect = new Rect ( ( startInfo . X + ( finishInfo . X - startInfo . X ) * v ) ,
49- ( startInfo . Y + ( finishInfo . Y - startInfo . Y ) * v ) ,
50- ( startInfo . Size . Width + ( finishInfo . Size . Width - startInfo . Size . Width ) * v ) ,
51- ( startInfo . Size . Height + ( finishInfo . Size . Height - startInfo . Size . Height ) * v ) ) ;
52- layout . LayoutChild ( view , ( int ) rect . X , ( int ) rect . Y , ( int ) rect . Width , ( int ) rect . Height ) ;
53- //view.Layout(rect);
54- view . TranslationX = ( finishInfo . TranlateX - startInfo . TranlateX ) * v ;
55- view . TranslationY = ( finishInfo . TranlateY - startInfo . TranlateY ) * v ;
56- /*Func<double, Rect> computeBounds = progress =>
56+ if ( diffInfo . X != 0 || diffInfo . Y != 0 || diffInfo . Size . Width != 0 || diffInfo . Size . Height != 0 )
5757 {
58- double x = startInfo.X + (finishInfo.X - startInfo.X) * progress;
59- double y = startInfo.Y + (finishInfo.Y - startInfo.Y) * progress;
60- double w = startInfo.Size.Width + (finishInfo.Size.Width - finishInfo.Size.Width) * progress;
61- double h = startInfo.Size.Height + (finishInfo.Size.Height - finishInfo.Size.Height) * progress;
58+ var rect = new Rect ( ( startInfo . X + diffInfo . X * v ) ,
59+ ( startInfo . Y + diffInfo . Y * v ) ,
60+ ( startInfo . Size . Width + diffInfo . Size . Width * v ) ,
61+ ( startInfo . Size . Height + diffInfo . Size . Height * v ) ) ;
62+ layout . LayoutChild ( view , ( int ) rect . X , ( int ) rect . Y , ( int ) rect . Width , ( int ) rect . Height ) ;
63+ }
6264
63- return new Rect(x, y, w, h);
64- };
65- view.Layout(computeBounds(v));*/
66- view . Rotation = startInfo . Rotation + ( finishInfo . Rotation - startInfo . Rotation ) * v ;
67- view . RotationX = startInfo . RotationX + ( finishInfo . RotationX - startInfo . RotationX ) * v ;
68- view . RotationY = startInfo . RotationY + ( finishInfo . RotationY - startInfo . RotationY ) * v ;
65+ if ( diffInfo . TranlateX != 0 )
66+ view . TranslationX = diffInfo . TranlateX * v ;
67+ if ( diffInfo . TranlateY != 0 )
68+ view . TranslationY = diffInfo . TranlateY * v ;
69+ if ( diffInfo . Rotation != 0 )
70+ view . Rotation = startInfo . Rotation + diffInfo . Rotation * v ;
71+ if ( diffInfo . RotationX != 0 )
72+ view . RotationX = startInfo . RotationX + diffInfo . RotationX * v ;
73+ if ( diffInfo . RotationY != 0 )
74+ view . RotationY = startInfo . RotationY + diffInfo . RotationY * v ;
6975 //view.RotationZ = (finishInfo.RotationZ - startInfo.RotationZ) * v;
70- view . Scale = startInfo . Scale + ( finishInfo . Scale - startInfo . Scale ) * v ;
71- view . ScaleX = startInfo . ScaleX + ( finishInfo . ScaleX - startInfo . ScaleX ) * v ;
72- view . ScaleY = startInfo . ScaleY + ( finishInfo . ScaleY - startInfo . ScaleY ) * v ;
73- view . Opacity = startInfo . Alpha + ( finishInfo . Alpha - startInfo . Alpha ) * v ;
76+
77+ if ( diffInfo . Scale != 0 )
78+ view . Scale = startInfo . Scale + diffInfo . Scale * v ;
79+ if ( diffInfo . ScaleX != 0 )
80+ view . ScaleX = startInfo . ScaleX + diffInfo . ScaleX * v ;
81+ if ( diffInfo . ScaleY != 0 )
82+ view . ScaleY = startInfo . ScaleY + diffInfo . ScaleY * v ;
83+
84+ if ( diffInfo . Alpha != 0 )
85+ view . Opacity = startInfo . Alpha + diffInfo . Alpha * v ;
7486 } , 0 , 1 , easing ) ) ;
7587 }
7688 return animation ;
0 commit comments