滚动scrollview时自动滚动滚动视图内部滚动视图

时间:2013-04-06 17:25:26

标签: android android-mapview android-scrollview

如何自动滚动滚动视图内的地图,如 Expedia应用程序

1 个答案:

答案 0 :(得分:0)

得到了解决方案。 首先创建自定义scrollview。 将mapview放在scrollview中。 然后在onScrollChanged上自定义scrollview,滚动mapview,如

在CustomScrollView.java上:

protected void onScrollChanged(int l, int t, int oldl, int oldt) 
{
    super.onScrollChanged(l, t, oldl, oldt);
    scrollMap(l,t);
}

在YourActivity.java上:

public void scrollMap(int x, int y)
{
    int scrollLength = scrollView.getMeasuredHeight();
    int mapViewHeight = mapView.getMeasuredHeight();

    int mapViewCenter = (int)(mapViewHeight/2);
    int scrollMapHeight = (mapViewCenter + ((int)((mapViewHeight*y)/scrollLength)));

    scrollMapHeight = (int)((mapViewCenter - scrollMapHeight)/(1.5));

    if(scrollMapHeight<0)           
    mapView.scrollTo(x, scrollMapHeight);       
}