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

Android Google Maps: Custom marker for gradient color

我有一个简单的 Android 应用程序,其中有一个显示当前默认标记的 Google 地图。好的是我可以简单地更改标记的 color/hue,在我的情况下,这取决于标记 "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;
}

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

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

尝试添加可绘制对象并通过 canvas 绘制它,例如:

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