在上篇文章中实现了优酷菜单执行动画,本文接着完善已经实现的动画功能
本文地址:http://www.cnblogs.com/wuyudong/p/5915958.html ,转载请注明源地址。
已经实现的菜单动画功能存在一点BUG,那就是当快速连续点击menu或home按钮的时候,动画出现进入和退出不定操作现象
下面来解决这一问题,可以通过监听事件来判断当前是否正在执行进入或进出操作,代码如下:
package com.wuyudong.youkumenu.utils; import android.view.animation.Animation; import android.view.animation.Animation.AnimationListener; import android.view.animation.RotateAnimation; import android.widget.RelativeLayout; public class AnimationUtils { public static int runningAnimationCount = 0; // 旋转出去的动画 public static void rotateOutAnim(RelativeLayout layout, long delay) { RotateAnimation ra = new RotateAnimation( 0f, -180f, //开始,结束的角度, 逆时针 Animation.RELATIVE_TO_SELF, 0.5f, //相对的x坐标点(指定旋转中心x值) Animation.RELATIVE_TO_SELF, 1.0f); //相对的y坐标点(指定旋转中心y值) ra.setDuration(500); ra.setFillAfter(true); //设置动画停留在结束位置 ra.setStartOffset(delay); //设置动画开始延时 ra.setAnimationListener(new MyAnimationListener());//添加监听 layout.startAnimation(ra); } // 旋转进来的动画 public static void rotateInAnim(RelativeLayout layout) { RotateAnimation ra = new RotateAnimation( -180f, 0f, //开始,结束的角度, 顺时针 Animation.RELATIVE_TO_SELF, 0.5f, //相对的x坐标点(指定旋转中心x值) Animation.RELATIVE_TO_SELF, 1.0f); //相对的y坐标点(指定旋转中心y值) ra.setDuration(500); ra.setFillAfter(true); ra.setAnimationListener(new MyAnimationListener()); layout.startAnimation(ra); } static class MyAnimationListener implements AnimationListener { @Override public void onAnimationStart(Animation animation) { runningAnimationCount++; } @Override public void onAnimationEnd(Animation animation) { runningAnimationCount--; } @Override public void onAnimationRepeat(Animation animation) { // TODO Auto-generated method stub } } }
在点击事件中添加一小段判断的代码即可
public void onClick(View v) { if (AnimationUtils.runningAnimationCount > 0) { //当前有动画正在执行,不执行当前事件 return; } .......
问题搞定
接下来还要进一步完善,当用户点击手机menu的时候,同样能得到点击优酷菜单同样的动画效果
这样需要判断用户的点击本机menu按键事件:
public boolean onKeyDown(int keyCode, KeyEvent event) { // keyCode 事件码 if(keyCode == KeyEvent.KEYCODE_MENU) { if (AnimationUtils.runningAnimationCount > 0) { //当前有动画正在执行,不执行当前事件 return true; } if (islevel1Display) { long delay = 0; //隐藏三级菜单 if(islevel3Display) { AnimationUtils.rotateOutAnim(rl_level3, 0); islevel3Display = false; delay += 200; } //隐藏二级菜单 if(islevel2Display) { AnimationUtils.rotateOutAnim(rl_level2, delay); islevel2Display = false; delay += 200; } //隐藏二级菜单 AnimationUtils.rotateOutAnim(rl_level1, delay); } else { AnimationUtils.rotateInAnim(rl_level1, 0); AnimationUtils.rotateInAnim(rl_level2, 200); AnimationUtils.rotateInAnim(rl_level3, 400); islevel3Display = true; islevel2Display = true; } islevel1Display = !islevel1Display; return true; //消费当前事件 } return super.onKeyDown(keyCode, event); }
完整的AnimationUtils代码如下:
package com.wuyudong.youkumenu.utils; import android.view.animation.Animation; import android.view.animation.Animation.AnimationListener; import android.view.animation.RotateAnimation; import android.widget.RelativeLayout; public class AnimationUtils { public static int runningAnimationCount = 0; // 旋转出去的动画 public static void rotateOutAnim(RelativeLayout layout, long delay) { int childCount = layout.getChildCount(); //如果隐藏,则找到所有的子View,禁用 for (int i = 0; i < childCount; i++) { layout.getChildAt(i).setEnabled(false); } RotateAnimation ra = new RotateAnimation( 0f, -180f, //开始,结束的角度, 逆时针 Animation.RELATIVE_TO_SELF, 0.5f, //相对的x坐标点(指定旋转中心x值) Animation.RELATIVE_TO_SELF, 1.0f); //相对的y坐标点(指定旋转中心y值) ra.setDuration(500); ra.setFillAfter(true); //设置动画停留在结束位置 ra.setStartOffset(delay); //设置动画开始延时 ra.setAnimationListener(new MyAnimationListener());//添加监听 layout.startAnimation(ra); } // 旋转进来的动画 public static void rotateInAnim(RelativeLayout layout, long delay) { int childCount = layout.getChildCount(); //如果隐藏,则找到所有的子View,启用 for (int i = 0; i < childCount; i++) { layout.getChildAt(i).setEnabled(true); } RotateAnimation ra = new RotateAnimation( -180f, 0f, //开始,结束的角度, 顺时针 Animation.RELATIVE_TO_SELF, 0.5f, //相对的x坐标点(指定旋转中心x值) Animation.RELATIVE_TO_SELF, 1.0f); //相对的y坐标点(指定旋转中心y值) ra.setDuration(500); ra.setFillAfter(true); ra.setStartOffset(delay); //设置动画开始延时 ra.setAnimationListener(new MyAnimationListener()); layout.startAnimation(ra); } static class MyAnimationListener implements AnimationListener { @Override public void onAnimationStart(Animation animation) { runningAnimationCount++; } @Override public void onAnimationEnd(Animation animation) { runningAnimationCount--; } @Override public void onAnimationRepeat(Animation animation) { // TODO Auto-generated method stub } } }
完整的MainActivity代码如下:
package com.wuyudong.youkumenu; import com.wuyudong.youkumenu.utils.AnimationUtils; import android.os.Bundle; import android.app.Activity; import android.view.KeyEvent; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; import android.widget.RelativeLayout; public class MainActivity extends Activity implements OnClickListener { private RelativeLayout rl_level1; private RelativeLayout rl_level2; private RelativeLayout rl_level3; private boolean islevel1Display = true; private boolean islevel2Display = true; private boolean islevel3Display = true; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // 初始化控件 initViews(); } private void initViews() { // 添加点击事件 findViewById(R.id.ib_home).setOnClickListener(this); findViewById(R.id.ib_menu).setOnClickListener(this); rl_level1 = (RelativeLayout) findViewById(R.id.rl_level1); rl_level2 = (RelativeLayout) findViewById(R.id.rl_level2); rl_level3 = (RelativeLayout) findViewById(R.id.rl_level3); } @Override public boolean onKeyDown(int keyCode, KeyEvent event) { // keyCode 事件码 if(keyCode == KeyEvent.KEYCODE_MENU) { if (AnimationUtils.runningAnimationCount > 0) { //当前有动画正在执行,不执行当前事件 return true; } if (islevel1Display) { long delay = 0; //隐藏三级菜单 if(islevel3Display) { AnimationUtils.rotateOutAnim(rl_level3, 0); islevel3Display = false; delay += 200; } //隐藏二级菜单 if(islevel2Display) { AnimationUtils.rotateOutAnim(rl_level2, delay); islevel2Display = false; delay += 200; } //隐藏二级菜单 AnimationUtils.rotateOutAnim(rl_level1, delay); } else { AnimationUtils.rotateInAnim(rl_level1, 0); AnimationUtils.rotateInAnim(rl_level2, 200); AnimationUtils.rotateInAnim(rl_level3, 400); islevel3Display = true; islevel2Display = true; } islevel1Display = !islevel1Display; return true; //消费当前事件 } return super.onKeyDown(keyCode, event); } @Override public void onClick(View v) { if (AnimationUtils.runningAnimationCount > 0) { //当前有动画正在执行,不执行当前事件 return; } switch (v.getId()) { case R.id.ib_home: if (islevel2Display) { long delay = 200; // 如果当前三级菜单已经显示,先转出去 if (islevel3Display) { AnimationUtils.rotateOutAnim(rl_level3, 0); islevel3Display = false; delay += 200; } // 如果当前二级菜单已经显示,转出去 AnimationUtils.rotateOutAnim(rl_level2, delay); } else { // 如果当前二级菜单没有显示,转出来 AnimationUtils.rotateInAnim(rl_level2, 0); } islevel2Display = !islevel2Display; break; case R.id.ib_menu: if (islevel3Display) { // 如果当前三级菜单已经显示,转出去 AnimationUtils.rotateOutAnim(rl_level3, 0); } else { AnimationUtils.rotateInAnim(rl_level3, 0); } islevel3Display = !islevel3Display; break; default: break; } } }