平滑放大mapview

时间:2010-12-09 12:54:37

标签: android google-maps zoom

当我使用MapController.setZoom(x)时,例如,从5级到15级缩放,缩放的执行速度非常快,并且通常不会加载新级别的地图图块。

这对用户来说看起来不太好。任何地图构建功能都可以将此更改为更慢的缩放,以便在达到15级之前可以加载或至少几乎加载切片?

祝你好运

P

2 个答案:

答案 0 :(得分:8)

更简单的方法是利用MapController.zoomIn()方法,该方法提供一些简单的动画来缩放步骤级别。

以下是一些代码:

    // a Quick runnable to zoom in
    int zoomLevel = mapView.getZoomLevel();
    int targetZoomLevel = 18;

    long delay = 0;
    while (zoomLevel++ < targetZoomLevel) {
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                mapController.zoomIn();
            }
        }, delay);

        delay += 350; // Change this to whatever is good on the device
    }

它的作用是创建一系列延迟的runnables,每一个都将在前一个之后350ms调用zoomIn()。

这假设您有一个Handler附加到主UI线程称为'handler'

: - )

答案 1 :(得分:0)

没有简单的方法可以做到这一点。但是,我可以帮助你。

首先,这是我的一个个人实用工具类的免费礼物, Tween.java

import android.os.Handler;

public class Tween {

    public static interface TweenCallback {
        public void onTick(float time, long duration);
        public void onFinished();
    }

    long start;
    long duration;
    Handler handler;
    TweenCallback callback;

    public Tween(TweenCallback callback) {      
        handler = new Handler();
        this.callback = callback;
    }
    public void start(final int duration) {
        start = android.os.SystemClock.uptimeMillis();
        this.duration = duration;
        tickRunnable.run();
    }
    public void stop() {
        handler.removeCallbacks(tickRunnable);
    }
    Runnable tickRunnable= new Runnable() {
        public void run() {
            long now = android.os.SystemClock.uptimeMillis();
            float time = now - start;
            boolean finished = (time >= duration);
            if (finished) {
                time = duration;
            }
            callback.onTick(time, duration);
            if (!finished) {
                handler.post(tickRunnable);
            }
            else {
                callback.onFinished();
            }
        }
    };

    //
    // Tweening functions. The 4 parameters are :
    //
    //  t - time, ranges from 0 to d
    //  b - begin, i.e. the initial value for the quantity being changed over time
    //  c - change, the amount b will be changed by at the end
    //  d - duration, of the transition, normally in milliseconds. 
    //
    // All were adapted from http://jstween.sourceforge.net/Tween.js 
    //
    public static float strongEaseInOut(float t, float b, float c, float d) {
        t/=d/2; 
        if (t < 1) return c/2*t*t*t*t*t + b;
        return c/2*((t-=2)*t*t*t*t + 2) + b;
    }
    public static float regularEaseIn(float t, float b, float c, float d) {
        return c*(t/=d)*t + b;
    }
    public static float strongEaseIn(float t, float b, float c, float d) {
        return c*(t/=d)*t*t*t*t + b;
    }
}

我建议你做的是将MapController.zoomToSpan()与Tween结合使用...这里有一些完全未经测试的代码应该可以使用,也许只需要一两个调整,你只需将它传递给目标lat&amp; Lon spans。 :

public void slowZoom(int latE6spanTarget, int lonE6spanTarget) {
    final float initialLatE6span = mapView.getLatitudeSpan();
    final float initialLonE6span = mapView.getLongitudeSpan();
    final float latSpanChange = (float)(latE6spanTarget - initialLatE6span);
    final float lonSpanChange = (float)(lonE6spanTarget - initialLonE6span);
    Tween tween = new Tween(new Tween.TweenCallback() {
        public void onTick(float time, long duration) {
            float latSpan = Tween.strongEaseIn(time, initialLatE6span, latSpanChange, duration);
            float lonSpan = Tween.strongEaseIn(time, initialLonE6span, lonSpanChange, duration);
            mapView.getController().zoomToSpan((int)latSpan, (int)lonSpan);
        }
        public void onFinished() {
        }
    });
    tween.start(5000);
}