COLOR的定義是采用ARGB的方式,以int型數(shù)字來(lái)表示凉当。
Color.argb((int) 255, 32, 40, 50)
Alpha 是透明度首启,范圍: 0——255,位于int的高8位掺出;(0是完全透明徽千,255是完全不透明)
RED 是紅,范圍: 0——255汤锨,位于int的8-16位双抽;
Green 是綠,范圍: 0——255闲礼,位于int的16-24位牍汹;
Blue 是透明度,范圍: 0——255柬泽,位于int的低8位
argb()方法的參數(shù)依次為透明度,紅,綠,藍(lán)的大小慎菲,可以理解為濃度,這里組合起來(lái)的就是黑色锨并。
在程序中直接控制
setBackgroundColor(Color.argb((int) alpha, 32,40,50));
如果是直接在java代碼中定義露该。這里要注意哦。透明度不可以省去哦5谥蟆=庥住!就像這樣0xFF080287空盼,前面的0x代表16進(jìn)制书幕。
intmycolor = 0xff123456;
Button btn = (Button) findViewById(R.id.btn);
btn.setBackgroundColor(mycolor);
利用靜態(tài)方法argb來(lái)設(shè)置顏色:
Button btn = (Button) findViewById(R.id.btn);
btn.setBackgroundColor(Color.argb(0xff,0x00, 0x00,0x00));
這種方法必須使用0x開(kāi)頭,而不是用我們常用的#揽趾。值也必須用8位表示 台汇,不接受6位的顏色表示。分組一下0x|ff|ff00ff篱瞎,0x是代表顏色整數(shù)的標(biāo)記苟呐,ff是表示透明度,ff00ff表示RGB顏色值
6位(#000000)就是RGB值
8位(#1e000000)ARGB 頭兩位是透明度俐筋,00是完全透明牵素,ff是完全不透明,后6位是RGB值,
比較適中的透明度值是int color = Color.argb ( 127, 255, 0, 255 ); // 半透明的紫色
其中第一個(gè)參數(shù)表示透明澄者,0表示完全透明笆呆,255(ff)表示完全不透明;后三位分別代表RGB的值了粱挡。
在滑動(dòng)布局中標(biāo)題欄背景色漸變實(shí)現(xiàn)
private void initListeners() {
// 獲取頂部輪播圖高度后赠幕,設(shè)置滾動(dòng)監(jiān)聽(tīng)
ViewTreeObserver vto = banner.getViewTreeObserver();
vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
banner.getViewTreeObserver().removeGlobalOnLayoutListener(
this);
imageHeight = banner.getHeight();
scrollView.setScrollViewListener(new ObservableScrollview.ScrollViewListener() {
@Override
public void onScrollChanged(ObservableScrollview scrollView, int x, int y, int oldx, int oldy) {
// Log.i("TAG", "y--->" + y + " height-->" + height);
if (y <= 0) {
tv_title.setBackgroundColor(Color.argb((int) 0, 32, 40, 50));//AGB由相關(guān)工具獲得,或者美工提供
} else if (y > 0 && y <= imageHeight) {
float scale = (float) y / imageHeight;
float alpha = (255 * scale);
rl_title_bg.setBackgroundColor(Color.argb((int) alpha, 32,40,50));
} else {
// rl_title_bg.setBackgroundResource(R.mipmap.title_bg);
rl_title_bg.setBackgroundColor(Color.argb((int) 255, 32, 40, 50));
}
}
});
}
});
}