Android直播軟件搭建之實(shí)現(xiàn)背景顏色滑動漸變效果的詳細(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)文章希望大家以后多多支持腳本之家!
- Android 滑動Scrollview標(biāo)題欄漸變效果(仿京東toolbar)
- Android之scrollview滑動使標(biāo)題欄漸變背景色的實(shí)例代碼
- Android開發(fā)實(shí)現(xiàn)標(biāo)題隨scrollview滑動變色的方法詳解
- Android ListView滑動改變標(biāo)題欄背景漸變效果
- Android 頂部標(biāo)題欄隨滑動時的漸變隱藏和漸變顯示效果
- Android 中實(shí)現(xiàn)ListView滑動隱藏標(biāo)題欄的代碼
- Android ScrollView滑動實(shí)現(xiàn)仿QQ空間標(biāo)題欄漸變
- Android開發(fā)之滑動圖片輪播標(biāo)題焦點(diǎn)
- Android實(shí)現(xiàn)背景顏色滑動漸變效果的全過程
- Android?App頁面滑動標(biāo)題欄顏色漸變詳解
相關(guān)文章
android實(shí)現(xiàn)獲取正在運(yùn)行的應(yīng)用程序
android如何獲取正在運(yùn)行的應(yīng)用程序,因?yàn)樵趂ramework中想添加這個功能,所以寫了個appliction來實(shí)現(xiàn)一下獲取正在運(yùn)行的應(yīng)用程序2013-01-01Android實(shí)現(xiàn)底部滾輪式選擇彈跳框
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)底部滾輪式選擇彈跳框,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-08-08圣誕節(jié),寫個程序練練手————Android 全界面懸浮按鈕實(shí)現(xiàn)
這篇文章主要介紹了圣誕節(jié),寫個程序練練手————Android 全界面懸浮按鈕實(shí)現(xiàn)的相關(guān)資料,需要的朋友可以參考下2015-12-12Android編程實(shí)現(xiàn)禁止StatusBar下拉的方法
這篇文章主要介紹了Android編程實(shí)現(xiàn)禁止StatusBar下拉的方法,涉及Android StatusBarManager相關(guān)屬性控制操作技巧,需要的朋友可以參考下2017-08-08Android Drawable和Bitmap的轉(zhuǎn)換實(shí)例詳解
這篇文章主要介紹了Android Drawable和Bitmap的轉(zhuǎn)換實(shí)例詳解的相關(guān)資料,需要的朋友可以參考下2017-05-05