鸿蒙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类型
269浏览 • 1回复 待解决
varchar类型怎么补有知道吗?
716浏览 • 1回复 待解决
PostgreSQL json 类型查询
1720浏览 • 2回复 待解决
PolarDB包含哪些备份类型
967浏览 • 1回复 待解决
TS的Callback类型找不到怎么回事?
273浏览 • 1回复 待解决
如何渲染gif类型的文件
4710浏览 • 1回复 待解决
Entity Framework Core jsonb 列类型
603浏览 • 1回复 待解决
MongoDB支持哪些数据类型
1421浏览 • 1回复 待解决
PolarDB 支持哪几种迁移类型
858浏览 • 1回复 待解决
OB支持的索引类型都有哪些?
3496浏览 • 1回复 待解决
Redis数据类型集合sets是什么?
952浏览 • 1回复 待解决
创建PolarDB实例如何选择网络类型
1108浏览 • 1回复 待解决
Redis数据类型列表list是什么?
1121浏览 • 1回复 待解决
如何选择PolarDB迁移类型和迁移对象?
1179浏览 • 1回复 待解决
关于HAP包的entry和feature类型
7351浏览 • 1回复 待解决
p40 和phone类型不匹配
2774浏览 • 1回复 待解决
Redis数据类型散列hash是什么?
1010浏览 • 1回复 待解决
安卓TextView鸿蒙安卓TextView鸿蒙
6294浏览 • 1回复 待解决