使用 RelativeLayout 和 SurfaceView 的 TranslateAnimation

TranslateAnimation with RelativeLayout and SurfaceView

直接提问,我目前在 RelativeLayout 中有 SurfaceView 如下:

<RelativeLayout
    android:id="@+id/surfaceframe"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="#FF000000"
    android:visibility="visible" >

    <SurfaceView
        android:id="@+id/surface"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:visibility="visible" />
</RelativeLayout>

我需要让 RelativeLayout 连同它的 SurfaceView 一起移动。简单。

mPlayerFrame.setTranslation(distance);

这个 mPlayerFrame 是可变的 RelativeLayout.

不过我也得支持API10级,客户非要这样。我在浏览这里和其他地方后想出了这段代码。

TranslateAnimation anim = new TranslateAnimation(0,0,distance,distance);
anim.setFillAfter(true);
anim.setDuration(0);
mPlayerFrame.startAnimation(anim);

通过上面的代码,我可以看到 mPlayerFrame 在移动,但播放(mPlayerViewSurfaceView)显然根本没有移动。

到目前为止,我已经尝试了以下方法:

总而言之,我在 RelativeLayout 中有一个 SurfaceView,我想移动这个 RelativeLayout 及其 SurfaceView 子项。使用 API 级别 11+ 很容易实现,但我也必须支持 API 级别 10。

谢谢,

如果您对旧的动画框架有困难,您可以使用很棒的库,它增加了 API11 ObjectAnimator 与旧 Android 平台的兼容性:

nineoldandroids

这个库和新的有一些不同,特别是新的实际上是将对象移动到新的位置,而库只是显示它的图像,所以我建议你根据API级。