Android 獲取顏色RGB值


常用獲取顏色的方法

//#FFB6C1轉成Int
int color = Color.parseColor("#FFB6C1") //或者直接寫成 0xFFFFB6C1

//ARGB轉Int
int color = Color.argb(255, 255, 255, 255);

//獲取color.xml的顏色資源
int color = getResources().getColor(R.color.red);

獲取顏色RGB值

   public static String toHexEncoding(int color) {
        String R, G, B;
        StringBuffer sb = new StringBuffer();
        R = Integer.toHexString(Color.red(color));
        G = Integer.toHexString(Color.green(color));
        B = Integer.toHexString(Color.blue(color));
        //判斷獲取到的R,G,B值的長度 如果長度等於1 給R,G,B值的前邊添0
        R = R.length() == 1 ? "0" + R : R;
        G = G.length() == 1 ? "0" + G : G;
        B = B.length() == 1 ? "0" + B : B;
        sb.append("0x");
        sb.append(R);
        sb.append(G);
        sb.append(B);
        return sb.toString();
    }

顏色代碼在線轉換


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM