【问题标题】:Android: How do I add a translucent geofence over the mapAndroid:如何在地图上添加半透明地理围栏
【发布时间】:2015-11-18 06:17:55
【问题描述】:

Android:如何在地图上添加半透明地理围栏。即,我可以用颜色填充它,但下面的地图仍然可见。

This is what I wish to achieve

【问题讨论】:

  • 欢迎来到 StackOverFlow ,编辑你的问题
  • 你有你想要实现的截图吗?
  • 我已经在上面的链接中添加了图片。

标签: android android-maps android-geofence


【解决方案1】:

使用Color.argb()。要了解更多信息,请转到颜色类参考页面 (http://developer.android.com/reference/android/graphics/Color.html)。以下是制作蓝色半透明填充的示例:

Color.argb(
    100, //This is your alpha.  Adjust this to make it more or less translucent
    Color.red(Color.BLUE), //Red component.
    Color.green(Color.BLUE),  //Green component.
    Color.blue(Color.BLUE));  //Blue component.

这将使您想要的蓝色半透明颜色。如果您有自定义十六进制颜色 (0xFFFFFFFF),只需调整第一个字节 (0xFF......) 使其或多或少半透明。

编辑

这是一个简单的函数,您可以使用它来传递 alpha 和颜色来调整它:

int alphaAdjust(int alpha, int color) {
    return Color.argb(
            alpha, //This is your alpha.  Adjust this to make it more or less translucent
            Color.red(color), //Red component.
            Color.green(color),  //Green component.
            Color.blue(color));  //Blue component.
}

【讨论】:

    【解决方案2】:

    你可以创建一个Circle,很简单: 准备好地理围栏详细信息(geofLocation 和 geofRadius),您可以这样做:

    // Instantiates a new CircleOptions object and defines the center and radius
    CircleOptions circleOptions = new CircleOptions()
    .strokeColor(Color.BLACK) //Outer black border
    .fillColor(Color.TRANSPARENT) //inside of the geofence will be transparent, change to whatever color you prefer like 0x88ff0000 for mid-transparent red
        .center(geofLocation) // the LatLng Object of your geofence location
        .radius(geofRadius)); // The radius (in meters) of your geofence
    
    // Get back the mutable Circle
    Circle circle = myMap.addCircle(circleOptions);
    

    【讨论】:

    • 我可以添加一个圆圈,但我希望这个圆圈是半透明的。这只会给我一个圆周,而圆圈内没有油漆。
    • 所以用不透明的替换填充颜色值
    【解决方案3】:

    尝试documentation 中列出的示例。我建议使用半透明的圆圈来表示您的地理围栏。

    【讨论】:

      【解决方案4】:

      类似这样的:

      LatLng latLng = ...
      CircleOptions circleOptions = new CircleOptions();
      circleOptions.center(latLng)
                   .fillColor(Color.argb(64, 0, 255, 0))
                   .strokeColor(Color.GREEN)
                   .strokeWidth(1)
                   .radius(100);
      

      注意 Color.argb() 方法中的第一个参数(值 64),该参数控制圆的 alpha/transculency,在这种情况下它将是一个半透明的绿色圆。将第一个参数值从 0 更改为 255 以实现您想要的透明度。这是 Color.argb 方法 javadoc - http://developer.android.com/reference/android/graphics/Color.html#argb(int, int, int, int)

      【讨论】:

        猜你喜欢
        • 2019-03-24
        • 1970-01-01
        • 1970-01-01
        • 2018-04-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-07-16
        相关资源
        最近更新 更多