在我的应用中,我有一个带有3个项目(索引:0、1、2)的操作栏,默认情况下,在xml中将其设置为android:visible=false
,当显示mapfragment时,有2个项目视图(索引:1和2)从菜单中被动画淡入,效果很好。
然后,我还有一个嵌套片段,每当长按一个标记信息文本窗口时,该片段就会弹出我的地图片段前面。
但是,每次显示此片段时,我都希望从操作栏中隐藏/淡出这2个菜单项,并同时显示带有动画的其余菜单项(0)。 (这不起作用)
在项目0上淡入淡出也可以在项目1上淡出,但是项目2保持不变,即使我注释掉项目1淡出的行,项目1也消失了!项目2保持不变,这很奇怪。
关闭弹出窗口片段时,发生相反的操作:淡入项1和2,淡出项0,这也效果很好!
这是我第一次使用动画,所以我真的不明白为什么动画不能仅在这种情况下起作用,我希望有人可以帮助我。
我使用处理程序的方式是从关于stackoverflow的答案中获得的,但对不起,我找不到链接了。
*inside map fragment*
boolean openingPopUp=false;
@Override
public void onPrepareOptionsMenu(Menu menu) {
super.onPrepareOptionsMenu(menu);
if (openingPopUp) {
openingPopUp = false;
changeFavIcon(menu);
} else {
final MenuItem cero, uno, dos;
cero=menu.getItem(0);
uno=menu.getItem(1);
dos=menu.getItem(2);
if (cero.isVisible()) {//when returning from popUp frag
//fadeout popUp fav icon(works)
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
AlphaAnimation animation = new AlphaAnimation(1.0f, 0.0f);
animation.setFillEnabled(true);
animation.setFillAfter(true);
animation.setDuration(1000);
getActivity().findViewById(R.id.action_popupfav).startAnimation(animation);
}
}, 1);
//disappear view after animation (works)
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
cero.setVisible(false);
}
}, 1000); // The animation is finished after 1000ms
//fade in views(works)
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
AlphaAnimation animation = new AlphaAnimation(0.0f, 1.0f);
animation.setDuration(1000);
// Make item visible and start the animation
uno.setVisible(true);
dos.setVisible(true);
getActivity().findViewById(R.id.action_favs).startAnimation(animation);
getActivity().findViewById(R.id.action_notifications).startAnimation(animation);
}
}, 1);
} else {//First time we open app
//fade in views (this works)
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
AlphaAnimation animation = new AlphaAnimation(0.0f, 1.0f);
animation.setDuration(1000);
// Make item visible and start the animation
uno.setVisible(true);
dos.setVisible(true);
getActivity().findViewById(R.id.action_favs).startAnimation(animation);
getActivity().findViewById(R.id.action_notifications).startAnimation(animation);
}
}, 1);
}
}
}
private void changeFavIcon(Menu menu) {
final MenuItem cero, uno, dos;
cero=menu.getItem(0);
uno=menu.getItem(1);
dos=menu.getItem(2);
//fade out views(THIS DOESNT WORK)
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
AlphaAnimation animation = new AlphaAnimation(1.0f, 0.0f);
animation.setFillEnabled(true);
animation.setFillAfter(true);
animation.setDuration(1000);
uno.setVisible(true);
dos.setVisible(true);
getActivity().findViewById(R.id.action_notifications).startAnimation(animation);
getActivity().findViewById(R.id.action_favs).startAnimation(animation);//<-this is the only one executing, even if commented out!
}
}, 1);
// //set visible false for views after animation(works)
/* new Handler().postDelayed(new Runnable() {
@Override
public void run() {
uno.setVisible(false); //This method is commented out to see that only one animation is executing
dos.setVisible(false);
}
}, 1000); // The animation is finished after 1000ms
*/
//fade in view(works)
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
AlphaAnimation animation = new AlphaAnimation(0.0f, 1.0f);
animation.setDuration(1000);
// Make item visible and start the animation
cero.setVisible(true);
getActivity().findViewById(R.id.action_popupfav).startAnimation(animation);
}
}, 1);
}