我有 ListFragment (support.v4),只显示 ListView 。单击元素时,将启动该元素上的动画。动画在 folder_animation_in_1.xml 中定义(动画的进一步说明在底部给出了截图):
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/accelerate_decelerate_interpolator"
android:shareInterpolator="true" >
<scale
android:fromYScale="1.0"
android:fromXScale="1.0"
android:toYScale="5.0"
android:toXScale="5.0"
android:pivotY="50%"
android:pivotX="100%"
android:duration="250"/>
<alpha
android:fromAlpha="1"
android:toAlpha="0"
android:duration="250"/>
</set>
我在 ListFragment 中启动动画,如下所示:
public void onListItemClick(final ListView l, final View v, final int position, long id) {
super.onListItemClick(l, v, position, id);
Animation expandAnim = AnimationUtils.loadAnimation(getActivity(), R.anim.folder_animation_in_1);
v.bringToFront();
v.setBackgroundColor(Color.BLACK);
v.setAnimation(expandAnim);
v.startAnimation(expandAnim);
}
只要方向是纵向,这样就可以正常工作。一旦方向改变为横向模式,我就会得到一种我不理解的怪异行为。单击的元素突然移动到ListView中的最后一个可见位置。现在动画从那里开始。
我还设置了一个平板电脑模拟器来真正启动景观中的应用程序,因为我认为由于方向更改本身可能存在问题。但这种行为根本没有改变。仍然是同样的问题。
感谢您的帮助!
更新 我使用以下代码进行了一些测试:
public void onListItemClick(final ListView l, final View v, final int position, long id) {
super.onListItemClick(l, v, position, id);
Rect rect = new Rect();
v.getDrawingRect(rect);
int[] coord = new int[2];
v.getLocationOnScreen(coord);
Log.e(tag, "ListView-item: left: " + rect.left + ", right: " + rect.right+ ", top: " + rect.top + ", bottom: " + rect.bottom + ", Pos on Screen: " + coord[0] + ", " + coord[1]);
View last = l.getChildAt(l.getLastVisiblePosition() - l.getFirstVisiblePosition());
last.getDrawingRect(rect);
last.getLocationOnScreen(coord);
Log.e(tag, "ListView-item: left: " + rect.left + ", right: " + rect.right+ ", top: " + rect.top + ", bottom: " + rect.bottom + ", Pos on Screen: " + coord[0] + ", " + coord[1]);
Animation expandAnim = AnimationUtils.loadAnimation(getActivity(), R.anim.folder_animation_in_1);
v.bringToFront();
v.setBackgroundColor(Color.BLACK);
v.setAnimation(expandAnim);
v.startAnimation(expandAnim);
v.postDelayed(new Runnable() {
@Override
public void run() {
Rect rect = new Rect();
v.getDrawingRect(rect);
int[] coord = new int[2];
v.getLocationOnScreen(coord);
Log.e(tag, "ListView-item: left: " + rect.left + ", right: " + rect.right+ ", top: " + rect.top + ", bottom: " + rect.bottom + ", Pos on Screen: " + coord[0] + ", " + coord[1]);
}
}, animationDuration);
}
给定测试的输出是:
ListView-item: left: 0, right: 683, top: 0, bottom: 100, Pos on Screen: 0, 269
ListView-item: left: 0, right: 683, top: 0, bottom: 100, Pos on Screen: 0, 677
ListView-item: left: 0, right: 683, top: 0, bottom: 100, Pos on Screen: 0, 677
我不确定这些信息对此有多大帮助,但它支持对效果的视觉感知 - 我真的认为ListView的给定元素在动画开始之前被放置在最后一个可见位置。但为什么??
纠正动画行为
横向模式中的动画行为