微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

android – 如何从MapController捕获放大/缩小动画的结尾?

这里只是一个有趣的查询,有一种方法可以在调用缩放动画序列结束时捕获:

MapController.zoomIn() or MapController.zoomOut();

我知道它确实启动了一个动画序列来放大/缩小到下一个级别,但是没有已知的方法可以找到/谷歌搜索等,以找出它何时完成该序列.我需要能够在停止时运行更新命令,以便我的地图正确更新.

我发现通过在调用上面的函数后运行update命令,Projection不是来自缩小级别,而是介于两者之间(所以我无法显示我需要的所有数据).

解决方法:

我不得不承认我在这里受到了抨击,这是一个黑客攻击,但它很有效.我开始时需要知道何时发生了缩放,并且一旦我迷上了(并且在一些有趣的调试之后)我发现一些值是“缩放之间”值,所以我需要等到缩放完成后.

正如Stack Overflow上其他地方所建议的那样,我的缩放侦听器是一个重写的MapView.dispatchDraw,用于检查自上次以来缩放级别是否已更改.

除此之外,我添加一个isResizing方法,该方法检查自getLongitudeSpan值停止更改以来时间戳是否超过100毫秒.效果很好.这是代码

我的第一个Stack Overflow帖子!喔喔!

公共类MapViewWithZoomListener扩展MapView {

private int oldZoomLevel = -1;
private List<OnClickListener> listeners = new ArrayList<OnClickListener>();
private long resizingLongitudeSpan = getLongitudeSpan();
private long resizingTime = new Date().getTime();

public MapViewWithZoomListener(Context context, String s) {
    super(context, s);
}

public MapViewWithZoomListener(Context context, AttributeSet attributeSet) {
    super(context, attributeSet);
}

public MapViewWithZoomListener(Context context, AttributeSet attributeSet, int i) {
    super(context, attributeSet, i);
}

public boolean isResizing() {
    // done resizing if 100ms has elapsed without a change in getLongitudeSpan
    return (new Date().getTime() - resizingTime < 100);
}

public void dispatchDraw(Canvas canvas) {
    super.dispatchDraw(canvas);
    if (getZoomLevel() != oldZoomLevel) {
        new AsyncTask() {
            @Override
            protected Object doInBackground(Object... objects) {
                try {
                    if (getLongitudeSpan() != resizingLongitudeSpan) {
                        resizingLongitudeSpan = getLongitudeSpan();
                        resizingTime = new Date().getTime();
                    }
                    Thread.sleep(125); //slightly larger than isMoving threshold
                } catch (InterruptedException e) {
                    e.printstacktrace();
                }
                return null;
            }

            @Override
            protected void onPostExecute(Object o) {
                super.onPostExecute(o);
                if (!isResizing() && oldZoomLevel != getZoomLevel()) {
                    oldZoomLevel = getZoomLevel();
                    invalidate();
                    for (OnClickListener listener : listeners) {
                        listener.onClick(null);
                    }
                }
            }
        }.execute();
    }
}

public void addZoomListener(OnClickListener listener) {
    listeners.add(listener);
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐