Search code examples
androidsurfaceviewtranslate-animation

TranslateAnimation with RelativeLayout and SurfaceView


Straight to question, I currently have SurfaceView inside a RelativeLayout as follows:

<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>

I have a requirement to make the RelativeLayout along with its SurfaceView to move. Easy.

mPlayerFrame.setTranslation(distance);

This mPlayerFrame is variable for the RelativeLayout.

However, I also have to support API level 10. The client insists on doing so. I came up with this chunk of code after browsing here and elsewhere.

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

With the above code, I could see that mPlayerFrame is moving but the playback (mPlayerView, the SurfaceView) apparently did not move at all.

So far I have tried the followings:

  • Setting an animation to mPlayerView

    mPlayerView.startAnimation(anim);
    
  • Setting offsetTopAndBottom

    mPlayerView.offsetTopAndBottom(distance);
    

    it does move using this, but at a much faster rate than the rate of which mPlayerFrame is moving.

So to summarize, I have a SurfaceView inside a RelativeLayout and I want to move this RelativeLayout along with its SurfaceView child around. Easily achievable using API level 11+ but I had to support API Level 10 as well.

Thank you,


Solution

  • If you have difficult with old Animation Framework, you could use great Library that add compatibility of API11 ObjectAnimator to old Android Platform:

    nineoldandroids

    There is some differences between this Library and the new one, especially the fact that the new actually move the object to the new position and the library just show image of it, so I recommend that you split your code according to API level.