My app one of the fragments draws a map in a surface view with an overlay for buttons and text.
Most of the other fragments are lists. My problem is that during the transition between the map and another fragment, ugly black rectangles momentarily appear in the map view where the text and button views are placed. This appears more pronounced on a transition back to the map, although it does happen in both directions. I am using a slide-left/slide-right animation which works nicely in all other respects. I've tried other animations and no animation. Setting the text and buttons invisible before the transition also doesn't help, since the views are still there in the overlay.
If anyone has any ideas how a clean fragment transition might be achieved it would be very much appreciated. I am using the support fragment manager.
Here is my code (there are considerably more text and buttons than I show below):
layout xml file:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/mapframeview"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<com.mypackage.MapView
android:id="@+id/maptileview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
</com.mypackage.MapView>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/mapoverlay"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TextView
android:id="@+id/titletext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_alignParentTop="true"
android:paddingTop="10dp"
android:textColor="#FFFFFF"
android:textSize="24dp"
android:textStyle="bold" />
<ImageButton
android:id="@+id/bookButton"
android:onClick="onClickBookButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:paddingTop="15dp"
android:paddingRight="15dp"
android:background="@android:color/transparent"
android:src="@drawable/bookbutton">
</ImageButton>
</RelativeLayout>
</FrameLayout>
fragment transition code:
private void startNextFragment(Fragment newFragment, String fragment tag) {
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction transaction =
fragmentManager.beginTransaction();
transaction.setCustomAnimations(R.layout.slideinright,
R.layout.slideoutleft,
R.layout.slideinleft,
R.layout.slideoutright);
transaction.replace(R.id.fragment_container, newFragment, fragmentTag);
transaction.addToBackStack(null);
// Commit the transaction
transaction.commit();
}
animation xmls:
<?xml version="1.0" encoding="utf-8"?>
<!--
/* //device/apps/common/res/anim/slide_in_right.xml */
-->
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate android:fromXDelta="50%p" android:toXDelta="0"
android:duration="@android:integer/config_mediumAnimTime"/>
<alpha android:fromAlpha="0.0" android:toAlpha="1.0"
android:duration="@android:integer/config_mediumAnimTime" />
</set>
<?xml version="1.0" encoding="utf-8"?>
<!--
/* //device/apps/common/res/anim/slide_out_left.xml*/
-->
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate android:fromXDelta="0" android:toXDelta="-50%p"
android:duration="@android:integer/config_mediumAnimTime"/>
<alpha android:fromAlpha="1.0" android:toAlpha="0.0"
android:duration="@android:integer/config_mediumAnimTime" />
</set>
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…