Java 如何在android中添加带边界的彩色多段线

Java 如何在android中添加带边界的彩色多段线,java,android,dictionary,polyline,boundary,Java,Android,Dictionary,Polyline,Boundary,我想画有彩色边界的多段线,如图中所示,我研究过它,并创建了谷歌的文档,但我无法理解它 请帮助我用android和java语言绘制这幅图。使用我已实现的单折线,您可以使用以下内容扩展此概念: public class TestActivityMAp extends FragmentActivity implements OnMapReadyCallback, GoogleMap.OnPolylineClickListener, GoogleMap.OnPolygonClic

我想画有彩色边界的多段线,如图中所示,我研究过它,并创建了谷歌的文档,但我无法理解它

请帮助我用android和java语言绘制这幅图。

使用我已实现的单折线,您可以使用以下内容扩展此概念:

public class TestActivityMAp extends FragmentActivity implements
    OnMapReadyCallback,
    GoogleMap.OnPolylineClickListener,
    GoogleMap.OnPolygonClickListener  {

private GoogleMap mMap;


private static final int COLOR_BLACK_ARGB = 0xff000000;
private static final int POLYLINE_STROKE_WIDTH_PX = 12;







private static final int COLOR_WHITE_ARGB = 0xffffffff;
private static final int COLOR_GREEN_ARGB = 0xff388E3C;
private static final int COLOR_PURPLE_ARGB = 0xff81C784;
private static final int COLOR_ORANGE_ARGB = 0xffF57F17;
private static final int COLOR_BLUE_ARGB = 0xffF9A825;

private static final int POLYGON_STROKE_WIDTH_PX = 8;
private static final int PATTERN_DASH_LENGTH_PX = 20;
private static final int PATTERN_GAP_LENGTH_PX = 20;
private static final PatternItem DOT = new Dot();
private static final PatternItem DASH = new Dash(PATTERN_DASH_LENGTH_PX);
private static final PatternItem GAP = new Gap(PATTERN_GAP_LENGTH_PX);
//
// Create a stroke pattern of a gap followed by a dot.
private static final List<PatternItem> PATTERN_POLYLINE_DOTTED = 
Arrays.asList(GAP, DOT);


// Create a stroke pattern of a gap followed by a dash.
private static final List<PatternItem> PATTERN_POLYGON_ALPHA = 
Arrays.asList(GAP, DASH);

// Create a stroke pattern of a dot followed by a gap, a dash, and another 
gap.
private static final List<PatternItem> PATTERN_POLYGON_BETA =
        Arrays.asList(DOT, GAP, DASH, GAP);

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_test_map);
    // Obtain the SupportMapFragment and get notified when the map is ready to 
    be used.
    SupportMapFragment mapFragment = (SupportMapFragment) 
getSupportFragmentManager()
            .findFragmentById(R.id.map);
    mapFragment.getMapAsync(this);
}


/**
 * Manipulates the map once available.
 * This callback is triggered when the map is ready to be used.
 * This is where we can add markers or lines, add listeners or move the 
camera. In this case,
 * we just add a marker near Sydney, Australia.
 * If Google Play services is not installed on the device, the user will be 
prompted to install
 * it inside the SupportMapFragment. This method will only be triggered once 
the user has
 * installed Google Play services and returned to the app.
 */
@Override
public void onMapReady(GoogleMap googleMap) {
    mMap = googleMap;
//
// Add a marker in Sydney and move the camera
//        LatLng sydney = new LatLng(-34, 151);
//        mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in 
Sydney"));
//        mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));



    Polyline polyline2 = googleMap.addPolyline(new PolylineOptions()
            .clickable(true)
            .add(
                    new LatLng(-27.457, 153.040),
                    new LatLng(-33.852, 151.211),
                    new LatLng(-37.813, 144.962),
                    new LatLng(-34.928, 138.599),
                    new LatLng(-25.928, 140.599)));
// Store a data object with the polyline, used here to indicate an arbitrary 
type.
    polyline2.setTag("A");

    Polygon polygon1 = googleMap.addPolygon(new PolygonOptions()
            .clickable(true)
            .add(
                    new LatLng(-27.457, 153.040),
                    new LatLng(-33.852, 151.211),
                    new LatLng(-37.813, 144.962),
                    new LatLng(-34.928, 138.599),
                    new LatLng(-25.928, 140.599)));
// Store a data object with the polygon, used here to indicate an arbitrary 
type.
    polygon1.setTag("alpha");

    stylePolygon(polygon1);

    // Position the map's camera near Alice Springs in the center of 
Australia,
    // and set the zoom factor so most of Australia shows on the screen.
    googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(-23.684, 
133.903), 4));

    // Set listeners for click events.
    googleMap.setOnPolylineClickListener(this);
    googleMap.setOnPolygonClickListener(this);
}


private void stylePolygon(Polygon polygon) {
    String type = "";
    // Get the data object stored with the polygon.
    if (polygon.getTag() != null) {
        type = polygon.getTag().toString();
    }

    List<PatternItem> pattern = null;
    int strokeColor = COLOR_BLACK_ARGB;
    int fillColor = COLOR_WHITE_ARGB;

    switch (type) {
        // If no type is given, allow the API to use the default.
        case "alpha":
            // Apply a stroke pattern to render a dashed line, and define 
colors.
            pattern = PATTERN_POLYGON_ALPHA;
            strokeColor = COLOR_GREEN_ARGB;
            fillColor = COLOR_PURPLE_ARGB;
            break;
        case "beta":
            // Apply a stroke pattern to render a line of dots and dashes, and 
define colors.
            pattern = PATTERN_POLYGON_BETA;
            strokeColor = COLOR_ORANGE_ARGB;
            fillColor = COLOR_BLUE_ARGB;
            break;
    }

    polygon.setStrokePattern(pattern);
    polygon.setStrokeWidth(POLYGON_STROKE_WIDTH_PX);
    polygon.setStrokeColor(strokeColor);
    polygon.setFillColor(fillColor);
}


private void stylePolyline(Polyline polyline) {
    String type = "";
    // Get the data object stored with the polyline.
    if (polyline.getTag() != null) {
        type = polyline.getTag().toString();
    }

    switch (type) {
        // If no type is given, allow the API to use the default.
        case "A":
            // Use a custom bitmap as the cap at the start of the line.
            polyline.setStartCap(
                    new CustomCap(

BitmapDescriptorFactory.fromResource(R.drawable.ic_arrow_forward_white_24dp), 
10));
            break;
        case "B":
            // Use a round cap at the start of the line.
            polyline.setStartCap(new RoundCap());
            break;
    }

    polyline.setEndCap(new RoundCap());
    polyline.setWidth(POLYLINE_STROKE_WIDTH_PX);
    polyline.setColor(COLOR_BLACK_ARGB);
    polyline.setJointType(JointType.ROUND);
}




@Override
public void onPolygonClick(Polygon polygon) {

}

@Override
public void onPolylineClick(Polyline polyline) {
    // Flip from solid stroke to dotted stroke pattern.
    if ((polyline.getPattern() == null) || 
(!polyline.getPattern().contains(DOT))) {
        polyline.setPattern(PATTERN_POLYLINE_DOTTED);
    } else {
        // The default pattern is a solid stroke.
        polyline.setPattern(null);
    }

    Toast.makeText(this, "Route type " + polyline.getTag().toString(),
            Toast.LENGTH_SHORT).show();
}
}
公共类TestActivityMAp扩展了FragmentActivity实现
四月一日,
GoogleMap.OnPolylineClickListener,
GoogleMap.OnPolygonClickListener{
私有谷歌地图;
私有静态最终整数颜色\u黑色\u ARGB=0xff000000;
专用静态最终整型多段线\笔划\宽度\像素=12;
私有静态最终整数颜色\u白色\u ARGB=0xffffffff;
私有静态最终整数颜色\绿色\ ARGB=0xff388E3C;
私有静态最终整数颜色\紫色\ ARGB=0xff81C784;
专用静态最终整数颜色\u橙色\u ARGB=0xffF57F17;
专用静态最终整型颜色\u蓝色\u ARGB=0xffF9A825;
私有静态最终整数多边形_笔划_宽度_PX=8;
私有静态最终整数模式\u破折号\u长度\u PX=20;
专用静态最终整数模式_间距_长度_PX=20;
私有静态最终模式项目点=新点();
专用静态最终模式项目破折号=新破折号(模式破折号长度PX);
专用静态最终图案项目间隙=新间隙(图案间隙长度x);
//
//创建一个间距后跟一个点的笔划图案。
私有静态最终列表模式\多段线\虚线=
数组.asList(间隙、点);
//创建一个带有破折号的间隙的笔划图案。
私有静态最终列表模式\u多边形\u ALPHA=
数组.asList(间隙、破折号);
//创建一个点的笔划图案,后跟一个间隙、一个破折号和另一个破折号
缺口
私有静态最终列表模式\u多边形\u测试版=
数组.asList(点、间隙、破折号、间隙);
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity\u test\u map);
//获取SupportMapFragment,并在地图准备就绪时收到通知
被使用。
SupportMapFragment mapFragment=(SupportMapFragment)
getSupportFragmentManager()
.findFragmentById(R.id.map);
getMapAsync(这个);
}
/**
*一旦可用,就可以操纵贴图。
*当映射准备好使用时,将触发此回调。
*这是我们可以添加标记或线条、添加侦听器或移动
摄像机,在这种情况下,
*我们只是在澳大利亚悉尼附近加了一个标记。
*如果设备上未安装Google Play服务,则用户将
提示安装
*它位于SupportMapFragment中。此方法将只触发一次
用户有
*已安装Google Play服务并返回应用程序。
*/
@凌驾
4月1日公开作废(谷歌地图谷歌地图){
mMap=谷歌地图;
//
//在Sydney添加一个标记并移动相机
//悉尼LatLng=新LatLng(-34151);
//mMap.addMarker(新MarkerOptions().position(sydney).title(“中的标记
悉尼),;
//mMap.moveCamera(CameraUpdateFactory.newLatLng(悉尼));
Polyline polyline2=googleMap.addPolyline(新的PolylineOptions()
.可点击(真)
.添加(
新车床(-27.457153.040),
新车床(-33.852151.211),
新车床(-37.813144.962),
新车床(-34.928138.599),
新车床(跌25.928140.599),;
//使用多段线存储数据对象,此处用于指示任意
类型。
多段线2.集合标记(“A”);
Polygon polygon1=googleMap.addPolygon(新polygonooptions()
.可点击(真)
.添加(
新车床(-27.457153.040),
新车床(-33.852151.211),
新车床(-37.813144.962),
新车床(-34.928138.599),
新车床(跌25.928140.599),;
//使用多边形存储数据对象,此处用于指示任意多边形
类型。
polygon1.setTag(“α”);
花柱多边形(polygon1);
//将地图的摄影机放置在地图中心的Alice Springs附近
澳大利亚
//并设置缩放因子,使澳大利亚的大部分地区显示在屏幕上。
谷歌地图。移动摄像头(摄像头更新工厂。新车床空间(新车床(-23.684,
133.903), 4));
//为单击事件设置侦听器。
setOnPolylineClickListener(这个);
setOnPolygonClickListener(this);
}
专用空心样式多边形(多边形){
字符串类型=”;
//获取与多边形一起存储的数据对象。
if(polygon.getTag()!=null){
type=polygon.getTag().toString();
}
列表模式=空;
int strokeColor=COLOR\U BLACK\U ARGB;
int fillColor=COLOR\u WHITE\u ARGB;
开关(类型){
//如果未指定类型,则允许API使用默认类型。
案例“alpha”:
//应用笔划图案以渲染虚线,然后定义
颜色。
pattern=pattern_POLYGON_ALPHA;
strokeColor=颜色\绿色\ ARGB;
fillColor=颜色\紫色\ ARGB;
打破
案例“beta”:
//应用笔划图案以渲染一行点和虚线,以及
定义颜色。
图案=图案\多边形\β;
strokeColor=颜色\橙色\ ARGB;
fillColor=颜色\蓝色\ ARGB;
打破
}
多边形。设置行程模式(模式);
多边形。设置行程宽度(多边形_笔划_宽度_PX);
多边形。设置strokeColor(strokeColor);
polygon.setFillColor(fillColor);
}
专用空心样式多段线(多段线多段线){
字符串类型=”;
//获取与多段线一起存储的数据对象。
if(polyline.getTag()!=null){
type=polyline.getTag().toString();
}
开关(类型){
//如果未指定类型,则允许API使用默认类型。
案例“A”:
//在行首使用自定义位图作为封口。
polyline.setStartCap(
新海关帽(
位图描述符