如何在Android開發中合理的使用enum 歡迎大家訪問我的Github開源庫,這里有好玩的App源碼,想和大家分享。https://github.com/ChoicesWang 轉載請注明:http://blog.csdn.net/zezeviyao/article/details/46695367 我們都知道,enum最早出現在C、C++、C#中。 而在 JDK1.5之后,Java中也引入了enum。當時使用的並不廣泛。后來我在Android項目開發中,嘗試大量使用enum,結果其他同事告訴我說:Android中使用enum是會影響性能的! 真的會影響性能嗎? 同事告訴我。他在Android文檔中看到過這樣的提示:Avoid Enums Where You Only Need Ints 。基本意思就是,在你只需要int(整形)的時候,不要使用枚舉,這會影響你的性能。 那果真這樣嗎,我也Google了相關問題,看到了不同的答案。就在我也猶豫不決的時候,在stackoverflow中找到了這樣的答案: http://stackoverflow.com/questions/5143256/why-was-avoid-enums-where-you-only-need-ints-removed-from-androids-performanc 所有,隨着虛擬機的提升,使用枚舉已經不再會影響性能。 什么是枚舉 如果你還不知道枚舉是什么,我建議你先看看下面兩篇文章: java 枚舉使用詳情 java enum(枚舉)使用詳解 + 總結 如何在Android中使用枚舉 我們先看一段代碼: /** * The available avatars with their corresponding drawable resource ids. * 阿凡達,其實就只頭像的資源ID */ public enum Avatar { //從1到16,一共16個不同的頭像 ONE(R.drawable.avatar_1), TWO(R.drawable.avatar_2), THREE(R.drawable.avatar_3), FOUR(R.drawable.avatar_4), FIVE(R.drawable.avatar_5), SIX(R.drawable.avatar_6), SEVEN(R.drawable.avatar_7), EIGHT(R.drawable.avatar_8), NINE(R.drawable.avatar_9), TEN(R.drawable.avatar_10), ELEVEN(R.drawable.avatar_11), TWELVE(R.drawable.avatar_12), THIRTEEN(R.drawable.avatar_13), FOURTEEN(R.drawable.avatar_14), FIFTEEN(R.drawable.avatar_15), SIXTEEN(R.drawable.avatar_16); //在枚舉中定義常量 private static final String TAG = "Avatar"; private final int mResId; //構造方法 Avatar(@DrawableRes final int resId) { mResId = resId; } //獲取頭像圖片ID @DrawableRes public int getDrawableId() { return mResId; } // ordinal 順序 public String getNameForAccessibility() { return TAG + " " + ordinal() + 1; } } 其中,我們看到,使用枚舉定了16個頭像常量。這些常量在程序中不會被改變。接下來就看他的幾個基本使用方法。 //直接用來初始化變量 private Avatar mSelectedAvatar = Avatar.ONE; //初始化數組 private static final Avatar[] mAvatars = Avatar.values(); //獲取枚舉的名字 String name = Avatar.ONE.name(); //通過String轉化為枚舉 Avatar avatar = Avatar.valueOf(name); 再舉一個Android中常用的例子 做過Android5.0 Material Design的同學都知道。Theme中需要定義不同的顏色。如果成套的管理這些主題顏色呢? 看代碼 //主題 public enum Theme { topeka(R.color.topeka_primary, R.color.theme_blue_background, R.color.theme_blue_text, R.style.Topeka), blue(R.color.theme_blue_primary, R.color.theme_blue_background, R.color.theme_blue_text, R.style.Topeka_Blue), green(R.color.theme_green_primary, R.color.theme_green_background, R.color.theme_green_text, R.style.Topeka_Green), purple(R.color.theme_purple_primary, R.color.theme_purple_background, R.color.theme_purple_text, R.style.Topeka_Purple), red(R.color.theme_red_primary, R.color.theme_red_background, R.color.theme_red_text, R.style.Topeka_Red), yellow(R.color.theme_yellow_primary, R.color.theme_yellow_background, R.color.theme_yellow_text, R.style.Topeka_Yellow); private final int mColorPrimaryId; //主題色 private final int mWindowBackgroundId; //窗口背景色 private final int mTextColorPrimaryId; //字體色 private final int mStyleId; //樣式ID //構造方法 Theme(final int colorPrimaryId, final int windowBackgroundId, final int textColorPrimaryId, final int styleId) { mColorPrimaryId = colorPrimaryId; mWindowBackgroundId = windowBackgroundId; mTextColorPrimaryId = textColorPrimaryId; mStyleId = styleId; } @ColorRes public int getTextPrimaryColor() { return mTextColorPrimaryId; } @ColorRes public int getWindowBackgroundColor() { return mWindowBackgroundId; } @ColorRes public int getPrimaryColor() { return mColorPrimaryId; } @StyleRes public int getStyleId() { return mStyleId; } } 上面這段代碼很清晰,讀起來朗朗上口啊 。意思應該都能看懂吧 。 轉載請注明:http://blog.csdn.net/zezeviyao/article/details/46695367
