我需要我的Android应用程序的地图引脚放置动画。做了很多研究,我没有发现任何有用的东西。所以我创造了一个小黑客。只需发布代码,以便其他人可以使用它并相应地修改它。
由于Android中的翻译动画仅适用于视图而叠加不是视图,我创建了一个小的黑客,通过翻译动画将视图放在同一个地理位置,然后删除视图以显示叠加。这样,用户认为地图叠加层从顶部开始掉落,即使它们不是。
以下是代码:
private void translateAnimation(MapView mapView,ArrayList<GeoPoint> geoPointsList,Context context) {
Point dropPoint = new Point();
Projection projection = mapView.getProjection(); /*
* Getting the map projections to calculate
* screen points and geopoints
*/
GeoPoint mGeoPoint = projection.fromPixels(0, 0);
/* Map View screen parameters */
MapView.LayoutParams screenParameters = new MapView.LayoutParams(
MapView.LayoutParams.WRAP_CONTENT, MapView.LayoutParams.WRAP_CONTENT, mGeoPoint, 0,
0, MapView.LayoutParams.LEFT | MapView.LayoutParams.BOTTOM);
for (GeoPoint geoPoint : geoPointsList) {
projection.toPixels(geoPoint, dropPoint); /*
* As Overlays are not a subclass of views so
* view animation cannot work
*/
ImageView imageView = new ImageView(context); /*
* Therefore creating an imageview for
* every overlay point apply the
* transition animation and then removing
* the imageview
*/
imageView.setImageResource(R.drawable.map_pin_focused);
/* Translation animation to show Falling Pins effect */
Bitmap markerImage = BitmapFactory.decodeResource(context.getResources(),
R.drawable.map_pin_focused);
TranslateAnimation drop = new TranslateAnimation(dropPoint.x
- (markerImage.getWidth() / 2), dropPoint.x - (markerImage.getWidth() / 2), 0,
dropPoint.y + (markerImage.getHeight() / 2));
drop.setDuration(600);
drop.setFillAfter(true);
drop.setStartOffset(100);
imageView.startAnimation(drop);
mapView.addView(imageView, screenParameters);
// imageView.setAnimation(drop);
}
}
当您需要显示此动画时 - 运行一个新线程,用户可以看到删除图像视图,然后在动画完成后将其删除。
private void startTranslationAnimation() {
translationAnimation(mapView, geoPointsList, this);
new Thread(new Runnable() {
public void run() {
try {
Thread.sleep(1000); /*
* Making thread sleep for 1 sec so that animation can be
* seen before updating the overlays
*/
} catch (InterruptedException e) {
e.printStackTrace();
}
mapView.post(new Runnable() {
public void run() {
mapView.removeAllViews(); /* Removing all the image Views */
mapOverlays.add(itemOverlay); /*
* Adding the overlay items on map overlay
* list
*/
mapView.invalidate(); /* Refreshing the map overlays */
}
});
}
}).start();
}
请随时修改,并建议任何更改。当然,如果你有大量的叠加层,它会产生大量的图像视图,也会导致内存问题,但它适用于少量叠加层。我使用它至少100个叠加,它很好用。