Xamarin.Forms.GoogleMaps icon indicating copy to clipboard operation
Xamarin.Forms.GoogleMaps copied to clipboard

Custom BitmapDescriptor caching not working when FromView used

Open jocontacter opened this issue 5 years ago • 3 comments

VERSIONS

  • Xamarin.Forms.GoogleMaps - 3.0.5
  • Xamarin.Forms - 3.5.0.169

PLATFORMS

  • [x] Android
  • [ ] iOS

ACTUAL BEHAVIOR

Too low performance map.Add(pin) when used BitmapDescriptorFactory.FromView(on Samsung 8 for 95 pins ~14sec)

My workaround to not to use BitmapDescriptorFactory.FromView() but use BitmapDescriptorFactory.FromStream(null, some_id). You hid Utils class so I was forced to copypaste that code to my project and in CachingNativeBitmapDescriptorFactory cache Created AndroidBitmapDescriptor's from view. In such way performance is very well.

public AndroidBitmapDescriptor ToNative(BitmapDescriptor descriptor)
{
	if (!string.IsNullOrEmpty(descriptor.Id))
	{
		var discountText = descriptor.Id.Split("%");

		var cacheEntry = _cache.GetOrAdd(descriptor.Id, _ => Convert(new PinView(discountText[0]+"%", discountText[1] == "1" ? true : false)));
		return cacheEntry;
	}

	return DefaultBitmapDescriptorFactory.Instance.ToNative(descriptor);
}

private AndroidBitmapDescriptor Convert(Xamarin.Forms.View iconView)
{
	var nativeView = Utils.ConvertFormsToNative(
		iconView,
		new Xamarin.Forms.Rectangle(0, 0, Utils.DpToPx((float)iconView.WidthRequest), Utils.DpToPx((float)iconView.HeightRequest)),
		Xamarin.Forms.Platform.Android.Platform.CreateRendererWithContext(iconView, CrossCurrentActivity.Current.Activity));
	var otherView = new FrameLayout(nativeView.Context);
	nativeView.LayoutParameters = new FrameLayout.LayoutParams(Utils.DpToPx((float)iconView.WidthRequest), Utils.DpToPx((float)iconView.HeightRequest));
	otherView.AddView(nativeView);
	return Utils.ConvertViewToBitmapDescriptor(otherView);
}

jocontacter avatar Apr 08 '19 11:04 jocontacter

hello jocontacter;

can u share the utils library ?

mehmetulutug avatar Jul 09 '19 15:07 mehmetulutug

hello jocontacter;

can u share the utils library ?

check message attaches If you want some example - welcome

AndroidUtils.txt iOSUtils.txt

jocontacter avatar Jul 20 '19 16:07 jocontacter

Hey @jocontacter could you please show me example of using the utils library?

dejanbasic avatar Apr 05 '20 19:04 dejanbasic