Android Color 判斷色值小結
小菜我最近在處理主題色方面的問題,有個小需求是處理更改顏色,判斷色值等,稍稍整理了一下。
Color 大家都很熟悉,其組成方式是 RGB 紅綠藍三原色,小菜覺得可以按 ARGB 即 Alpha 透明度、Red 紅色、Green 綠 和 Blue 藍色來記。預設的 Alpha 為 FF/255 完全不透明,可不設定;若 Alpha 為 00/0 時,代表完全透明,則紅綠藍不起作用;而介於 00-FF/0-255 之間時,可以顯示出顏色不同的層次效果。
小菜的測試步驟如下:
在 color.xml 中定義幾個測試顏色值;
<?xml version="1.0" encoding="utf-8"?><resources> <color name="test_color1">#3F51B5</color> <color name="test_color2">#3F51B5</color> <color name="test_color3">#FF4081</color> <color name="test_color4">#40FF4081</color></resources>
小菜想是否可以直接用 R.color.XX 方式判斷色值,測試不相同,小菜理解獲取的是 R 的值;
// 日誌輸出Log.e("color1==" + R.color.test_color1, "color2==" + R.color.test_color2);// 結果color1==2131427410: color2==2131427411
小菜測試用 getResources().getColor(R.color.XX) 方式,結果是對的;
// 日誌輸出Log.e("test_color1==" + getResources().getColor(R.color.test_color1), "test_color2==" + getResources().getColor(R.color.test_color2));// 結果test_color1==-12627531: test_color2==-12627531
繼續測試,獲取某個控制元件背景色;
// 日誌輸出if (mColorTv1.getBackground() instanceof ColorDrawable) { ColorDrawable colordDrawable = (ColorDrawable) mColorTv1.getBackground(); int color = colordDrawable.getColor(); Log.e("color1==" + color, "test_color3==" + getResources().getColor(R.color.test_color3)); }// 結果color==-49023: test_color3==-49023
獲取方式都是可以的,只是這種方式看起來並不直接,轉成 16 進位制看起來會更自然;
String Color_16(int color) { StringBuffer stringBuffer = new StringBuffer(); stringBuffer.append("#"); stringBuffer.append(Integer.toHexString(Color.alpha(color))); stringBuffer.append(Integer.toHexString(Color.red(color))); stringBuffer.append(Integer.toHexString(Color.green(color))); stringBuffer.append(Integer.toHexString(Color.blue(color))); return stringBuffer.toString(); }String Color_16_NoAlpha(int color) { StringBuffer stringBuffer = new StringBuffer(); stringBuffer.append("#"); stringBuffer.append(Integer.toHexString(Color.red(color))); stringBuffer.append(Integer.toHexString(Color.green(color))); stringBuffer.append(Integer.toHexString(Color.blue(color))); return stringBuffer.toString(); }
// 日誌輸出if (mColorTv1.getBackground() instanceof ColorDrawable) { ColorDrawable colordDrawable = (ColorDrawable) mColorTv1.getBackground(); int color = colordDrawable.getColor(); Log.e("color==" + Color_16(color), "test_color3==" + Color_16(getResources().getColor(R.color.test_color3))); }// 結果color==#FF4081: test_color3==#FF4081
測試結果
以下是測試完整程式碼:
public class ColorActivity extends AppCompatActivity { Toolbar mToolbar; TextView mTitleTv, mColorTv1, mColorTv6; StringBuffer strBuffer = new StringBuffer(); @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); this.requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.activity_color); mToolbar = (Toolbar) this.findViewById(R.id.toolbar); mTitleTv = (TextView) this.findViewById(R.id.tv_toolbar_title); mTitleTv.setText("Color 色值判斷"); mColorTv1 = (TextView) this.findViewById(R.id.color_tv1); mColorTv6 = (TextView) this.findViewById(R.id.color_tv6); strBuffer.append("R.color.test_color1 方式 " + R.color.test_color1 + "n" + "R.color.test_color2 方式 " + R.color.test_color2 + "n"); strBuffer.append("getResources().getColor(R.color.test_color1) 方式 " + getResources().getColor(R.color.test_color1) + "n" + "getResources().getColor(R.color.test_color2) 方式 " + getResources().getColor(R.color.test_color2) + "n" + "getResources().getColor(R.color.test_color3) 方式 " + getResources().getColor(R.color.test_color3) + "n"); if (mToolbar.getBackground() instanceof ColorDrawable) { ColorDrawable colordDrawable = (ColorDrawable) mToolbar.getBackground(); int color = colordDrawable.getColor(); Log.e("=======color1==" + color, "=======color2==" + getResources().getColor(R.color.test_color3)); } if (mColorTv1.getBackground() instanceof ColorDrawable) { ColorDrawable colordDrawable = (ColorDrawable) mColorTv1.getBackground(); int color = colordDrawable.getColor(); strBuffer.append("ColorTv " + color + "n"); strBuffer.append("ColorTv 十六進位制 " + Color_16(color) + "n"); strBuffer.append("ColorTv 十六進位制(無透明度) " + Color_16_NoAlpha(color) + "n"); strBuffer.append("ColorTv 透明度 " + Color_Alpha(color) + " 紅 " + Color_Red(color) + " 綠 " + Color_Green(color) + " 藍 " + Color_Blue(color) + "n"); } strBuffer.append("R.color.test_color3 十六進位制 " + Color_16(getResources().getColor(R.color.test_color3)) + "n"); strBuffer.append("R.color.test_color3 十六進位制(無透明度) " + Color_16_NoAlpha(getResources().getColor(R.color.test_color3)) + "n"); strBuffer.append("R.color.test_color3 透明度 " + Color_Alpha(getResources().getColor(R.color.test_color3)) + " 紅 " + Color_Red(getResources().getColor(R.color.test_color3)) + " 綠 " + Color_Green(getResources().getColor(R.color.test_color3)) + " 藍 " + Color_Blue(getResources().getColor(R.color.test_color3)) + "n"); strBuffer.append("R.color.test_color4 十六進位制 " + Color_16(getResources().getColor(R.color.test_color4)) + "n"); strBuffer.append("R.color.test_color4 十六進位制(無透明度) " + Color_16_NoAlpha(getResources().getColor(R.color.test_color4)) + "n"); strBuffer.append("R.color.test_color4 透明度 " + Color_Alpha(getResources().getColor(R.color.test_color4)) + " 紅 " + Color_Red(getResources().getColor(R.color.test_color4)) + " 綠 " + Color_Green(getResources().getColor(R.color.test_color4)) + " 藍 " + Color_Blue(getResources().getColor(R.color.test_color4)) + "n"); mColorTv6.setText(strBuffer.toString()); } String Color_16(int color) { StringBuffer stringBuffer = new StringBuffer(); stringBuffer.append("#"); stringBuffer.append(Integer.toHexString(Color.alpha(color))); stringBuffer.append(Integer.toHexString(Color.red(color))); stringBuffer.append(Integer.toHexString(Color.green(color))); stringBuffer.append(Integer.toHexString(Color.blue(color))); return stringBuffer.toString(); } String Color_16_NoAlpha(int color) { StringBuffer stringBuffer = new StringBuffer(); stringBuffer.append("#"); stringBuffer.append(Integer.toHexString(Color.red(color))); stringBuffer.append(Integer.toHexString(Color.green(color))); stringBuffer.append(Integer.toHexString(Color.blue(color))); return stringBuffer.toString(); } String Color_Alpha(int color) { StringBuffer stringBuffer = new StringBuffer(); stringBuffer.append(Integer.toHexString(Color.alpha(color))); return stringBuffer.toString(); } String Color_Red(int color) { StringBuffer stringBuffer = new StringBuffer(); stringBuffer.append(Integer.toHexString(Color.red(color))); return stringBuffer.toString(); } String Color_Green(int color) { StringBuffer stringBuffer = new StringBuffer(); stringBuffer.append(Integer.toHexString(Color.green(color))); return stringBuffer.toString(); } String Color_Blue(int color) { StringBuffer stringBuffer = new StringBuffer(); stringBuffer.append(Integer.toHexString(Color.blue(color))); return stringBuffer.toString(); } public static int Color_ChangeAlpha(int color, int alpha) { int red = Color.red(color); int green = Color.green(color); int blue = Color.blue(color); return Color.argb(alpha, red, green, blue); } }
Tips:獲取控制元件背景色時要注意 backdround 是 color 還是 drawable,可先判斷是否是 ColorDrawable。
作者:老菜和尚
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/4560/viewspace-2822299/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- js 正則判斷顏色值,HEX轉RGB,HSL轉RGBJS
- Android斷點下載小結Android斷點
- 判斷物件值是否為空物件
- HTML input color 拾色器HTML
- 判斷值的資料型別資料型別
- javascript 布林型別值判斷JavaScript型別
- Color Wheel for Mac數字色輪Mac
- Color Wheel for Mac (數字色輪)Mac
- JavaScript判斷整數或者小數JavaScript
- 下拉選項,一個小判斷
- 判斷一個物件是否為空物件,判斷一個物件中是否有空值物件
- Activiti判斷流程是否結束
- Shell 條件判斷總結
- Android 判斷網路是否正常Android
- android判斷手機是否rootAndroid
- 如何判斷一個值是否等於NaNNaN
- golang中判斷兩個slice是否相等與判斷值下的 陣列是否相等Golang陣列
- Color UI for Mac(顏色選擇器)UIMac
- CSS-背景顏色|background-colorCSS
- Flutter Flame教程9 -- Color and Palette 顏色和調色盤Flutter
- JavaScript分支結構(判斷結構)使用教程JavaScript
- Node判斷檔案是否連結
- exp匯出操作結果判斷
- 智雲通CRM:如何判斷專案的價值?
- js如何判斷一個變數是否具有值JS變數
- js如何判斷一個變數是否有值JS變數
- android顏色對應的xml配置值,顏色表AndroidXML
- Python判斷海域有多少個小島Python
- 判斷單連結串列是否存在環,判斷兩個連結串列是否相交問題詳解
- 直播小程式原始碼,Swift 獲取漸變色顏色值原始碼Swift
- null(空值)小結Null
- android判斷狀態列是否可見Android
- Android 微信支付 微信是否安裝判斷Android
- Android判斷企業微信是否登入:Android
- JavaScript判斷ios還是android系統JavaScriptiOSAndroid
- android監聽TF卡插拔事件 判斷Android事件
- Android 判斷當前系統語言Android
- Android判斷服務是否在執行Android