• Android 中如何使用 enum / 枚举


    如何在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
  • 相关阅读:
    WCF客户端链接服务超时客户端close
    C# byte数组常用扩展浅析(转)
    代码生成相关工具及技术
    已处理证书链,但是在不受信任提供程序信任的根证书中终止。
    清理SQL Server数据库日志的两种方法
    开源框架项目列表
    SQL Server数据库文件恢复技术
    VS2008找不到导出模板
    jquery 学习笔记(二)
    方法的参数的默认值设置
  • 原文地址:https://www.cnblogs.com/Free-Thinker/p/6952620.html
Copyright © 2020-2023  润新知