问题
I needed the map pin drop animation for my android application. Doing a lot of research, i found nothing useful . So i created a small hack. Just posting the code so that other can use it and modify it accordingly.
As Translate animation in android only works on Views and overlays are not views, I created a small hack which drops the views at the same geo location via translate animation and then remove the view to show overlays. This way user thinks the map overlays are dropping from the top even though they are not.
Here is the code:
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);
}
}
When you need to show this animation - run a new thread where user can see the dropping of image views and then remove it after the animation is done.
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();
}
Please feel free to modify it , and suggest any changes. Ofcourse if you have tons of overlays , it will create tons of imageviews too causing memory issues but it works great for small set of overlays. I am using it for atleast 100 overlays and it works great.
来源:https://stackoverflow.com/questions/10607042/tutorial-android-map-pin-drop-animation-working-right