安卓環形菜單(處理了菜單鍵和返回鍵+加入了動畫)
廢話不多說,先看:
package com.example.circlemenuofbottom.anim;import android.view.animation.RotateAnimation;import android.widget.RelativeLayout;/** * @author lzd * * QQ : 2622596982 * * email :www.bkjia.com * * QQ 交流群 :277599214 */public class MyAnimationUtils {/** * 出去的動畫---順時針出 * * @param view * 執行動畫的對象 */public static void animaOut(RelativeLayout view) {/* * RotateAnimation animation = new RotateAnimation(0, 180, * view.getWidth() / 2, view.getHeight()); animation.setDuration(500); * animation.setFillAfter(true); view.startAnimation(animation); */animaOut(view, 0);}/** * 出去的動畫---逆時針進 * * @param view * 執行動畫的對象 */public static void animaIn(RelativeLayout view) {/* * // 順時針進 // RotateAnimation animation = new RotateAnimation(180, // * 360,view.getWidth() / 2, view.getHeight()); // 逆時針進 RotateAnimation * animation = new RotateAnimation(-180, -360, view.getWidth() / * 2,view.getHeight()); animation.setDuration(500); * animation.setFillAfter(true); view.startAnimation(animation); */animaIn(view, 0);}/** * 出去的動畫---順時針出 * * @param view * 執行動畫的對象 * @param offSetTime * 延時執行的時間 */public static void animaOut(RelativeLayout view, int offSetTime) {RotateAnimation animation = new RotateAnimation(0, 180,view.getWidth() / 2, view.getHeight());animation.setDuration(500);animation.setStartOffset(offSetTime);animation.setFillAfter(true);view.startAnimation(animation);}public static void animaIn(RelativeLayout view, int offSetTime) {// 順時針進// RotateAnimation animation = new RotateAnimation(180,// 360,view.getWidth() / 2, view.getHeight());// 逆時針進RotateAnimation animation = new RotateAnimation(-180, -360,view.getWidth() / 2, view.getHeight());animation.setDuration(500);animation.setStartOffset(offSetTime);animation.setFillAfter(true);view.startAnimation(animation);}}
按鍵的處理思路:
/* * 監聽菜單鍵,當使用者點擊菜單鍵的時候,顯示和隱藏一級和二級菜單 * * 監聽返回鍵,再按一次退出程式 */private long exitTime = 0;@Overridepublic boolean onKeyDown(int keyCode, KeyEvent event) {// 菜單鍵的處理if(event.getAction() == KeyEvent.ACTION_DOWN && keyCode == KeyEvent.KEYCODE_MENU){// 如果 一級菜單顯示,隱藏 全部if(isShowLevel1){MyAnimationUtils.animaOut(level);isShowLevel1 = false;if(isShowLevel2){MyAnimationUtils.animaOut(level2,120);isShowLevel2 = false;if(isShowLevel3){MyAnimationUtils.animaOut(level3,120);isShowLevel3 = false;}}}else{// 如果一級菜單式隱藏的,顯示 一,二級菜單MyAnimationUtils.animaIn(level);MyAnimationUtils.animaIn(level2, 120);isShowLevel1 = true;isShowLevel2 = true;}}// 返回鍵if(event.getAction() == KeyEvent.ACTION_DOWN && keyCode == KeyEvent.KEYCODE_BACK){if(System.currentTimeMillis() - exitTime > 2000){Toast.makeText(this, 再按一次返回鍵退出, Toast.LENGTH_SHORT).show();exitTime = System.currentTimeMillis();}else{finish();System.exit(0);}return true;}return super.onKeyDown(keyCode, event);}