这可能有点长,但我会在解释时非常清楚。
我正在开发一个应用程序,它基本上由一个布局组成,有两个相对布局。当您单击两个布局中的任何一个时,第二个布局会根据其实际可见性进行扩展或缩小。
这是布局:
这是侦听任何单击“main”或“child”以展开/收缩的代码:
public void toggleAmagar(View v){
View parent = (View) v.getParent();
View child = parent.findViewById(R.id.child);
Log.d("debugging","toggle amagar. Child visibility is :"+child.getVisibility()+". Clickable? Child: "+child.isClickable()+". Parent: "+v.isClickable());
ExpandAnimation expandAni = new ExpandAnimation(child, 500);
child.startAnimation(expandAni);
}
这是动画类:
public class ExpandAnimation extends Animation {
private View mAnimatedView;
private LayoutParams mViewLayoutParams;
private int mMarginStart, mMarginEnd;
private boolean mIsVisibleAfter = false;
private boolean mWasEndedAlready = false;
public ExpandAnimation(View view, int duration) {
setDuration(duration);
mAnimatedView = view;
mViewLayoutParams = (LayoutParams) view.getLayoutParams();
// decide to show or hide the view
mIsVisibleAfter = (view.getVisibility() == View.VISIBLE);
mMarginStart = mViewLayoutParams.bottomMargin;
mMarginEnd = (mMarginStart == 0 ? (0- view.getHeight()) : 0);
view.setVisibility(View.VISIBLE);
}
@Override
protected void applyTransformation(float interpolatedTime, Transformation t) {
super.applyTransformation(interpolatedTime, t);
Log.d("debugging","aplicant. Visible = "+this.mAnimatedView.getVisibility());
mAnimatedView.setClickable(false);
if (this.hasEnded()){
mAnimatedView.setClickable(true);
View pare = (View) mAnimatedView.getParent();
View germa = (View) pare.findViewById(R.id.main);
germa.setClickable(true);
} else {
mAnimatedView.setClickable(false);
View pare = (View) mAnimatedView.getParent();
View germa = (View) pare.findViewById(R.id.main);
germa.setClickable(false);
}
if (interpolatedTime < 1.0f) {
// Calculating the new bottom margin, and setting it
mViewLayoutParams.bottomMargin = mMarginStart
+ (int) ((mMarginEnd - mMarginStart) * interpolatedTime);
// Invalidating the layout, making us seeing the changes we made
mAnimatedView.requestLayout();
// Making sure we didn't run the ending before (it happens!)
} else if (!mWasEndedAlready) {
mViewLayoutParams.bottomMargin = mMarginEnd;
mAnimatedView.requestLayout();
if (mIsVisibleAfter) {
mAnimatedView.setVisibility(View.GONE);
}
mWasEndedAlready = true;
}
}
这是在 Android 2.3 中发生的情况:
这些是生成的日志:
日志一:
03-17 13:57:17.509: D/debugging(403): toggle amagar. Child visibility is :0. Clickable? Child: true. Parent: true
03-17 13:57:17.521: D/debugging(403): aplicant. Visible = 0
03-17 13:57:17.549: D/debugging(403): aplicant. Visible = 0
03-17 13:57:17.619: D/debugging(403): aplicant. Visible = 0
03-17 13:57:17.699: D/debugging(403): aplicant. Visible = 0
03-17 13:57:17.789: D/debugging(403): aplicant. Visible = 0
03-17 13:57:17.929: D/debugging(403): aplicant. Visible = 0
03-17 13:57:18.119: D/debugging(403): aplicant. Visible = 0
03-17 13:57:18.189: D/debugging(403): aplicant. Visible = 8
日志 B
03-17 13:57:45.439: D/debugging(403): toggle amagar. Child visibility is :8. Clickable? Child: true. Parent: true
现在我也在做同样的事情,但在 4.2 中(4.0.4 真实设备的工作方式相同):
这些是日志:
日志一:
03-17 14:13:06.104: D/debugging(815): toggle amagar. Child visibility is :0. Clickable? Child: true. Parent: true
03-17 14:13:06.192: D/debugging(815): aplicant. Visible = 0
03-17 14:13:06.212: D/debugging(815): aplicant. Visible = 0
03-17 14:13:06.475: D/debugging(815): aplicant. Visible = 0
03-17 14:13:06.692: D/debugging(815): aplicant. Visible = 0
03-17 14:13:06.792: D/debugging(815): aplicant. Visible = 8
日志 B:
03-17 14:13:08.923: D/debugging(815): toggle amagar. Child visibility is :8. Clickable? Child: true. Parent: true
03-17 14:13:09.013: D/debugging(815): aplicant. Visible = 0
03-17 14:13:09.022: D/debugging(815): aplicant. Visible = 0
03-17 14:13:09.232: D/debugging(815): aplicant. Visible = 0
03-17 14:13:09.442: D/debugging(815): aplicant. Visible = 0
03-17 14:13:09.666: D/debugging(815): aplicant. Visible = 0
03-17 14:13:09.803: D/debugging(815): aplicant. Visible = 0
如您所见,唯一的区别是第二次点击没有在 2.3 设备上应用动画。
我已经在具有 2.3 和其他 4.0.4 的真实设备上对其进行了测试,并且它也在发生。这些是虚拟化的。
如果您需要更多信息,请询问我。
哦,顺便说一下,Animation 类中的 setClickable() 技巧是为了防止用户双击动画和布局,以防万一你想知道它。
谢谢你。
塞尔吉
编辑 尝试了几个 SDK:
- 2.2 -> 错误
- 2.3 -> 错误
- 4.2 -> 好的
- 4.0 -> 好的
编辑 19/03
更新:仍在发生,我不知道为什么。如果没有人找到答案,明天我将继续调试......谢谢。