繁体   English   中英

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

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

我有一个带有Google Map的简单Android应用,可显示当前默认标记。 不错的是,我可以简单地更改标记的颜色/色相,在我的情况下,这取决于标记“失效”的时间

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;
}

这工作得很好。 但是,现在我想使用自定义标记来支持不同类型的标记。 虽然我设法使用其他PNG可绘制对象更改了标记形状,但我没有成功对颜色进行任何更改。 可绘制对象,位图,画布,位图描述符(不同类型)的整个概念……我无法理解。

我已经尝试过各种在网上找到的东西,但没有任何效果。 我得到了转换错误,空指针异常,或者没有错误,但是没有影响设置颜色。

尝试添加drawable并通过画布绘制它,例如:

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

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