RelativeLayout causes animation not to work?

2020-07-09 08:12发布

问题:

I have an activity whose layout only contains a VideoView. Here is the xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
 xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="match_parent"
 android:layout_height="match_parent">


  <VideoView
android:id="@+id/videoplayer"
android:layout_width="match_parent"
android:layout_height="match_parent" 
android:layout_alignParentRight="true"
android:layout_alignParentLeft="true" 
android:layout_alignParentTop="true"
android:layout_alignParentBottom="true"
>
</VideoView>

I am trying to apply this animation to the VideoView after it stops playing:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate android:fromYDelta="0%" android:toYDelta="-100%" android:duration="500"/>
    <scale
    android:fromXScale="1.0"
    android:toXScale="0"
    android:fromYScale="1.0"
    android:toYScale="0"
    android:duration="500"
    android:pivotX="50%"
    android:pivotY="0%"
     />

</set>

This works perfectly as it is shown. But if I switch from LinearLayout to RelativeLayout in the layout then the animation no longer works and the video just freezes on the last frame that is shown before it stops.

Why would the type of root layout in my activity cause an animation not to function properly?

EDIT: To add to the weirdness if I add this TextView

<TextView
android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text=" ">
</TextView> 

To the RelativeLayout below the VideoView then the animation will work. However if I take the space out of the android:text element of this then it is back to not working. o.O

EDIT: I've awarded the bounty to Beowulf Bjornson for the nice tip of using the newer animation framework.

But I am still very interested if anyone ever figures out what is going on with the old style animations in this situation I'd be more than happy to throw some more points up for it.

回答1:

I'm not sure if using a third-party library is a viable solution for you, but I strongly recommend NineOldAndroids, so you can use the Animation capabilities of 3.0+ in earlier versions of Android. They make this job WAY easier.

Here's the link: http://nineoldandroids.com/

Then, on you activity you can do something as easy as:

VideoView view = (VideoView) findViewById(R.id.videoplayer);
animate(view).scaleX(0).scaleY(0).setDuration(500L);

I've tested it using RelativeLayout and it works as expected.

EDIT: If you wish to use the native honeycomb implementation, this specific API is 3.1+ and it should be used nativelly like the following:

view.animate().scaleX(0).scaleY(0).setDuration(500L);

And if you wish to nativelly support 3.0, you'll have to use it like this:

AnimatorSet set = new AnimatorSet();
set.playTogether(
    ObjectAnimator.ofFloat(view, "scaleX", 1.0f, 0.0f),
    ObjectAnimator.ofFloat(view, "scaleY", 1.0f, 0.0f),
);
set.setDuration(500).start();


回答2:

I am not sure but In My app i have RelativeLayout and Animaion and also the Video works fine.

If VideoView is the Only View of the Layout then put the relativeLayout height and Width as fill_parent and also for the VideoView and then try.

Hope it will works. If not then let me know.