Android的使用颜色为谷歌地图标记 [英] Android use HEX color for google maps marker

查看:336
本文介绍了Android的使用颜色为谷歌地图标记的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用一个自定义的十六进制颜色谷歌地图的标记,但我看到你只能使用色调颜色0-360和10 predefined的。有什么办法可以改变的标志颜色是十六进制的,或至少转换成十六进制值色相,所以我可以使用呢?我已经知道如何设置色调颜色的标记,而不是一个十六进制的。

I want to use a custom hex color for google maps markers, but I saw you can only use hue colors from 0-360 and the 10 predefined ones. Is there any way you can change the marker color to be a hex one, or at least convert the hex value to hue so I can use that instead? I already know how to set a hue color to the marker, but not a hex one.

推荐答案

以下code给出了一个例子。它是基于使用JavaScript这个伟大的答案: http://stackoverflow.com/a/3732187/1207156

The following code shows an example. It's based on this great answer that uses JavaScript: http://stackoverflow.com/a/3732187/1207156

public class Convert {

    public static class Hsl {
        public double h, s, l;

        public Hsl(double h, double s, double l) {
            this.h = h;
            this.s = s;
            this.l = l;
        }
    }

    public static void main(String[] args) {
        String color = "#c7d92c"; // A nice shade of green.
        int r = Integer.parseInt(color.substring(1, 3), 16); // Grab the hex representation of red (chars 1-2) and convert to decimal (base 10).
        int g = Integer.parseInt(color.substring(3, 5), 16);
        int b = Integer.parseInt(color.substring(5, 7), 16);    

        double hue = rgbToHsl(r, g, b).h * 360;

        System.out.println("The hue value is " + hue);
    }

    private static Hsl rgbToHsl(double r, double g, double b) {
        r /= 255d; g /= 255d; b /= 255d;

        double max = Math.max(Math.max(r, g), b), min = Math.min(Math.min(r, g), b);
        double h, s, l = (max + min) / 2;

        if (max == min) {
            h = s = 0; // achromatic
        } else {
            double d = max - min;
            s = l > 0.5 ? d / (2 - max - min) : d / (max + min);

            if (max == r) h = (g - b) / d + (g < b ? 6 : 0);
            else if (max == g) h = (b - r) / d + 2;
            else h = (r - g) / d + 4; // if (max == b)

            h /= 6;
        }

        return new Hsl(h, s, l);
    }

}

这篇关于Android的使用颜色为谷歌地图标记的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆