我的应用程序有一个Fragment,在SurfaceView上绘制地图,并使用叠加层添加按钮和文本。其他大部分Fragment都是列表。我的问题是,在从地图切换到另一个Fragment时,丑陋的黑色矩形会在地图视图中出现,这是放置文本和按钮视图的位置。虽然在两个方向上都会发生,但在切换回地图时更为明显。我正在使用左滑/右滑动画,在所有其他方面都很好。我尝试了其他动画和无动画。在过渡之前将文本和按钮设置为不可见也没有帮助,因为这些视图仍然存在于覆盖层中。
如果有人有任何想法如何实现干净的Fragment转换,那将非常感激。我正在使用support fragment manager。
以下是我的代码(比我下面展示的文本和按钮要多得多):
布局XML文件:
如果有人有任何想法如何实现干净的Fragment转换,那将非常感激。我正在使用support fragment manager。
以下是我的代码(比我下面展示的文本和按钮要多得多):
布局XML文件:
<?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>
片段转换代码:
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();
}
动画XML:
<?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>
SurfaceView
的动画效果有很长一段历史问题。如果你仅支持API Level 11+,考虑使用TextureView
,因为TextureView
对于动画应该更有效。 - CommonsWare