我正在尝试在Honeycomb的新LayoutTransition类.在将视图添加到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));
}
});
}
}
这里是与该示例一起的布局文件:
android:layout_width="match_parent" android:layout_height="match_parent"
android:orientation="vertical">
android:layout_height="wrap_content" android:text="Add Button"
android:id="@+id/addNewButton" />
android:layout_width="match_parent" android:layout_height="match_parent"
android:id="@+id/frame_container" android:animateLayoutChanges="true" />
如何在APPEARING动画之前消除延迟?