鸿蒙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类型
725浏览 • 1回复 待解决
Resource类型如何转为String
201浏览 • 1回复 待解决
varchar类型怎么补有知道吗?
1056浏览 • 1回复 待解决
PostgreSQL json 类型查询
2144浏览 • 2回复 待解决
TS的Callback类型找不到怎么回事?
503浏览 • 1回复 待解决
安卓TextView鸿蒙安卓TextView鸿蒙
6690浏览 • 1回复 待解决
PolarDB包含哪些备份类型
1278浏览 • 1回复 待解决
mysql varchar类型的疑问
529浏览 • 1回复 待解决
MongoDB支持哪些数据类型
1677浏览 • 1回复 待解决
如何渲染gif类型的文件
5032浏览 • 1回复 待解决
Entity Framework Core jsonb 列类型
952浏览 • 1回复 待解决
taskPool的参数支持Function类型
198浏览 • 1回复 待解决
OB支持的索引类型都有哪些?
3948浏览 • 1回复 待解决
前端鸿蒙怎么过渡,需要学习Java吗
5873浏览 • 4回复 待解决
PolarDB 支持哪几种迁移类型
1034浏览 • 1回复 待解决
AndroidSdk如何转为鸿蒙SDK
7154浏览 • 1回复 待解决
如何选择PolarDB迁移类型和迁移对象?
1555浏览 • 1回复 待解决