android - Xamarin 表单 | Android - GroundOverlayOptions 中的引用图像

标签 android xamarin xamarin.android xamarin.forms

我正在使用 Android.gms.maps 类在我的 Xamarin Forms 应用程序中为 Android 手机创建自定义渲染器谷歌地图。我希望使用 GroundOverlayOptions

将图像添加到我的 map
    GroundOverlayOptions newarkMap = new GroundOverlayOptions()
    .InvokeImage(BitmapDescriptorFactory.FromResource(/*My image?*/))
    .position(e.Point, 8600f, 6500f);
     map.AddGroundOverlay(newarkMap);

我需要代码注释部分中的图像资源 ID。如何找出图像的资源 ID?有其他方法吗?

最佳答案

使用 BitmapDescriptorFactory关于如何获取 BitmapDescriptor,您有多种选择

  • FromResource
  • FromAsset
  • FromPath
  • FromBitmap

几个例子:

使用FromResource ,图像必须在您的资源树中,大概在 Drawable 中子文件夹,Xamarin 将以 Resource.Drawable.XXXXX 的形式为您生成一个 ID 描述符这将是一个整数常量:

var overlayOption = new GroundOverlayOptions()
    .InvokeImage(BitmapDescriptorFactory.FromResource(Resource.Drawable.overlayface2))
    .Position(e.Point, 5000f, 5000f);

enter image description here

FromAsset从“ Assets ”子文件夹加载图像:

var overlayOption = new GroundOverlayOptions()
    .InvokeImage(BitmapDescriptorFactory.FromAsset("OverlayFace.png"))
    .Position(e.Point, 5000f, 5000f);

enter image description here

FromPath从任意路径加载图像,因此您可以加载下载并存储在应用程序缓存目录中的图像,或存储在 sdcard 等中的图像...

var overlayOption3 = new GroundOverlayOptions()
    .InvokeImage(BitmapDescriptorFactory.FromPath("/mnt/sdcard/Download/OverlayFace.png"))
    .Position(e.Point, 5000f, 5000f);

注意:人们遇到的最大问题是没有正确设置图像大小,它以公里为单位。不是像素,不是英里等...

注意:另外,为了提高速度,请将图像大小保持在 2 的幂,否则每次加载叠加层(内存和性能命中)

关于android - Xamarin 表单 | Android - GroundOverlayOptions 中的引用图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45365375/

相关文章:

xamarin - Android Google Analytics 无法实例化接收器 com.google.analytics.tracking.android.CampaignTrackingReceiver

android - 如何在 Android 中自动捕获图像

android - Google Game Center 中的空指针异常

ios - 如何使用 webview 在 Xamarin.iOS 中实现手动 Facebook 登录

ios - 非泛型类型不能与类型参数一起使用 (Realm Xamarin)

javascript - 在 xamarin Android CrossWalk Web View 中启用 javascript 警报对话框

android - 我是否需要 SIP 服务器/注册器来实现 SIP 对讲系统?

从方法初始化的 Java Final 实例变量

c# - Xamarin iOS 10 横向

c# - MvxBind :Error: 10. 40 View 绑定(bind)时抛出异常