本文介绍了谷歌地图v2的片段来看闪烁时,改变它的宽度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序中包含谷歌地图V2 API片段视图。在按钮点击我想改变它的宽度由左到右是这样的:谷歌地图以半屏 - >谷歌地图以全屏幕

它的工作原理,但我看到的宽度转变过程中诡异的黑色区域右侧的画面。我已阅读,这是谷歌的错误,他们发布的此修复程序,但它只是由于Android 4.1版本的作品。我使用的是Android 4.0

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

任何人都可以帮我吗?

我的XML:

 < XML版本=1.0编码=UTF-8&GT?;
< RelativeLayout的的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
的xmlns:地图=htt​​p://schemas.android.com/apk/res-auto
机器人:ID =@ + ID / mainLayout
机器人:layout_width =match_parent
机器人:layout_height =match_parent
机器人:方向=垂直>

<片段
    机器人:ID =@ + ID /图
    机器人:名称=com.google.android.gms.maps.SupportMapFragment
    机器人:layout_width =510dp
    机器人:layout_height =match_parent
    机器人:layout_marginLeft =10dp
    机器人:layout_marginTop =10dp/>

< ImageView的
    机器人:ID =@ + ID / mapExpandButton
    机器人:layout_width =WRAP_CONTENT
    机器人:layout_height =WRAP_CONTENT
    机器人:layout_alignParentBottom =真
    机器人:layout_alignParentRight =真
    机器人:layout_marginBottom =25dp
    机器人:layout_marginRight =510dp
    机器人:背景=@可绘制/ map_expand/>

< ImageView的
    机器人:ID =@ + ID / mapCloseButton
    机器人:layout_width =WRAP_CONTENT
    机器人:layout_height =WRAP_CONTENT
    机器人:layout_alignParentBottom =真
    机器人:layout_marginBottom =25dp
    机器人:layout_marginLeft =15dp
    机器人:背景=@可绘制/ map_close/>
 

谷歌地图初始化:

  mapFragment =(SupportMapFragment)getActivity()getSupportFragmentManager()findFragmentById(R.id.map)。
        地图= mapFragment.getMap();
        map.getUiSettings()setRotateGesturesEnabled(假)。 //禁止转
        map.moveCamera(CameraUpdateFactory.newLatLngZoom(新经纬度(54.6873439,25.2770559),16.0f)); //设置初始位置
        map.getUiSettings()setZoomControlsEnabled(假)。
 

在扩展按钮点击,我扩大谷歌地图这样的:

  ResizeWidthAnimation动画=新ResizeWidthAnimation(mapFragment.getView(),宽度);
anim.setDuration(400);
mapFragment.getView().startAnimation(阿尼姆);
 

动画类:

 公共类ResizeWidthAnimation扩展动画
 {
  私人诠释mWidth;
  私人诠释mStartWidth;
  私人查看MView的;

公共ResizeWidthAnimation(查看视图,诠释宽度)
{
    MVIEW =图。
    mWidth =宽度;
    mStartWidth = view.getWidth();
}

@覆盖
保护无效applyTransformation(浮动interpolatedTime,变换T)
{
    INT newWidth = mStartWidth +(INT)((mWidth  -  mStartWidth)* interpolatedTime);

    mView.getLayoutParams()宽度= newWidth。
    mView.requestLayout();

}

@覆盖
公共无效初始化(INT宽度,高度INT,INT上级宽度,诠释上级高度)
{
    super.initialize(宽度,高度,上级宽度,上级高度);
}

@覆盖
公共布尔willChangeBounds()
{
    返回true;
}
}
 

解决方案

我们通过添加视图在地图上的顶部和动画这一观点的宽度变化解决了这个问题。没有别的工作。

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

  1. 不做添加或替换的操作与内含有谷歌地图片段的片段。相反,使用显示/隐藏操作与片段视图。当你使用这些操作这会不会重现已创建的片段。例如:

      FragmentManager经理= getSupportFragmentManager();
    FragmentTransaction FT1 = manager.beginTransaction();
    ft1.show(someFragment);
    ft1.hide(someFragment2);
    ft1.commit();
     

  2. 在应用程序执行,不要试图去改变谷歌地图的片段尺寸参数(宽度,高度,保证金等)都没有。搜索解决方法使用另一种(附加)的意见,如果你需要一些谷歌地图动画,宽度的变化,等等。

My app contains Google maps v2 API fragment view. On button click I want to change it's width from left to right side like this: Google map taking half screen --> Google map taking full screen.

It works, But I see strange black area on the right side of the screen during width transition.I have read that it's Google bug and that they released fix for this but it only works since Android 4.1 version. I am using Android 4.0

Also I checked some workarounds mentioned by another people but all of them focuses on tasks such like scrolling, etc. Nobody is mentioning google maps view width change.

Anybody can help me?

My 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 "expand" button click, I am expanding google map like this:

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

Animation class:

 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;
}
}
解决方案

We solved this issue by adding a view on top of the map and animating this view width change. Nothing else worked.

Tips to avoid Google map flickering bug:

  1. Don't do "add" or "replace" operations with the fragment which contains Google map fragment inside. Instead, use show/hide operations with fragment view. This will not recreate already created fragment when you use these operations. For example:

    FragmentManager manager = getSupportFragmentManager();  
    FragmentTransaction ft1 = manager.beginTransaction();
    ft1.show (someFragment);
    ft1.hide (someFragment2);
    ft1.commit();   
    

  2. During app execution, don't try to change google map fragment dimension parameters (width, height, margin, etc.) at all. Search workarounds by using another (additional) views if you need some google map animations, width changes, etc.

这篇关于谷歌地图v2的片段来看闪烁时,改变它的宽度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 10:27