连续弹跳标记:android映射v2

时间:2013-12-18 07:17:13

标签: android google-maps google-maps-markers

搜索了如何在android maps v2中弹出一个标记后,我发现只有一种方法可以找到它并且工作正常。但只有在标记点击时,我需要的是使标记连续弹跳,就像在JavaScript地图v3中一样。 我怎样才能做到这一点?

以下代码:

map.setOnMarkerClickListener(new OnMarkerClickListener() {

            @Override
            public boolean onMarkerClick(Marker arg0) {
                arg0.showInfoWindow();
                bounceMarker(arg0);
                return true;
            }
        });

private void bounceMarker(final Marker marker){
        final Handler handler = new Handler();

        final long startTime = SystemClock.uptimeMillis();
        final long duration = 1500;

        Projection proj = busmap.getProjection();
        final LatLng markerLatLng = marker.getPosition();
        Point startPoint = proj.toScreenLocation(markerLatLng);
        startPoint.offset(0, -100);
        final LatLng startLatLng = proj.fromScreenLocation(startPoint);

        final Interpolator interpolator = new BounceInterpolator();

        handler.post(new Runnable() {
            @Override
            public void run() {
                long elapsed = SystemClock.uptimeMillis() - startTime;
                float t = interpolator.getInterpolation((float) elapsed / duration);
                double lng = t * markerLatLng.longitude + (1 - t) * startLatLng.longitude;
                double lat = t * markerLatLng.latitude + (1 - t) * startLatLng.latitude;
                marker.setPosition(new LatLng(lat, lng));

                if (t < 1.0) {
                    // Post again 16ms later.
                    handler.postDelayed(this, 16);
                }
            }
        });

}

2 个答案:

答案 0 :(得分:8)

这是实现这一目标的另一种方式。我从M D的解决方案中借了很多东西,这非常聪明,因为它使用Marker的锚而不是实际改变插值器中Marker的LatLng,但它实际上是Jarvis代码和M D的混合,只有很小的修改。我根本不使用TimerTask,而是创建一个方法来使Marker反弹并且以递归方式调用它,这使得代码更加紧凑。我不确定是否会对此产生任何负面影响,但它确实有效,所以我会将它发布给任何不想创建TimerTask的人,而只是想让动画连续循环:

private void setMarkerBounce(final Marker marker) {
    final Handler handler = new Handler();
    final long startTime = SystemClock.uptimeMillis();
    final long duration = 2000;
    final Interpolator interpolator = new BounceInterpolator();
    handler.post(new Runnable() {
        @Override
        public void run() {
            long elapsed = SystemClock.uptimeMillis() - startTime;
            float t = Math.max(1 - interpolator.getInterpolation((float) elapsed/duration), 0);
            marker.setAnchor(0.5f, 1.0f +  t);

            if (t > 0.0) {
                handler.postDelayed(this, 16);
            } else {
                setMarkerBounce(marker);
            }
        }
    });
}

答案 1 :(得分:6)

嘿Javis这里是你的解决方案下面的代码是完美的工作在我的情况下。 首先添加:

 static final LatLng SECC = new LatLng(55.8607, -4.2871);
 private Marker mPerth;

现在,将Marker添加到地图中:

mPerth = mMap
                .addMarker(new MarkerOptions()
                        .position(SECC)
                        .title("SECC")
                        .snippet(
                                "Exhibition Way, Glasgow, G3 8YW\nSports: Boxing, Gymnastics, Judo, Netball, Wrestling, Weightlifting"));

        Timer timer = new Timer();
        TimerTask updateProfile = new CustomTimerTask(Stacky.this); 
        timer.scheduleAtFixedRate(updateProfile, 10,5000);

        mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(SECC, 18.0f));

最后添加CustomTimeTask,这将继续每隔15秒调用一次:

class CustomTimerTask extends TimerTask {
    private Context context;
    private Handler mHandler = new Handler();

    // Write Custom Constructor to pass Context
    public CustomTimerTask(Context con) {
        this.context = con;
    }

    @Override
    public void run() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                mHandler.post(new Runnable() {
                    @Override
                    public void run() {
                        final Handler handler = new Handler();
                        final long start = SystemClock.uptimeMillis();
                        final long duration = 1500;

                        final Interpolator interpolator = new BounceInterpolator();

                        handler.post(new Runnable() {
                            @Override
                            public void run() {
                                long elapsed = SystemClock.uptimeMillis() - start;
                                float t = Math.max(
                                        1 - interpolator.getInterpolation((float) elapsed
                                                / duration), 0);
                                mPerth.setAnchor(0.5f, 1.0f + 2 * t);

                                if (t > 0.0) {
                                    // Post again 16ms later.
                                    handler.postDelayed(this, 16);
                                }
                            }
                        });
                    }
                });
            }
        }).start();

    }

}

我希望这会对你有所帮助。