ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

Android 中如何使用 enum / 枚举

2022-07-21 19:36:43  阅读:403  来源: 互联网

标签:color enum theme final int 枚举 avatar Android drawable


如何在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;
    }
}

上面这段代码很清晰,读起来朗朗上口啊 。意思应该都能看懂吧 。

标签:color,enum,theme,final,int,枚举,avatar,Android,drawable
来源: https://www.cnblogs.com/chenxibobo/p/16503144.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有