我正在尝试在Honeycomb中使用新的LayoutTransition
类。 我设置了一个动画,当将一个View
添加到ViewGroup
中时,它会滑动到位。 我注意到视图第一次呈现和LayoutTransition.APPEARING
动画开始之间有轻微的延迟(大约20ms)。 换句话说,在视图出现在屏幕上之后,它会悬停一会儿,然后开始动画效果。 甚至在ApiDemos
示例项目中也可以注意到这一点。在布局动画示例中,ViewGroup
的APPEARING动画总是存在延迟。 我甚至尝试将其他LayoutTransition
动画设置为null或最终给它们非常短的持续时间,但仍然会延迟APPEARING
动画。 这是我的代码:
public class DebugExampleFour extends Activity {
private int numButtons = 1;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.debug_example_four);
final ViewGroup frame = (ViewGroup) findViewById(R.id.frame_container);
LayoutTransition transitioner = new LayoutTransition();
Animator appearingAnimation = ObjectAnimator.ofFloat(null, "translationX", 600, 0);
appearingAnimation.setDuration(45);
appearingAnimation.setStartDelay(0);
appearingAnimation.setInterpolator(new DecelerateInterpolator());
appearingAnimation.addListener(new AnimatorListenerAdapter() {
public void onAnimationEnd(Animator anim) {
View view = (View) ((ObjectAnimator) anim).getTarget();
view.setTranslationX(0f);
}
});
transitioner.setAnimator(LayoutTransition.APPEARING, appearingAnimation);
Animator dummyAnimation = ObjectAnimator.ofInt(0, 1);
dummyAnimation.setDuration(1);
dummyAnimation.setStartDelay(0);
transitioner.setAnimator(LayoutTransition.CHANGE_APPEARING, dummyAnimation);
transitioner.setAnimator(LayoutTransition.CHANGE_DISAPPEARING, dummyAnimation);
transitioner.setAnimator(LayoutTransition.DISAPPEARING, dummyAnimation);
frame.setLayoutTransition(transitioner);
Button addButton = (Button) findViewById(R.id.addNewButton);
addButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Button newButton = new Button(DebugExampleFour.this);
newButton.setText("Click To Remove " + (numButtons++));
newButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
frame.removeView(v);
}
});
frame.addView(newButton, new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
}
});
}
}
这是与示例相关的布局文件:
<?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"
android:orientation="vertical">
<Button android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="Add Button"
android:id="@+id/addNewButton" />
<LinearLayout android:orientation="vertical"
android:layout_width="match_parent" android:layout_height="match_parent"
android:id="@+id/frame_container" android:animateLayoutChanges="true" />
</LinearLayout>
如何消除 APPEARING
动画之前的延迟?