4

I have a view that has a MapFragment in it

My mainViewPanel looks something like this

<LinearLayout
android:id="@+id/main_view_panel"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="0.5"
android:orientation="horizontal">

<fragment
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="0.5"
class="com.google.android.gms.maps.SupportMapFragment"
map:cameraTargetLat="40.72"
map:cameraTargetLng="-74.00"
map:cameraZoom="8"/>

</LinearLayout>

My translate code looks like this

TranslateAnimation anim = new TranslateAnimation(0, 500, 0, 0);
anim.setFillAfter(true);
anim.setDuration(250);
mainViewPanel.startAnimation(anim);

The rest of the view animates, but the map stays put. It's like the main view window is moving over and I can see more to the right of the map, but the map itself is not visually moving. The animation works just fine for other views like a TextView

Drew LeSueur
  • 19,185
  • 29
  • 89
  • 106
  • 2
    That's probably because the map is based on a `SurfaceView`, which has somewhat different characteristics than regular widgets. Clearly, there are ways to translate a map, as `ViewPager` can do it, but I suspect it is not using `TranslateAnimation`, which is clunky for even normal widgets, let alone a `SurfaceView`. – CommonsWare Jan 19 '13 at 19:42
  • 1
    I just ran into the exact same issue. How would you animate it if not with a TranslateAnimation? – Goddchen Jan 30 '13 at 16:54
  • For older versions of android, I didn't animate, just did something like `mapWrapperView.setPadding(offsetWidth, 0, -offsetWidth, 0);` – Drew LeSueur Jan 30 '13 at 18:10

2 Answers2

3

This works on newer versions of android

mainViewPanel.animate().translationX(500);
Drew LeSueur
  • 19,185
  • 29
  • 89
  • 106
0

This is because of New map api is created using SurfaceView and due to that animation is not feasible.

As a temporary patch that I have found is: Use any transparent view over the MapFragment. So if you will animate the MapFragment, it will animate with it and at that time map will be invisible.

As an example:

<RelativeLayout
android:id="@+id/main_view_panel"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">

<fragment
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="0.5"
class="com.google.android.gms.maps.SupportMapFragment"
map:cameraTargetLat="40.72"
map:cameraTargetLng="-74.00"
map:cameraZoom="8"/>

 <ImageView
            android:id="@+id/map_overlay"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="#00000000" />

</RelativeLayout>

Try this. It should work.

Note : may be you have to change its visibility when you animate view. If you want map to be display after animation, you have to hide the overlay view and when you want to start animating , you have to unhide the overlay view. But you have to make sure that hide/unhide process will be done only after animation complete.

Steps for animation :

  • Before starting animation, unhide overlay view
  • after animation complete, means after some delay, hide the overlay view

It works for me.

Nirav Shah
  • 2,064
  • 5
  • 24
  • 34