如果想实现点击Menu键显示隐藏菜单,那么就需要重写onKeyDown方法。想解决多次点击事件,动画无法完全执行,就需要AnimUtil类中定义一个静态变量,用于记录当前执行的动画数量。需要使用RotateAnimation中的setAnimationListener来实现对该静态变量的操作,当启动动画的时候进行对该变量进行自加操作,当结束动画的时候对该变量进行自减操作。
public class AnimUtil { public static int animCount = 0;//记录当前执行的动画数量 public static void closeMenu(RelativeLayout rl,int startOffset){ for (int i = 0; i < rl.getChildCount(); i++) { rl.getChildAt(i).setEnabled(false); } //pivotXValue: 0-1 RotateAnimation animation = new RotateAnimation(0, -180, RotateAnimation.RELATIVE_TO_SELF, 0.5f, RotateAnimation.RELATIVE_TO_SELF, 1); animation.setDuration(500); animation.setFillAfter(true);//动画结束后保持当时的状态 animation.setStartOffset(startOffset); animation.setAnimationListener(new MyAnimationListener()); rl.startAnimation(animation); } public static void showMenu(RelativeLayout rl,int startOffset){ for (int i = 0; i < rl.getChildCount(); i++) { rl.getChildAt(i).setEnabled(true); } RotateAnimation animation = new RotateAnimation(-180, 0, RotateAnimation.RELATIVE_TO_SELF, 0.5f, RotateAnimation.RELATIVE_TO_SELF, 1); animation.setDuration(500); animation.setFillAfter(true);//动画结束后保持当时的状态 animation.setStartOffset(startOffset); animation.setAnimationListener(new MyAnimationListener()); rl.startAnimation(animation); } static class MyAnimationListener implements AnimationListener{ @Override public void onAnimationStart(Animation animation) { animCount++; } @Override public void onAnimationEnd(Animation animation) { animCount--; } @Override public void onAnimationRepeat(Animation animation) { } } }
MainActivity代码如下:
public class MainActivity extends Activity implements OnClickListener { private String tag = MainActivity.class.getSimpleName(); private ImageView iv_home, iv_menu; private RelativeLayout leve1, leve2, leve3; // 是否显示二级菜单 private boolean isShowLeve2 = true; // 是否显示三级菜单 private boolean isShowLeve3 = true; // 是否显示整个菜单,包括1级,2级,3级的菜单 private boolean isShowMenu = true; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // 初始化View initView(); // 初始监听 initListenter(); } // 用于初始化View private void initView() { setContentView(R.layout.activity_main); iv_home = (ImageView) findViewById(R.id.iv_home); iv_menu = (ImageView) findViewById(R.id.iv_menu); leve1 = (RelativeLayout) findViewById(R.id.level1); leve2 = (RelativeLayout) findViewById(R.id.level2); leve3 = (RelativeLayout) findViewById(R.id.level3); } // 初始监听 private void initListenter() { iv_home.setOnClickListener(this); iv_menu.setOnClickListener(this); } @Override public boolean onKeyDown(int keyCode, KeyEvent event) { if (keyCode == KeyEvent.KEYCODE_MENU) { if (isShowMenu) { // 需要关闭所有菜单 int startOffset = 0; if (isShowLeve3) { AnimUtil.closeMenu(leve3, startOffset); isShowLeve3 = false; startOffset += 200; } if (isShowLeve2) { AnimUtil.closeMenu(leve2, startOffset); isShowLeve2 = false; startOffset += 200; } AnimUtil.closeMenu(leve1, startOffset); } else { // 需要显示所有菜单 AnimUtil.showMenu(leve1, 0); AnimUtil.showMenu(leve2, 200); isShowLeve2 = true; AnimUtil.showMenu(leve3, 400); isShowLeve3 = true; } isShowMenu = !isShowMenu; // 表示我们点击Menu键 处理了 return true; } return super.onKeyDown(keyCode, event); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.iv_home: if(AnimUtil.animCount != 0){ //说明有动画执行 return; } if (isShowLeve2) { // 需要隐藏 int startOffset = 0; if (isShowLeve3) { AnimUtil.closeMenu(leve3, startOffset); startOffset += 300; isShowLeve3 = false; } AnimUtil.closeMenu(leve2, startOffset); } else { // 需要显示 AnimUtil.showMenu(leve2, 0); } isShowLeve2 = !isShowLeve2; break; case R.id.iv_menu: if(AnimUtil.animCount != 0){ //说明有动画执行 return; } if (isShowLeve3) { // 关闭三级菜单 AnimUtil.closeMenu(leve3, 0); } else { // 显示三级菜单 AnimUtil.showMenu(leve3, 0); } isShowLeve3 = !isShowLeve3; break; default: break; } } }