【问题标题】:Google maps v2 fragment view flickers when changing it's width更改宽度时,谷歌地图 v2 片段视图闪烁
【发布时间】:2014-06-11 05:28:32
【问题描述】:

我的应用包含 Google 地图 v2 API 片段视图。单击按钮时,我想从左到右更改它的宽度,如下所示:谷歌地图采用半屏->谷歌地图采用全屏。

它有效,但我在宽度转换期间在屏幕右侧看到奇怪的黑色区域。 我读过这是谷歌的错误,他们为此发布了修复程序,但它只适用于 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" />

谷歌地图初始化:

        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);

点击“展开”按钮,我正在展开谷歌地图,如下所示:

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;
}
}

【问题讨论】:

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


    【解决方案1】:

    我们通过在地图顶部添加视图并对视图宽度变化进行动画处理来解决此问题。没有其他工作。

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

    1. 不要对内部包含谷歌地图片段的片段进行“添加”或“替换”操作。相反,使用片段视图的显示/隐藏操作。当您使用这些操作时,这不会重新创建已创建的片段。例如:

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

    【讨论】:

    • 我正在使用 attach() 和 detach() 。这会导致在每个附件上重新创建片段。这是有意的吗?
    【解决方案2】:

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

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

    【讨论】:

      猜你喜欢
      • 2013-10-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多