使用 osmdroid 的夜间模式

Mic*_*zuk 1 android openstreetmap osmdroid

是否可以使用 osmdroid 实现地图的夜间模式?

我的想法是为所有资源添加一些过滤器,但也许有更好的解决方案?

小智 5

从 5.1 版开始,您可以这样设置:

this.mMapView.getOverlayManager().getTilesOverlay().setColorFilter(TilesOverlay.INVERT_COLORS);


ecl*_*cle 5

默认ColorMatrixColorFilter版本TilesOverlay.INVERT_COLORS不符合我的喜好。因此,我通过更改地图的夜间模式颜色过滤器(晚上 7 点之后和早上 7 点之前)制作了自己的自定义版本,并从Medium: Welcome to the (Color) Matrix 中获取提示:

SimpleDateFormat format = new SimpleDateFormat("HH", Locale.getDefault());
String hour = format.format(Tools.getTime());

//night mode after 7 PM and before 7 AM
if (Integer.valueOf(hour) < 7 || Integer.valueOf(hour) > 19) {
    //taking cue from https://medium.com/square-corner-blog/welcome-to-the-color-matrix-64d112e3f43d
    ColorMatrix inverseMatrix = new ColorMatrix(new float[] {
            -1.0f, 0.0f, 0.0f, 0.0f, 255f,
            0.0f, -1.0f, 0.0f, 0.0f, 255f,
            0.0f, 0.0f, -1.0f, 0.0f, 255f,
            0.0f, 0.0f, 0.0f, 1.0f, 0.0f
    });

    int destinationColor = Color.parseColor("#FF2A2A2A");
    float lr = (255.0f - Color.red(destinationColor))/255.0f;
    float lg = (255.0f - Color.green(destinationColor))/255.0f;
    float lb = (255.0f - Color.blue(destinationColor))/255.0f;
    ColorMatrix grayscaleMatrix = new ColorMatrix(new float[] {
            lr, lg, lb, 0, 0, //
            lr, lg, lb, 0, 0, //
            lr, lg, lb, 0, 0, //
            0, 0, 0, 0, 255, //
    });
    grayscaleMatrix.preConcat(inverseMatrix);
    int dr = Color.red(destinationColor);
    int dg = Color.green(destinationColor);
    int db = Color.blue(destinationColor);
    float drf = dr / 255f;
    float dgf = dg / 255f;
    float dbf = db / 255f;
    ColorMatrix tintMatrix = new ColorMatrix(new float[] {
            drf, 0, 0, 0, 0, //
            0, dgf, 0, 0, 0, //
            0, 0, dbf, 0, 0, //
            0, 0, 0, 1, 0, //
    });
    tintMatrix.preConcat(grayscaleMatrix);
    float lDestination = drf * lr + dgf * lg + dbf * lb;
    float scale = 1f - lDestination;
    float translate = 1 - scale * 0.5f;
    ColorMatrix scaleMatrix = new ColorMatrix(new float[] {
            scale, 0, 0, 0, dr * translate, //
            0, scale, 0, 0, dg * translate, //
            0, 0, scale, 0, db * translate, //
            0, 0, 0, 1, 0, //
    });
    scaleMatrix.preConcat(tintMatrix);
    ColorMatrixColorFilter filter = new ColorMatrixColorFilter(scaleMatrix);
    surfaceView.getOverlayManager().getTilesOverlay().setColorFilter(filter);
}
Run Code Online (Sandbox Code Playgroud)

使用自定义 ColorMatrixColorFilter 和 Mapnik 的夜间模式示例

注意:如果您使用过ScaleBarOverlay,您可能需要更改其颜色以使其在夜间模式下更明显。

为了帮助找到正确的矩阵,我使用这个方便的工具colormatrix-viewer