(原创)android加载GIF图片到地图(或者其他图层)中
最近在项目中遇到需要加载动态图片(类似GIF)到地图图层中,Google许久之后找到答案:
1.将GIF图片分解成多个图层,如location_anim0.png,location_anim1.png,location_anim2.png,location_anim3.png...
然后将上述文件扔进drawable文件夹;新建一个location_anim.xml, 同样放置于drawable文件夹当中,内容如下:
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
android:oneshot="false" >
<item
android:drawable="@drawable/location_anim0"
android:duration="300"/>
<item
android:drawable="@drawable/location_anim1"
android:duration="300"/>
<item
android:drawable="@drawable/location_anim2"
android:duration="300"/>
<item
android:drawable="@drawable/location_anim3"
android:duration="300"/>
</animation-list>
2.做好上面的准备工作后,接下来写一个生成动态view并且将其加入到Mapview当中的方法:
// 将GIF图加入到map当中
public static void addAnimationToMap(MapView map, int animationResourceId,
GeoPoint geoPoint) {
final ImageView view = new ImageView(map.getContext());
view.setImageResource(animationResourceId);
// Post to start animation because it doesn't start if start() method is
// called in activity OnCreate method.
view.post(new Runnable() {
@Override
public void run() {
AnimationDrawable animationDrawable = (AnimationDrawable) view
.getDrawable();
animationDrawable.start();
}
});
//GeoPoint为经纬度
MapView.LayoutParams layoutParams = new MapView.LayoutParams(
MapView.LayoutParams.WRAP_CONTENT,
MapView.LayoutParams.WRAP_CONTENT, geoPoint, 10, 10,
MapView.LayoutParams.CENTER);
view.setLayoutParams(layoutParams);
view.setId(100);
map.addView(view);
}
3.最后在需要添加GIF图的Activity中调用此工具类,就大功告成了:
LocationUtils.addAnimationToMap(mMapView,
R.drawable.location_anim, gPoint);
如果需要动态的删除这个GIF图,只需要findViewById(100),然后removeView就OK。
这个方案适合用来制作比较动感的地图实时定位点。
OVER~