Skip to content

Commit

Permalink
fix(android): Clamp android animation scale values to accepted values
Browse files Browse the repository at this point in the history
  • Loading branch information
jeromelaban committed Jul 15, 2021
1 parent 4a7589e commit bb00bfe
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 24 deletions.
5 changes: 0 additions & 5 deletions src/SamplesApp/SamplesApp.net6mobile/Main.Android.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,7 @@
using Android.Views;
using Android.Widget;
using Windows.UI.Xaml.Media;

#if !NET6_0
using Com.Nostra13.Universalimageloader.Core;
#endif

[assembly: UsesPermission("android.permission.ACCESS_COARSE_LOCATION")]
[assembly: UsesPermission("android.permission.ACCESS_FINE_LOCATION")]
Expand Down Expand Up @@ -42,7 +39,6 @@ public Application(IntPtr javaReference, JniHandleOwnership transfer)

private void ConfigureUniversalImageLoader()
{
#if !NET6_0
// Create global configuration and initialize ImageLoader with this config
ImageLoaderConfiguration config = new ImageLoaderConfiguration
.Builder(Context)
Expand All @@ -51,7 +47,6 @@ private void ConfigureUniversalImageLoader()
ImageLoader.Instance.Init(config);

ImageSource.DefaultImageLoader = ImageLoader.Instance.LoadImageAsync;
#endif
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public class Given_Convert
[TestMethod]
public void When_Uri()
{
#if NET5_0
#if NET5_0 || NET6_0_OR_GREATER
string Expected = "http://platform.uno";
#else
string Expected = "http://platform.uno/";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,13 +83,16 @@ private static IValueAnimator GetGPUAnimator(this Timeline timeline, double star

if (target is ScaleTransform scale)
{
var nativeStartingValue = ToNativeScale(startingValue);
var nativeTargetValue = ToNativeScale(targetValue);

switch (property)
{
case nameof(ScaleTransform.ScaleX):
return new NativeValueAnimatorAdapter(GetRelativeAnimator(scale.View, "scaleX", startingValue, targetValue), PrepareScaleX(scale, startingValue), Complete(scale));
return new NativeValueAnimatorAdapter(GetRelativeAnimator(scale.View, "scaleX", nativeStartingValue, nativeTargetValue), PrepareScaleX(scale, nativeStartingValue), Complete(scale));

case nameof(ScaleTransform.ScaleY):
return new NativeValueAnimatorAdapter(GetRelativeAnimator(scale.View, "scaleY", startingValue, targetValue), PrepareScaleY(scale, startingValue), Complete(scale));
return new NativeValueAnimatorAdapter(GetRelativeAnimator(scale.View, "scaleY", nativeStartingValue, nativeTargetValue), PrepareScaleY(scale, nativeStartingValue), Complete(scale));
}
}

Expand All @@ -107,6 +110,7 @@ private static IValueAnimator GetGPUAnimator(this Timeline timeline, double star

if (target is CompositeTransform composite)
{

switch (property)
{
case nameof(CompositeTransform.TranslateX):
Expand All @@ -119,10 +123,14 @@ private static IValueAnimator GetGPUAnimator(this Timeline timeline, double star
return new NativeValueAnimatorAdapter(GetRelativeAnimator(composite.View, "rotation", startingValue, targetValue), PrepareAngle(composite, startingValue), Complete(composite));

case nameof(CompositeTransform.ScaleX):
return new NativeValueAnimatorAdapter(GetRelativeAnimator(composite.View, "scaleX", startingValue, targetValue), PrepareScaleX(composite, startingValue), Complete(composite));
var nativeStartingValueX = ToNativeScale(startingValue);
var nativetargetValueX = ToNativeScale(targetValue);
return new NativeValueAnimatorAdapter(GetRelativeAnimator(composite.View, "scaleX", nativeStartingValueX, nativetargetValueX), PrepareScaleX(composite, nativeStartingValueX), Complete(composite));

case nameof(CompositeTransform.ScaleY):
return new NativeValueAnimatorAdapter(GetRelativeAnimator(composite.View, "scaleY", startingValue, targetValue), PrepareScaleY(composite, startingValue), Complete(composite));
var nativeStartingValueY = ToNativeScale(startingValue);
var nativetargetValueY = ToNativeScale(targetValue);
return new NativeValueAnimatorAdapter(GetRelativeAnimator(composite.View, "scaleY", nativeStartingValueY, nativetargetValueY), PrepareScaleY(composite, nativeStartingValueY), Complete(composite));

//case nameof(CompositeTransform.SkewX):
// return ObjectAnimator.OfFloat(composite.View, "scaleX", ViewHelper.LogicalToPhysicalPixels(targetValue), startingValue);
Expand Down Expand Up @@ -216,8 +224,8 @@ private static void ResetPivot(View view)
// Apply transform using native values
OverridePivot(scale.View, scale.CenterX, scale.CenterY);
scale.View.ScaleX = (float)from;
scale.View.ScaleY = (float)scale.ScaleY;
scale.View.ScaleX = (float)ToNativeScale(from);
scale.View.ScaleY = (float)ToNativeScale(scale.ScaleY);
};

private static Action PrepareScaleY(ScaleTransform scale, double from) => () =>
Expand All @@ -227,8 +235,8 @@ private static void ResetPivot(View view)
// Apply transform using native values
OverridePivot(scale.View, scale.CenterX, scale.CenterY);
scale.View.ScaleX = (float)scale.ScaleX;
scale.View.ScaleY = (float)from;
scale.View.ScaleX = (float)ToNativeScale(scale.ScaleX);
scale.View.ScaleY = (float)ToNativeScale(from);
};

private static Action PrepareTranslateX(CompositeTransform composite, double from) => () =>
Expand All @@ -239,8 +247,8 @@ private static void ResetPivot(View view)
// Apply transform using native values
composite.View.TranslationX = ViewHelper.LogicalToPhysicalPixels(from);
composite.View.TranslationY = ViewHelper.LogicalToPhysicalPixels(composite.TranslateY);
composite.View.ScaleX = (float)composite.ScaleX;
composite.View.ScaleY = (float)composite.ScaleY;
composite.View.ScaleX = (float)ToNativeScale(composite.ScaleX);
composite.View.ScaleY = (float)ToNativeScale(composite.ScaleY);
composite.View.Rotation = (float)composite.Rotation;
};

Expand All @@ -252,8 +260,8 @@ private static void ResetPivot(View view)
// Apply transform using native values
composite.View.TranslationX = ViewHelper.LogicalToPhysicalPixels(composite.TranslateX);
composite.View.TranslationY = ViewHelper.LogicalToPhysicalPixels(from);
composite.View.ScaleX = (float)composite.ScaleX;
composite.View.ScaleY = (float)composite.ScaleY;
composite.View.ScaleX = (float)ToNativeScale(composite.ScaleX);
composite.View.ScaleY = (float)ToNativeScale(composite.ScaleY);
composite.View.Rotation = (float)composite.Rotation;
};

Expand All @@ -266,8 +274,8 @@ private static void ResetPivot(View view)
OverridePivot(composite.View, composite.CenterX, composite.CenterY);
composite.View.TranslationX = ViewHelper.LogicalToPhysicalPixels(composite.TranslateX);
composite.View.TranslationY = ViewHelper.LogicalToPhysicalPixels(composite.TranslateY);
composite.View.ScaleX = (float)composite.ScaleX;
composite.View.ScaleY = (float)composite.ScaleY;
composite.View.ScaleX = (float)ToNativeScale(composite.ScaleX);
composite.View.ScaleY = (float)ToNativeScale(composite.ScaleY);
composite.View.Rotation = (float)from;
};

Expand All @@ -280,8 +288,8 @@ private static void ResetPivot(View view)
OverridePivot(composite.View, composite.CenterX, composite.CenterY);
composite.View.TranslationX = ViewHelper.LogicalToPhysicalPixels(composite.TranslateX);
composite.View.TranslationY = ViewHelper.LogicalToPhysicalPixels(composite.TranslateY);
composite.View.ScaleX = (float)from;
composite.View.ScaleY = (float)composite.ScaleY;
composite.View.ScaleX = (float)ToNativeScale(from);
composite.View.ScaleY = (float)ToNativeScale(composite.ScaleY);
composite.View.Rotation = (float)composite.Rotation;
};

Expand All @@ -294,8 +302,8 @@ private static void ResetPivot(View view)
OverridePivot(composite.View, composite.CenterX, composite.CenterY);
composite.View.TranslationX = ViewHelper.LogicalToPhysicalPixels(composite.TranslateX);
composite.View.TranslationY = ViewHelper.LogicalToPhysicalPixels(composite.TranslateY);
composite.View.ScaleX = (float)composite.ScaleX;
composite.View.ScaleY = (float)from;
composite.View.ScaleX = (float)ToNativeScale(composite.ScaleX);
composite.View.ScaleY = (float)ToNativeScale(from);
composite.View.Rotation = (float)composite.Rotation;
};

Expand All @@ -322,5 +330,12 @@ private static ValueAnimator GetRelativeAnimator(Java.Lang.Object target, string
{
return ObjectAnimator.OfFloat(target, property, (float)from, (float)to);
}

/// <summary>
/// Ensures that scale value is without the android accepted values
/// </summary>
private static double ToNativeScale(double value)
=> double.IsNaN(value) ? 1 : value;

}
}

0 comments on commit bb00bfe

Please sign in to comment.