将 Gif 图像设置为标记 android
Posted
技术标签:
【中文标题】将 Gif 图像设置为标记 android【英文标题】:set Gif Image As a marker android 【发布时间】:2018-11-08 21:48:28 【问题描述】:有没有可能 谷歌地图。在我的项目中,我想将 gif 图像设置为标记
if (bmp != null)
System.out.println("----marker-set----------" + base64);
marker = googleMap.addMarker(new MarkerOptions()
.position(new LatLng(MyCurrent_lat, MyCurrent_long))
.icon(BitmapDescriptorFactory.fromBitmap(bmp)));
googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(MyCurrent_lat, MyCurrent_long),
17));
【问题讨论】:
【参考方案1】:来自文档:
https://developers.google.com/maps/documentation/android-sdk/marker
https://developers.google.com/android/reference/com/google/android/gms/maps/model/Marker
图标 为标记显示的位图。如果图标离开 未设置,则显示默认图标。您可以指定一个替代方案 使用 defaultMarker(float) 为默认图标着色。
您不能将 GIF 图像用作标记,因为该图标是静态的。并且您无法在运行时更改图像。但是,您可以使用代码更改图像(通过在事件触发器、计时器等上完全删除标记,然后使用不同的图像再次添加)。
【讨论】:
【参考方案2】:您不能将Gif
设置为地图标记。
google maps android api v2 中的标记图标是静态图像,一旦创建标记,您将无法更改图标。因此,如果您尝试使用 GIF 图像包含任何动画,它将不受支持,并且只会将 GIF 中的单个图像显示为图标。
参考:Add GIF image to marker of Google Map Api v2
相反,默认地图标记可以使用具有所需图像的自定义标记进行更改,如下所示:
创建带有图像支架的自定义标记布局 (ImageView
)
初始化地图标记布局
View marker = ((LayoutInflater)getSystemService(getApplicationContext().LAYOUT_INFLATER_SERVICE)).inflate(R.layout.custom_marker_layout, null);
ImageView img = marker.findViewById(R.id.img);
将imageView
中的图片设置为:
if(bmp!=null)
img.setImageBitmap(bmp);
将其设置为地图:
Marker customMarker = mMap.addMarker(new MarkerOptions()
.position(new LatLng(lati, longi))
.title(title)
.icon(BitmapDescriptorFactory.fromBitmap(createDrawableFromView(this, marker))));
其中createDrawableFromView
是一个方法,其定义为:
public static Bitmap createDrawableFromView(Context context, View view)
DisplayMetrics displayMetrics = new DisplayMetrics();
((Activity)context).getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
view.setLayoutParams(new WindowManager.LayoutParams(ActionBar.LayoutParams.WRAP_CONTENT, ActionBar.LayoutParams.WRAP_CONTENT));
view.measure(displayMetrics.widthPixels, displayMetrics.heightPixels);
view.layout(0, 0, displayMetrics.widthPixels, displayMetrics.heightPixels);
view.buildDrawingCache();
Bitmap bitmap = Bitmap.createBitmap(view.getMeasuredWidth(), view.getMeasuredHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
view.draw(canvas);
return bitmap;
希望它对你有用。
【讨论】:
我对你的答案投了反对票,因为如果使用从自定义标记视图布局构建的位图,它仍然是静态图像而不是动画 gif。所以你的回答会浪费其他人的时间以上是关于将 Gif 图像设置为标记 android的主要内容,如果未能解决你的问题,请参考以下文章