谷歌地图v2片段视图在更改宽度时会闪烁

时间:2014-04-25 13:10:02

标签: java android eclipse google-maps android-fragments

我的应用包含Google maps v2 API片段视图。点击按钮,我想从左到右改变它的宽度,如下所示:Google地图拍摄半屏 - > Google地图全屏显示。

它有效,但在宽度过渡期间,我在屏幕右侧看到奇怪的黑色区域。 我已经阅读了它的谷歌错误,他们发布了修复此问题,但它只适用于Android 4.1版本。我使用的是Android 4.0

此外,我检查了另一个人提到的一些变通方法,但所有这些变通都集中在滚动等任务上。没有人提到谷歌地图视图宽度变化。

任何人都可以帮助我?

我的xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:map="http://schemas.android.com/apk/res-auto"
android:id="@+id/mainLayout"     
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<fragment
    android:id="@+id/map"
    android:name="com.google.android.gms.maps.SupportMapFragment"
    android:layout_width="510dp"       
    android:layout_height="match_parent"
    android:layout_marginLeft="10dp"           
    android:layout_marginTop="10dp" />

<ImageView
    android:id="@+id/mapExpandButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentRight="true"
    android:layout_marginBottom="25dp"
    android:layout_marginRight="510dp"
    android:background="@drawable/map_expand" />

<ImageView
    android:id="@+id/mapCloseButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_marginBottom="25dp"
    android:layout_marginLeft="15dp"
    android:background="@drawable/map_close" />

Google maps init:

        mapFragment = (SupportMapFragment)getActivity().getSupportFragmentManager().findFragmentById(R.id.map);
        map = mapFragment.getMap ();
        map.getUiSettings().setRotateGesturesEnabled(false);    // disable rotation         
        map.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(54.6873439, 25.2770559) , 16.0f));  // set initial position         
        map.getUiSettings().setZoomControlsEnabled(false);

On&#34; expand&#34;点击按钮,我正在扩展谷歌地图:

ResizeWidthAnimation anim = new ResizeWidthAnimation(mapFragment.getView (), width);
anim.setDuration(400);
mapFragment.getView ().startAnimation(anim);

动画类:

 public class ResizeWidthAnimation extends Animation
 {
  private int mWidth;
  private int mStartWidth;
  private View mView;

public ResizeWidthAnimation(View view, int width)
{
    mView = view;
    mWidth = width;
    mStartWidth = view.getWidth();
}

@Override
protected void applyTransformation(float interpolatedTime, Transformation t)
{
    int newWidth = mStartWidth + (int) ((mWidth - mStartWidth) * interpolatedTime);

    mView.getLayoutParams().width = newWidth;
    mView.requestLayout();

}

@Override
public void initialize(int width, int height, int parentWidth, int parentHeight)
{
    super.initialize(width, height, parentWidth, parentHeight);
}

@Override
public boolean willChangeBounds()
{
    return true;
}
}

2 个答案:

答案 0 :(得分:2)

我们通过在地图顶部添加视图并为此视图宽度更改设置动画来解决此问题。没有其他工作。

避免谷歌地图闪烁错误的提示:

  1. 不要做&#34;添加&#34;或&#34;替换&#34;使用包含Google地图片段的片段进行操作。而是使用片段视图显示/隐藏操作。使用这些操作时,这不会重新创建已创建的片段。例如:

    FragmentManager manager = getSupportFragmentManager();  
    FragmentTransaction ft1 = manager.beginTransaction();
    ft1.show (someFragment);
    ft1.hide (someFragment2);
    ft1.commit();   
    
  2. 在应用执行期间,请勿尝试更改Google地图片段尺寸参数(宽度,高度,边距等)。如果您需要一些谷歌地图动画,宽度更改等,请使用其他(其他)视图搜索变通方法。

答案 1 :(得分:0)

在动画的每一帧上重新布线是非常昂贵的(对于地图来说甚至更多),你不应该这样做。如果您真的想要为地图的边界设置动画,请尝试应用缩放动画,然后在其末尾将视图重新布局到最终的新尺寸并禁用缩放。

要使用地图或任何其他SurfaceView来提高动画效果,您可以在MapFragment容器上调用requestTransparentRegion(),并将自身作为参数传递。