鸿蒙int类型转为Color类型怎么转?

鸿蒙 类型转换
2021-04-23 11:10:24
浏览
收藏 0
回答 4
已解决
回答 4
按赞同
/
按时间
没用的喵叔
7

no bibi, show u the code!

//注意 public Color(int color)传入的是颜色int,不是resId
Color color = new Color(ResUtil.getColor(this, ResourceTable.Color_green));

public class ResUtil {
    //把resId转成color int
    public static int getColor(Context context, int resId) {
        try {
            context = getSateContext(context);
            return context.getResourceManager().getElement(resId).getColor();
//            return context.getColor(resId);//在tv上报错java.lang.NoSuchMethodError: No interface method getColor(I)
        } catch (Exception ignore) {
        }
        return GET_COLOR_STATE_FAILED;
    }

    //Fraction需要特殊处理
    public static Context getSateContext(Context context) {
        if (context instanceof Fraction) {
            Fraction fraction = (Fraction) context;
            return fraction.getFractionAbility().getContext();
        }
        return context;
    }
}
已于2021-4-24 11:05:54修改
分享
微博
QQ
微信
回复
2021-04-24 11:04:50
余映画
4

new的时候可以new Color(int,int,int)指定rgb值

分享
微博
QQ
微信
回复
2021-04-23 11:31:35
云中的雨
2

Color color = new Color(int a);

分享
微博
QQ
微信
回复
2021-04-23 17:44:17
白鹿白鹿
2

这里的int类型有两种

分享
微博
QQ
微信
回复
2021-04-23 19:45:58
相关问题
mysql数据类型使用int类型和string类型
745浏览 • 1回复 待解决
Resource类型如何转为String
221浏览 • 1回复 待解决
varchar类型怎么补有知道吗?
1091浏览 • 1回复 待解决
TS的Callback类型找不到怎么回事?
514浏览 • 1回复 待解决
PostgreSQL json 类型查询
2156浏览 • 2回复 待解决
mysql varchar类型的疑问
543浏览 • 1回复 待解决
PolarDB包含哪些备份类型
1293浏览 • 1回复 待解决
安卓TextView鸿蒙安卓TextView鸿蒙
6696浏览 • 1回复 待解决
Entity Framework Core jsonb 列类型
965浏览 • 1回复 待解决
如何渲染gif类型的文件
5044浏览 • 1回复 待解决
MongoDB支持哪些数据类型
1696浏览 • 1回复 待解决
PolarDB 支持哪几种迁移类型
1051浏览 • 1回复 待解决
AndroidSdk如何转为鸿蒙SDK
7188浏览 • 1回复 待解决
taskPool的参数支持Function类型
207浏览 • 1回复 待解决
OB支持的索引类型都有哪些?
3963浏览 • 1回复 待解决
创建PolarDB实例如何选择网络类型
1483浏览 • 1回复 待解决
Redis数据类型集合sets是什么?
1174浏览 • 1回复 待解决