You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何为Jetpack Compose谷歌地图的BitmapDescriptor添加颜色滤镜?

在Jetpack Compose谷歌地图中为Polyline的BitmapDescriptor匹配线条颜色

你可以沿用View体系里的核心逻辑,将Drawable添加颜色滤镜后转成BitmapDescriptor,适配Jetpack Compose的写法即可,具体实现如下:

1. 编写生成带颜色的BitmapDescriptor工具函数

import android.content.Context
import android.graphics.Bitmap
import android.graphics.Canvas
import android.graphics.PorterDuff
import android.graphics.drawable.Drawable
import androidx.core.content.ContextCompat
import com.google.android.gms.maps.model.BitmapDescriptor
import com.google.android.gms.maps.model.BitmapDescriptorFactory

fun createColoredBitmapDescriptor(context: Context, resId: Int, color: Int): BitmapDescriptor {
    // 获取Drawable并mutate,避免影响全局实例
    val drawable = ContextCompat.getDrawable(context, resId)?.mutate() 
        ?: return BitmapDescriptorFactory.defaultMarker()
    
    drawable.setBounds(0, 0, drawable.intrinsicWidth, drawable.intrinsicHeight)
    // 给Drawable添加颜色滤镜,使用MULTIPLY模式和目标颜色混合
    drawable.setColorFilter(color, PorterDuff.Mode.MULTIPLY)
    
    // 将Drawable绘制到Bitmap上
    val bitmap = Bitmap.createBitmap(
        drawable.intrinsicWidth, 
        drawable.intrinsicHeight, 
        Bitmap.Config.ARGB_8888
    )
    val canvas = Canvas(bitmap)
    drawable.draw(canvas)
    
    return BitmapDescriptorFactory.fromBitmap(bitmap)
}

2. 在Compose中使用该函数生成匹配颜色的BitmapDescriptor

import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.runtime.Composable
import androidx.compose.runtime.derivedStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalContext
import com.google.android.gms.maps.model.LatLng
import com.google.maps.android.compose.GoogleMap
import com.google.maps.android.compose.Polyline
import com.google.maps.android.compose.PolylineDefaults
import com.google.maps.android.compose.ui.MapUiSettings

@Composable
fun MapWithColoredPolyline(polylinePoints: List<LatLng>) {
    val polylineColor = Color(0xff0e71b8)
    val context = LocalContext.current
    
    // 用remember结合derivedStateOf,只有颜色变化时才重新生成BitmapDescriptor
    val arrowBitmapDescriptor by remember(polylineColor) {
        derivedStateOf {
            createColoredBitmapDescriptor(
                context = context,
                resId = R.drawable.arrow,
                color = polylineColor.toArgb() // 将Compose Color转为Int类型的颜色值
            )
        }
    }

    GoogleMap(modifier = Modifier.fillMaxSize()) {
        Polyline(
            points = polylinePoints,
            endCap = PolylineDefaults.CustomCap(arrowBitmapDescriptor, 30f),
            color = polylineColor
        )
    }
}

关键说明

  • 调用Drawable.mutate()是为了创建当前Drawable的独立副本,防止修改滤镜后影响其他地方使用同一张资源的Drawable。
  • 使用remember(polylineColor)作为依赖项,确保只有当线条颜色改变时才重新生成BitmapDescriptor,优化性能。
  • 通过Color.toArgb()将Compose的Color类型转换为Android原生的Int颜色值,适配Drawable的颜色滤镜接口。

内容的提问来源于stack exchange,提问作者NullPointerException

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.14 12:07:17