Convert hex color value ( #ffffff ) to integer value

前端 未结 9 1002
孤城傲影
孤城傲影 2020-12-13 01:26

I am receiving hex color values from a server (in this form, #xxxxxx , example #000000 for black)

How do I convert this to an integer value

相关标签:
9条回答
  • 2020-12-13 02:10

    Based on CQM's answer and on ovokerie-ogbeta's answer to another question I've come up with this solution:

    if (colorAsString.length() == 4) { // #XXX
        colorAsString = colorAsString.replaceAll("#([0-9a-fA-F])([0-9a-fA-F])([0-9a-fA-F])", "#$1$1$2$2$3$3");
    }
    
    int color = Color.parseColor(colorAsString);
    
    0 讨论(0)
  • 2020-12-13 02:10

    Try this, create drawable in your resource...

    <shape xmlns:android="http://schemas.android.com/apk/res/android" >
        <solid android:color="@color/white"/>
        <size android:height="20dp"
            android:width="20dp"/>
    </shape>
    

    then use...

     Drawable mDrawable = getActivity().getResources().getDrawable(R.drawable.bg_rectangle_multicolor);
    mDrawable.setColorFilter(Color.parseColor(color), PorterDuff.Mode.SRC_IN);
    mView1.setBackground(mDrawable);
    

    with color... "#FFFFFF"

    if the color is transparent use... setAlpha

    mView1.setAlpha(x); with x float 0-1 Ej (0.9f)

    Good Luck

    0 讨论(0)
  • 2020-12-13 02:11

    The real answer is to use:

    Color.parseColor(myPassedColor) in Android, myPassedColor being the hex value like #000 or #000000 or #00000000.

    However, this function does not support shorthand hex values such as #000.

    0 讨论(0)
提交回复
热议问题