I wanted to Implement Spotlight feature in my app, as the Spotlight library is Android specific and I didn't knew how to access platform specific libraries from the core Forms project, @Jason recommended to use DependencyService and @Colex - MSFT helped in its implementation. There are few small things, which aren't working as expected.
I've basically this 3 buttons (1st on top-left, 2nd on top-right, 3rd on bottom-center).
The line animation has to start from the component (button), like this Preview from Spotlight library, but in my app they are starting from the top-left (all of them).
I want that when 1st button's spotlight is fade out, then the 2nd button's spotlight should fade in and when it's fade out, the third button's spotlight should fade in. But what's happening now is that when the 1st button's spotlight is fade out, the 2nd button's spotlight is already faded in (already enabled), and hence no animation (line starts from the component (button) and the text appears).
Droid\Control\SpotLightService.cs
[assembly: Xamarin.Forms.Dependency(typeof(SpotLightService))]
namespace CustomRenderer.Droid.Control
{
public class SpotLightService : ISpotLight
{
private bool isRevealEnabled_FirstButton_SpotLight = true;
private bool isRevealEnabled_SecondButton_SpotLight = true;
private bool isRevealEnabled_ThirdButton_SpotLight = true;
private SpotlightView FirstButton_SpotLight;
private SpotlightView SecondButton_SpotLight;
private SpotlightView ThirdButton_SpotLight;
public void ShowSpotLight_FirstButton(Xamarin.Forms.View view, string usageId)
{
FirstButton_SpotLight = new SpotlightView.Builder(MainActivity.Instance)
.IntroAnimationDuration(400)
.EnableRevealAnimation(isRevealEnabled_FirstButton_SpotLight)
.PerformClick(true)
.FadeinTextDuration(400)
.HeadingTvColor(Android.Graphics.Color.ParseColor("#eb273f"))
.HeadingTvSize(32)
.HeadingTvText("First Button")
.SubHeadingTvColor(Android.Graphics.Color.ParseColor("#eb273f"))
.SubHeadingTvSize(16)
.SubHeadingTvText("Lorem ipsum dolor sit amet, consectetur adipiscing elit")
.MaskColor(Android.Graphics.Color.ParseColor("#dc000000"))
.Target(ConvertFormsToNative(view))
.LineAnimDuration(400)
.LineAndArcColor(Android.Graphics.Color.ParseColor("#eb273f"))
.DismissOnTouch(true)
.DismissOnBackPress(true)
.EnableDismissAfterShown(true)
.UsageId(usageId)
.ShowTargetArc(true)
.Show();
}
public void ShowSpotLight_SecondButton(Xamarin.Forms.View view, string usageId)
{
SecondButton_SpotLight = new SpotlightView.Builder(MainActivity.Instance)
.IntroAnimationDuration(400)
.EnableRevealAnimation(isRevealEnabled_SecondButton_SpotLight)
.PerformClick(true)
.FadeinTextDuration(400)
.HeadingTvColor(Android.Graphics.Color.ParseColor("#eb273f"))
.HeadingTvSize(32)
.HeadingTvText("Second Button")
.SubHeadingTvColor(Android.Graphics.Color.ParseColor("#eb273f"))
.SubHeadingTvSize(16)
.SubHeadingTvText("Sed do eiusmod tempor incididunt ut labore eta")
.MaskColor(Android.Graphics.Color.ParseColor("#dc000000"))
.Target(ConvertFormsToNative(view))
.LineAnimDuration(400)
.LineAndArcColor(Android.Graphics.Color.ParseColor("#eb273f"))
.DismissOnTouch(true)
.DismissOnBackPress(true)
.EnableDismissAfterShown(true)
.UsageId(usageId)
.ShowTargetArc(true)
.Show();
}
public void ShowSpotLight_ThirdButton(Xamarin.Forms.View view, string usageId)
{
ThirdButton_SpotLight = new SpotlightView.Builder(MainActivity.Instance)
.IntroAnimationDuration(400)
.EnableRevealAnimation(isRevealEnabled_ThirdButton_SpotLight)
.PerformClick(true)
.FadeinTextDuration(400)
.HeadingTvColor(Android.Graphics.Color.ParseColor("#eb273f"))
.HeadingTvSize(32)
.HeadingTvText("Third Button")
.SubHeadingTvColor(Android.Graphics.Color.ParseColor("#eb273f"))
.SubHeadingTvSize(16)
.SubHeadingTvText("Ut enim ad minim veniam, quis nostrud exercitation")
.MaskColor(Android.Graphics.Color.ParseColor("#dc000000"))
.Target(ConvertFormsToNative(view))
.LineAnimDuration(400)
.LineAndArcColor(Android.Graphics.Color.ParseColor("#eb273f"))
.DismissOnTouch(true)
.DismissOnBackPress(true)
.EnableDismissAfterShown(true)
.UsageId(usageId)
.ShowTargetArc(true)
.Show();
}
public View ConvertFormsToNative(Xamarin.Forms.View view)
{
var vRenderer = Platform.CreateRendererWithContext(view, MainActivity.Instance);
var Androidview = vRenderer.View;
vRenderer.Tracker.UpdateLayout();
var size = view.Bounds;
var layoutParams = new ViewGroup.LayoutParams((int)size.Width, (int)size.Height);
Androidview.LayoutParameters = layoutParams;
view.Layout(size);
Androidview.Layout((int)size.X, (int)size.Y, (int)view.WidthRequest, (int)view.HeightRequest);
Androidview.SetBackgroundColor(Android.Graphics.Color.Red);
return Androidview;
}
}
}
MainPage.xml.cs
public partial class MainPage : ContentPage
{
public MainPage()
{
InitializeComponent();
LayoutChanged += FirstButton_SpotLight;
LayoutChanged += SecondButton_SpotLight;
LayoutChanged += ThirdButton_SpotLight;
}
bool isShown_FirstButton_SpotLight = false;
private void FirstButton_SpotLight(object sender, EventArgs e)
{
if (!isShown_FirstButton_SpotLight)
{
DependencyService.Get<ISpotLight>().ShowSpotLight_FirstButton(FirstButton, "FirstButton");
isShown_FirstButton_SpotLight = true;
}
}
bool isShown_SecondButton_SpotLight = false;
private void SecondButton_SpotLight(object sender, EventArgs e)
{
if (!isShown_SecondButton_SpotLight)
{
DependencyService.Get<ISpotLight>().ShowSpotLight_SecondButton(SecondButton, "SecondButton");
isShown_SecondButton_SpotLight = true;
}
}
bool isShown_ThirdButton_SpotLight = false;
private void ThirdButton_SpotLight(object sender, EventArgs e)
{
if (!isShown_ThirdButton_SpotLight)
{
DependencyService.Get<ISpotLight>().ShowSpotLight_ThirdButton(ThirdButton, "ThirdButton");
isShown_ThirdButton_SpotLight = true;
}
}
}