I am animating a view in an activity after onGlobalLayoutFinished
is called on the view. My animation is skipping ~300 ms worth of frames in the beginning. If I delay the animation by more than ~300ms, it does not skip any frames. What is going on in the activity that is causing this to happen? How can I stop it or how can I listen for when it is finished?
I have created a dead simple app to demonstrate this behavior.
contents of <application>
in AndroidManifest.xml:
<activity
android:name=".main.TestLagActivity"
android:label="Test Lag Activity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
TestLagActivity.java:
public class TestLagActivity extends ActionBarActivity {
private View mRedSquareView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test_lag);
mRedSquareView = findViewById(R.id.activity_test_lag_redSquareView);
if (mRedSquareView.getViewTreeObserver() != null) {
mRedSquareView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
public void onGlobalLayout() {
mRedSquareView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
animate();
}
});
}
}
private void animate() {
ObjectAnimator xAnimator = ObjectAnimator.ofFloat(mRedSquareView, "x", 0, 1000);
xAnimator.setDuration(1000);
xAnimator.start();
}
}
activity_test_lag.xml:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<View
android:id="@+id/activity_test_lag_redSquareView"
android:layout_width="50dp"
android:layout_height="50dp"
android:background="#FF0000"/>
</FrameLayout>
In this demo, a red square moves from left to right 1000 pixels over 1000 milliseconds. If no delay is set, it skips roughly the first 300 pixels. If a delay is set, it animates smoothly. Please see videos below.
No delay (skips frames): https://www.youtube.com/watch?v=dEwvllhvvN0
400ms delay (does not skip fames): https://www.youtube.com/watch?v=zW0akPhl_9I&feature=youtu.be
Any comments are welcome.
onGlobalLayout()
callback is being invoked earlier than you need it. Try starting your animation from aRunnable
that you post to one your yourView
s inonCreate()
instead. – ArgyrolonGlobalLayout()
actually gets called about 10ms after the runnable runs that I post to the view, so the result is the same. – Territory