将 ARGB 颜色字符串转换为 int ARGB int 格式 Color in android

Convert ARGB color string to int ARGB int format Color in android

我需要将 ARGB 颜色作为字符串从 1 Activity 传递到其他 activity。现在我需要将其他 activity 中的相同颜色字符串转换为 int 以传递给颜色方法中的参数。

这是我的实现:

Activity1

Intent i = new Intent(Activity1.java,Activity2.class);

...

i.putExtra("color1", "Color.argb(200, 69, 202, 252)");
i.putExtra("color2", "Color.argb(200, 48, 63, 159)");
startActivity(i);

现在,我在其他 class 中使用此字符串,即

Activity2

Intent i = getIntent();
String color1 = i.getStringExtra("color1");
String color2 = i.getStringExtra("color2");

...

tvContent.setBackground(getGradientColor(Color.parseColor(color1), Color.parseColor(color1)));

...

public static GradientDrawable getGradientColor(int color1, int color2) {
    int[] colors = new int[2];
    colors[0] = color1;
    colors[1] = color2;

    GradientDrawable gd = new GradientDrawable(
            GradientDrawable.Orientation.TL_BR, colors);

    gd.setGradientType(GradientDrawable.LINEAR_GRADIENT);

    gd.setCornerRadius(50);
    gd.setStroke(0, Color.WHITE);

    return gd;
}

错误

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.activity.SubListActivity}: java.lang.IllegalArgumentException: Unknown color
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2927)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2988)
    at android.app.ActivityThread.-wrap14(ActivityThread.java)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1631)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:154)
    at android.app.ActivityThread.main(ActivityThread.java:6682)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1520)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1410)
 Caused by: java.lang.IllegalArgumentException: Unknown color
    at android.graphics.Color.parseColor(Color.java:163)
    at com.gstechnovos.mathsploy.activity.SubListActivity.onCreate(SubListActivity.java:101)
    at android.app.Activity.performCreate(Activity.java:6942)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1126)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2880)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2988) 
    at android.app.ActivityThread.-wrap14(ActivityThread.java) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1631) 
    at android.os.Handler.dispatchMessage(Handler.java:102) 
    at android.os.Looper.loop(Looper.java:154) 
    at android.app.ActivityThread.main(ActivityThread.java:6682) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1520) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1410) 

提前致谢。

您将无效的 String 传递给 Color.parseColor(String):

Supported formats are: #RRGGBB or #AARRGGBB

我建议从一开始就对颜色整数进行操作。从 putExtras 中删除引号以创建 argb int 代替:

// Activity1
i.putExtra("color1", Color.argb(200, 69, 202, 252));
i.putExtra("color2", Color.argb(200, 48, 63, 159));
startActivity(i);

然后检索并使用秒 activity:

中的整数
Intent i = getIntent();
int color1 = i.getIntExtra("color1");
int color2 = i.getIntExtra("color2");

...

tvContent.setBackground(getGradientColor(color1, color2));

...