clo*_*ith 8 animation android android-layout activity-lifecycle android-activity
在视图onGlobalLayoutFinished上调用后,我在活动中设置视图的动画.我的动画在开始时跳过~300 ms的帧.如果我将动画延迟超过300毫秒,它不会跳过任何帧.导致这种情况发生的活动是怎么回事?我怎么能阻止它或者我怎么能听完呢?
我创建了一个简单的死应用来演示这种行为.
<application>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>
Run Code Online (Sandbox Code Playgroud)
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();
}
}
Run Code Online (Sandbox Code Playgroud)
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>
Run Code Online (Sandbox Code Playgroud)
在此演示中,红色方块在1000毫秒内从左向右移动1000个像素.如果没有设置延迟,它会大致跳过前300个像素.如果设置了延迟,则可以平滑地动画.请看下面的视频.
没有延迟(跳过帧): https ://www.youtube.com/watch?v = dEvvllhvvN0
400毫秒延迟(不会跳过名人): https ://www.youtube.com/watch?v = zW0akPhl_9I &feature = youtu.be
欢迎任何评论.
小智 2
发生这种情况是因为活动的过渡动画,该动画在创建活动时播放。因此,您的自定义动画会在系统忙于绘制过渡时开始。
我还没有找到一个干净的解决方案。目前,我只是在开始动画之前等待过渡的持续时间,但它远非完美:
// 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);
Run Code Online (Sandbox Code Playgroud)
您还可以禁用活动转换,方法是:
overridePendingTransition(0, 0);
Run Code Online (Sandbox Code Playgroud)
我仍在寻找一种方法来收听过渡的确切结束时间。如果我找到解决方案,我会及时通知您。