我正在尝试为自定义菜单创建切换动画。可以通过不同的按钮选择多个菜单。 当没有菜单打开时,点击按钮应打开该菜单。 如果打开了另一个菜单,则关闭打开的菜单,然后播放该动画后,应打开所选的菜单。 每个关闭/打开动作都与ConstraintLayout过渡相结合。
由于无法正常工作,我创建了以下测试过程:
@Override
protected void onCreate(Bundle savedInstanceState) {
...
ConstraintLayout layout_main;
ConstraintSet l_mh0c = new ConstraintSet();
ConstraintSet l_mh1o = new ConstraintSet();
ConstraintSet l_mh1c = new ConstraintSet();
Button btn;
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
toggleOne();
}
});
layout_main = (ConstraintLayout) findViewById(R.id.constraintMain);
l_mh1o.clone(this, R.layout.main_menue_header1_open);
l_mh1c.clone(this, R.layout.main_menue_header1_closed);
l_mh0c.clone(this, R.layout.activity_main);
...
}
int openedMenue = -1;
long animationTime = 1000;
private void toggleOne() {
TransitionManager.endTransitions(layout_main); //<- makes no difference when commented
if(openedMenue==1) {
System.out.println("sync closing");
Runnable r = () -> toggleOne();
closeMenue(1, animationTime, r);
} else {
System.out.println("sync opening");
startMenue(1, animationTime);
}
}
public void startMenue(Integer index, final Long maxtime) {
Transition t;
switch (index) {
case 1:
t = new ChangeBounds();
t.setDuration(0).addListener(new TransitionEndListener() {
@Override
public void onTransitionEnd(Transition transition) {
Transition t = new ChangeBounds();
t.setDuration(maxtime / 2).addListener(new TransitionEndListener() {
@Override
public void onTransitionEnd(Transition transition) {
Transition t = new ChangeBounds();
t.setDuration(maxtime / 2);
TransitionManager.beginDelayedTransition(layout_main, t);
l_mh0o.applyTo(layout_main);
openedMenue = 1;
System.out.println("sync start finished");
}
});
TransitionManager.beginDelayedTransition(layout_main, t);
l_mh1o.applyTo(layout_main);
}
});
TransitionManager.beginDelayedTransition(layout_main, t);
l_mh1c.applyTo(layout_main);
// the following does not provoke any changes
Scene s = new Scene(layout_main);
TransitionManager.go(s);
break;
}
}
private void closeMenue(int index, final long maxtime, Runnable callback) {
System.out.println("sync closing menue " + openedMenue);
Transition t;
switch (index) {
case 1:
t = new ChangeBounds();
t.setDuration(maxtime/2).addListener(new TransitionEndListener() {
@Override
public void onTransitionEnd(Transition transition) {
Transition t = new ChangeBounds();
t.setDuration(maxtime/2).addListener(new TransitionEndListener() {
@Override
public void onTransitionEnd(Transition transition) {
Transition t = new ChangeBounds();
t.setDuration(0);
TransitionManager.beginDelayedTransition(layout_main, t);
l_mh0c.applyTo(layout_main);
openedMenue = -1;
try {
callback.run();
} catch (Exception e) { e.printStackTrace(); }
}
});
TransitionManager.beginDelayedTransition(layout_main, t);
l_mh1c.applyTo(layout_main);
openedMenue = 1;
}
});
TransitionManager.beginDelayedTransition(layout_main, t);
l_mh1o.applyTo(layout_main);
break;
}
}
运行它时,得到以下输出:
[Button Click]
sync opening
sync start finished
[Button Click]
sync closing
sync closing menue 1
sync opening
sync start finished // problem
但是,最后一行永远不会打印;好像(在关闭动作之后再次开始打开动作之后)startMenue()中的第一个过渡从未执行过对onTransitionEnd()的回调。
这里是TransitionEndListener(只是接口的一个简单包装器)
public abstract class TransitionEndListener implements Transition.TransitionListener {
@Override
public final void onTransitionStart(Transition transition) {}
@Override
public final void onTransitionCancel(Transition transition) {}
@Override
public final void onTransitionPause(Transition transition) {}
@Override
public final void onTransitionResume(Transition transition) {}
}
我已经通过在onTransitionCancel()中放置一条打印语句来检查是否取消了第二个开始的Transition,这似乎并非如此。
你能解释为什么会这样吗?
更新
我发现了this post on TransitionManager callbacks;
从mh0c到t0 mh1c的过渡是ConstraintLayout过渡,因为约束实际上发生了变化。但是,该过渡在UI上不可见,因为过渡元素的宽度为0。(此过渡是从一个菜单点到另一个菜单点的跳转,不可见。)
这可能是Transition不执行回调的原因吗?
如果是的话;我该如何解决?
更新2
我正在阅读the documentation,可能已经找到了使用TransitionManager.go(scene, transition)
的解决方案。
->不幸的是,这没有用;请参阅startMenue()的代码进行更改
答案 0 :(得分:0)
在越来越多的苦苦挣扎和审查这些帖子之后:
我发现了造成麻烦的原因:
closeMenue()的最内层部分(最内层的回调)
Transition t = new ChangeBounds();
t.setDuration(0);
TransitionManager.beginDelayedTransition(layout_main, t);
l_mh0c.applyTo(layout_main);
openedMenue = -1;
try {
callback.run();
} catch (Exception e) { e.printStackTrace(); }
命令顺序略有错误。它应该看起来像这样:
Transition t = new ChangeBounds();
t.setDuration(0).addListener(new TransitionEndListener() {
@Override
public void onTransitionEnd(Transition transition) {
try {
callback.run();
} catch (Exception e) { e.prtinStackTrace(); }
}
});
TransitionManager.beginDelayedTransition(layout_main, t);
l_mh0c.applyTo(layout_main);
openedMenue = -1;
这确保先完成关闭过渡,然后再开始打开过渡。