欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android直播軟件搭建之實(shí)現(xiàn)背景顏色滑動漸變效果的詳細(xì)代碼

 更新時間:2021年09月02日 14:50:53   作者:云豹科技-蘇凌霄  
這篇文章主要介紹了Android直播軟件搭建之實(shí)現(xiàn)背景顏色滑動漸變效果的詳細(xì)代碼,代碼簡單易懂,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下

Android直播軟件搭建實(shí)現(xiàn)背景顏色滑動漸變效果的相關(guān)代碼

一、介紹一下GradientDrawable

GradientDrawable 支持漸變色的Drawable,與shapeDrawable是類似的,多了支持漸變色。
代碼中的GradientDrawable比xml中的shape下gradient屬性更加具體,shape下gradient屬性只支持三色階漸變,而GradientDrawable可以有更多的色階漸變(GradientDrawable在Android中便是shape標(biāo)簽的代碼實(shí)現(xiàn))。

二、實(shí)現(xiàn)

1、在布局中放入一個ScrollView,然后確保里面的內(nèi)容能夠達(dá)到滑動的效果。

2、獲取屏幕的高度

//獲取屏幕高度
private float getScreenHeight(){
DisplayMetrics metric = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metric);
int width = metric.widthPixels; // 屏幕寬度(像素)
int height = metric.heightPixels; // 屏幕高度(像素)
return height;
}

3、獲取控件高度(此案例為ScrollView中包裹的第一個子控件)。
4、設(shè)置顏色(為了方便顏色自接寫出來)

Orientation.TOP_BOTTOM為縱向,橫向改變參數(shù)即可
GradientDrawable aDrawable = new GradientDrawable(GradientDrawable.Orientation.TOP_BOTTOM,
new int[]{Color.parseColor("#ffffff"), Color.parseColor("#009966"),Color.parseColor("#00ff00")});
ll_base.setBackground(aDrawable);

5、獲取控件與屏幕高度(寬度)的比例,根據(jù)比例設(shè)置顏色個數(shù)

//得到控件的高度與屏幕高度的比例
private float getScreenHeightScale(int height){
return height/getScreenHeight();
}

三、源碼:

public class BaseActivity extends Activity {
private LinearLayout ll_base;
private int heights;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_base);
initView();
}


private void initView() {
ll_base = (LinearLayout) findViewById(R.id.ll_base);
}

 

@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
@Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);

heights = ll_base.getMeasuredHeight();
float coloramount=getScreenHeightScale(heights);
if (coloramount>=0&&coloramount<1.5f){
GradientDrawable aDrawable = new GradientDrawable(GradientDrawable.Orientation.TOP_BOTTOM,
new int[]{Color.parseColor("#ffffff"), Color.parseColor("#009966")});
ll_base.setBackground(aDrawable);
}
if (coloramount>=1.5f&&coloramount<3.0f){
GradientDrawable aDrawable = new GradientDrawable(GradientDrawable.Orientation.TOP_BOTTOM,
new int[]{Color.parseColor("#ffffff"), Color.parseColor("#009966"), Color.parseColor("#00ff00")});
ll_base.setBackground(aDrawable);
}
if (coloramount>=3.0f&&coloramount<4.5f){
GradientDrawable aDrawable = new GradientDrawable(GradientDrawable.Orientation.TOP_BOTTOM,
new int[]{Color.parseColor("#ffffff"), Color.parseColor("#009966"), Color.parseColor("#00ff00"),Color.parseColor("#000000")});
ll_base.setBackground(aDrawable);
}
// .................
}

//得到控件的高度與屏幕高度的比例
private float getScreenHeightScale(int height){
return height/getScreenHeight();
}
//獲取屏幕高度
private float getScreenHeight(){
DisplayMetrics metric = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metric);
int width = metric.widthPixels; // 屏幕寬度(像素)
int height = metric.heightPixels; // 屏幕高度(像素)
return height;
}
}

以上就是 Android直播軟件搭建實(shí)現(xiàn)背景顏色滑動漸變效果的相關(guān)代碼,更多內(nèi)容歡迎關(guān)注之后的文章

到此這篇關(guān)于Android直播軟件搭建之實(shí)現(xiàn)背景顏色滑動漸變效果的詳細(xì)代碼的文章就介紹到這了,更多相關(guān)android背景顏色滑動漸變內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論