【问题标题】:TranslateAnimation with RelativeLayout and SurfaceView带有 RelativeLayout 和 SurfaceView 的 TranslateAnimation
【发布时间】:2015-07-02 12:32:33
【问题描述】:

直截了当,我目前在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);

这个mPlayerFrameRelativeLayout. 的变量

但是,我还必须支持 API 级别 10。客户端坚持这样做。在这里和其他地方浏览后,我想出了这段代码。

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

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

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

  • 将动画设置为mPlayerView

    mPlayerView.startAnimation(anim);
    
  • 设置偏移量TopAndBottom

    mPlayerView.offsetTopAndBottom(distance);
    

    它确实使用它移动,但速度比 mPlayerFrame 移动的速度快得多。

总而言之,我在RelativeLayout 中有一个SurfaceView,我想将这个RelativeLayout 连同它的SurfaceView 孩子一起移动。使用 API 级别 11+ 可以轻松实现,但我也必须支持 API 级别 10。

谢谢,

【问题讨论】:

    标签: android surfaceview translate-animation


    【解决方案1】:

    如果您对旧的动画框架有困难,您可以使用强大的库,为旧的 Android 平台添加 API11 ObjectAnimator 的兼容性:

    nineoldandroids

    这个库和新的有一些区别,特别是新的实际上是把对象移动到新的位置,而库只是显示它的图像,所以我建议你根据API级别拆分你的代码.

    【讨论】:

    • 嘿,太好了!有用。这是一个遗留项目,所以我没想到它包括了 Nineoldandroids。在那里看到它令人惊喜:)
    猜你喜欢
    • 1970-01-01
    • 2012-06-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多