我可以膨胀一个视图来替换 mapbox android 中的图标吗?
Can I inflate a view replacing the icon in mapbox android?
在 mapbox 中添加标记时,可以为标记添加自定义图标。我想知道我们是否可以膨胀视图(R.layout 文件)而不是分配可绘制图标。
代码如下:-
public void onMapReady(MapboxMap mapboxMap) {
IconFactory iconFactory=IconFactory.getInstance(context);
for(int i=0;i<coordinates.size();i++){
mapboxMap.addMarker(new MarkerOptions()
.position(new LatLng(lat,longt))
.icon(iconFactory.fromResource(R.drawable.ic_location_green))
//can we inflate a view here instead of assigning a drawable image?
}
}
我认为不可能
您可以做的是在运行时绘制自定义图标:
画在Canvas
在运行时生成Drawable
(而不是创建xml,你可以创建一个对象。所以如果你要键入<shape>
,你可以替换它在 Java)
中有新的 Shape();
生成一个视图并复制它的位图(How to convert Views to bitmaps?)这个选项只会提供它的外观——点击监听器之类的东西将不起作用 , 因此我看不出选择这个选项的理由
这可以使用以下实用程序 class:
/**
* Utility class to generate Bitmaps for Symbol.
* <p>
* Bitmaps can be added to the map with {@link com.mapbox.mapboxsdk.maps.MapboxMap#addImage(String, Bitmap)}
* </p>
*/
private static class SymbolGenerator {
/**
* Generate a Bitmap from an Android SDK View.
*
* @param view the View to be drawn to a Bitmap
* @return the generated bitmap
*/
public static Bitmap generate(@NonNull View view) {
int measureSpec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
view.measure(measureSpec, measureSpec);
int measuredWidth = view.getMeasuredWidth();
int measuredHeight = view.getMeasuredHeight();
view.layout(0, 0, measuredWidth, measuredHeight);
Bitmap bitmap = Bitmap.createBitmap(measuredWidth, measuredHeight, Bitmap.Config.ARGB_8888);
bitmap.eraseColor(Color.TRANSPARENT);
Canvas canvas = new Canvas(bitmap);
view.draw(canvas);
return bitmap;
}
}
可以找到集成此代码的完整示例 here. Note that this code can be executed on a background thread, so you don't need to block the main thread for it. While we don't expose a binary atm we are looking into creating a little plugin around this code. The feature request for it can be found here。
在 mapbox 中添加标记时,可以为标记添加自定义图标。我想知道我们是否可以膨胀视图(R.layout 文件)而不是分配可绘制图标。
代码如下:-
public void onMapReady(MapboxMap mapboxMap) {
IconFactory iconFactory=IconFactory.getInstance(context);
for(int i=0;i<coordinates.size();i++){
mapboxMap.addMarker(new MarkerOptions()
.position(new LatLng(lat,longt))
.icon(iconFactory.fromResource(R.drawable.ic_location_green))
//can we inflate a view here instead of assigning a drawable image?
}
}
我认为不可能
您可以做的是在运行时绘制自定义图标:
画在
Canvas
在运行时生成
Drawable
(而不是创建xml,你可以创建一个对象。所以如果你要键入<shape>
,你可以替换它在 Java) 中有新的 生成一个视图并复制它的位图(How to convert Views to bitmaps?)这个选项只会提供它的外观——点击监听器之类的东西将不起作用 , 因此我看不出选择这个选项的理由
Shape();
这可以使用以下实用程序 class:
/**
* Utility class to generate Bitmaps for Symbol.
* <p>
* Bitmaps can be added to the map with {@link com.mapbox.mapboxsdk.maps.MapboxMap#addImage(String, Bitmap)}
* </p>
*/
private static class SymbolGenerator {
/**
* Generate a Bitmap from an Android SDK View.
*
* @param view the View to be drawn to a Bitmap
* @return the generated bitmap
*/
public static Bitmap generate(@NonNull View view) {
int measureSpec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
view.measure(measureSpec, measureSpec);
int measuredWidth = view.getMeasuredWidth();
int measuredHeight = view.getMeasuredHeight();
view.layout(0, 0, measuredWidth, measuredHeight);
Bitmap bitmap = Bitmap.createBitmap(measuredWidth, measuredHeight, Bitmap.Config.ARGB_8888);
bitmap.eraseColor(Color.TRANSPARENT);
Canvas canvas = new Canvas(bitmap);
view.draw(canvas);
return bitmap;
}
}
可以找到集成此代码的完整示例 here. Note that this code can be executed on a background thread, so you don't need to block the main thread for it. While we don't expose a binary atm we are looking into creating a little plugin around this code. The feature request for it can be found here。