丑陋的片段转换到带有覆盖层的SurfaceView

4
我的应用程序有一个Fragment,在SurfaceView上绘制地图,并使用叠加层添加按钮和文本。其他大部分Fragment都是列表。我的问题是,在从地图切换到另一个Fragment时,丑陋的黑色矩形会在地图视图中出现,这是放置文本和按钮视图的位置。虽然在两个方向上都会发生,但在切换回地图时更为明显。我正在使用左滑/右滑动画,在所有其他方面都很好。我尝试了其他动画和无动画。在过渡之前将文本和按钮设置为不可见也没有帮助,因为这些视图仍然存在于覆盖层中。
如果有人有任何想法如何实现干净的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>

可能是 https://dev59.com/rmYq5IYBdhLWcg3whQ40 的重复问题。 - Ben Max Rubinstein
谢谢 - 这很可能是一个相关的问题,但我不认为它是一个重复的问题,因为这个问题与浮层有关。没有浮层,过渡就很干净。而且我们没有使用Google地图。 - Chris Fawcett
可能这些都是真的,但涉及SurfaceView的动画效果有很长一段历史问题。如果你仅支持API Level 11+,考虑使用TextureView,因为TextureView对于动画应该更有效。 - CommonsWare
我们必须支持Gingerbread这一版本,这并不是可选项。此外,我们需要一个SurfaceView来实现完整的地图功能。 - Chris Fawcett
1个回答

3

如果您正在使用SurfaceView,请将背景设为透明。

android:background="@android:color/transparent"

这对我有用,闪烁已消失。


可以确认,这个解决方法可行。 - Oleksii Masnyi

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接