11

我需要我的 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 个叠加层,效果很好。

4

0 回答 0