问题
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.
回答1:
This happens because of the activity's transition animation, which is played while the activity is being created. As a result, your custom animation starts while the system is busy drawing the transition.
I haven't found a clean solution yet. For now, I'm simply waiting for the duration of the transition before starting my animation, but it is far from being perfect:
// The duration of the animation was found here:
// http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/2.3.5_r1/frameworks/base/core/res/res/anim/activity_open_enter.xml
new Handler().postDelayed(runnable, android.R.integer.config_shortAnimTime);
You could also disable activity transitions instead, with:
overridePendingTransition(0, 0);
I'm still looking for a way to listen to the exact end time of the transition. Will keep you posted if I ever find a solution.
来源:https://stackoverflow.com/questions/28443752/animation-at-the-beginning-of-activity-skips-frames