首页 文章

在android中绘制不同颜色的折线

提问于
浏览
0

我在'm creating an app which tracks user'的位置并在调用onLocationChange(位置位置)时绘制折线 . 折线颜色取决于加速度计 . 我知道如何使用折线,但PolyLineOptions对象只能是单色 . 我读到它可以通过使用GroundOverlay来实现,但我不知道如何使用它的坐标) . 有人可以帮助我,给出一些示例链接吗?请注意,当用户移动时,应该动态添加每条折线(或位图线) . 我做过研究,但我发现的只是Drawing multi color PolyLines on Maps V2并没有解释一切 . 感谢帮助!

1 回答

  • 0

    “在每个位置改变,它的颜色取决于加速度计的变化,我想实时做到”

    请注意,无论您是添加折线还是使用您想要的GroundOverlay方法,这都可能会使您的应用显得迟钝或悬挂 .

    无论如何,这就是我用“GroundOverlay方法”做到的方法

    1.)创建代表每个速度范围的n个图像 . 例如"slow"(0-1000)的红色图像,1001-2000的绿色图像等...)

    2.)按照您希望的方式定义一个数组,该数组将保存这些图像的资源ID . 例如:

    int[] speedImageBucket = {R.drawable.slow,R.drawable.medium,R.drawable.fast};
    

    3.)因此,每次locationChange触发,计算要添加的图像,并作为叠加添加到 Map . 例:

    ....
    float speed = loc.getSpeed(); // loc is the current location object passed in
    int speedBucket = (int) (speed / 1000); // 1000 is the linear increment of your ranges for example
    
     //TODO: handle if speedBucket (array index) is out of range
    
     GoogleMap map = ...; // get a map.
     BitmapDescriptor image = BitmapDescriptorFactory.fromResource(speedImageBucket[speedBucket ]);; // get an image.
    
     // Adds a ground overlay with 50% transparency.
     GroundOverlay groundOverlay = map.addGroundOverlay(new GroundOverlayOptions()
     .image(image)
     .position(loc,1)
     .transparency(0.5));
    

相关问题