【发布时间】:2016-01-01 00:14:42
【问题描述】:
我想使用来自this answer 的展开/折叠动画来显示/隐藏弹出窗口。 我可以通过将动画应用于弹出视图来使用动画,弹出视图是弹出窗口内的视图。我现在面临的问题是,当用户在弹出窗口之外触摸时,弹出窗口会自动关闭,并且在关闭弹出窗口之前我无法显示折叠动画。
这是我写的代码:
View popupView = View.inflate(context,R.layout.popuplayout, null);
popup = new PopupWindow(popupView,ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT);
popup.setAnimationStyle(0);
popup.setOutsideTouchable(true);
popup.setFocusable(true);
popup.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
popup.showAsDropDown(anchor, 0, 0);
popup.setBackgroundDrawable(null);
popupView.post(new Runnable() {
@Override
public void run() {
expand(popupView);
}
});
.
.
.
private void expand(final View v) {
final int targetHeight = ((View)v.getParent()).getHeight();
// Older versions of android (pre API 21) cancel animations for views with a height of 0.
v.getLayoutParams().height = 1;
v.setVisibility(View.VISIBLE);
Animation a = new Animation()
{
@Override
protected void applyTransformation(float interpolatedTime, Transformation t) {
v.getLayoutParams().height = interpolatedTime == 1
? LayoutParams.MATCH_PARENT
: (int)(targetHeight * interpolatedTime);
v.requestLayout();
}
@Override
public boolean willChangeBounds() {
return true;
}
};
a.setDuration(200);
v.startAnimation(a);
}
我想知道是否有一种方法可以在没有 xml 样式的情况下在触摸外部时关闭弹出窗口之前显示动画,或者使用 xml 动画实现给定的动画。
【问题讨论】:
标签: android animation android-popupwindow