鸿蒙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;
    }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
已于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类型
3466浏览 • 1回复 待解决
Resource类型如何转为String
2991浏览 • 1回复 待解决
HarmonyOS json对象map类型
1066浏览 • 1回复 待解决
HarmonyOS String如何Int
818浏览 • 1回复 待解决
HarmonyOS ColorColorFilter方式?
866浏览 • 1回复 待解决
HarmonyOS 怎么进行类型判断?
1667浏览 • 1回复 待解决
HarmonyOS 怎么识别设备类型
936浏览 • 1回复 待解决
HarmonyOS Resource怎么进行转换类型
1396浏览 • 1回复 待解决
Map类型怎么转换成Json string
3098浏览 • 1回复 待解决
怎么判断一个类型是stirng
1616浏览 • 1回复 待解决