简体   繁体   English

Android Google Maps:渐变颜色的自定义标记

[英]Android Google Maps: Custom marker for gradient color

I have a simple Android app with a Google Map that shows currently default markers. 我有一个带有Google Map的简单Android应用,可显示当前默认标记。 The nice thing is that I can simple change the color/hue of the marker, which in my case depend on the time a marker will "expire" 不错的是,我可以简单地更改标记的颜色/色相,在我的情况下,这取决于标记“失效”的时间

bitmapDescriptor = BitmapDescriptorFactory.defaultMarker(this.calculateMarkerHue(msg));

MarkerOptions markerOptions = new MarkerOptions()
                .position(new LatLng(...))
                .icon(bitmapDescriptor)
                .title("Title")
                .snippet("Snippet");


private float calculateMarkerHue(Message msg) {
    float hue = <calculate some value 0.0..360.0 depending on msg>
    return hue;
}

This works perfectly fine. 这工作得很好。 However, now I would like to use custom markers to support different types of markers. 但是,现在我想使用自定义标记来支持不同类型的标记。 While I managed to change the marker shape using other PNG drawables, I didn't succeed to make any changes to the color. 虽然我设法使用其他PNG可绘制对象更改了标记形状,但我没有成功对颜色进行任何更改。 The whole notions of (different types of) drawables, bitmaps, canvases, bitmap descriptors...I cannot get my head around. 可绘制对象,位图,画布,位图描述符(不同类型)的整个概念……我无法理解。

I've tried all kinds of stuff I found online but nothing worked. 我已经尝试过各种在网上找到的东西,但没有任何效果。 Either I got casting errors, null pointer exceptions, or no errors but not effects regarding setting the color. 我得到了转换错误,空指针异常,或者没有错误,但是没有影响设置颜色。

Try to add drawable and draw it via canvas something like: 尝试添加drawable并通过画布绘制它,例如:

markerOptions.icon(bitmapFromDrawable(getActivity(), R.drawable.your_gradient));

The bitmapFromDrawable should be like: bitmapFromDrawable应该类似于:

 private BitmapDescriptor bitmapFromDrawable(Context context, int vectorResId) {
        Drawable vectorDrawable = ContextCompat.getDrawable(context, vectorResId);
        vectorDrawable.setBounds(0, 0, vectorDrawable.getIntrinsicWidth(), vectorDrawable.getIntrinsicHeight());
        Bitmap bitmap = Bitmap.createBitmap(vectorDrawable.getIntrinsicWidth(), vectorDrawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(bitmap);
        vectorDrawable.draw(canvas);
        return BitmapDescriptorFactory.fromBitmap(bitmap);
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM