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í)例代碼給大家介紹了Android仿銀行客戶簽名并且保存簽名的截圖文件并命名為本地時間,需要的朋友可以參考下2017-07-07
Android開發(fā)之PopupWindow創(chuàng)建彈窗、對話框的方法詳解
這篇文章主要介紹了Android開發(fā)之PopupWindow創(chuàng)建彈窗、對話框的方法,結(jié)合實(shí)例形式詳細(xì)分析了Android使用PopupWindow創(chuàng)建對話框相關(guān)操作技巧,需要的朋友可以參考下2019-03-03
Android中AndroidStudio&Kotlin安裝到運(yùn)行過程及常見問題匯總
這篇文章主要介紹了Android(AndroidStudio&Kotlin)安裝到運(yùn)行過程及常見問題匯總,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒借鑒價值,需要的朋友可以參考下2020-03-03
Android數(shù)據(jù)持久化之ContentProvider機(jī)制詳解
這篇文章主要介紹了Android數(shù)據(jù)持久化之ContentProvider機(jī)制,結(jié)合實(shí)例形式分析了ContentProvider機(jī)制的原理與相關(guān)使用技巧,需要的朋友可以參考下2017-05-05
Android實(shí)現(xiàn)自動變換大小的ViewPager
ViewPager使用適配器類將數(shù)據(jù)和view的處理分離,ViewPager的適配器叫PagerAdapter,這是一個抽象類,不能實(shí)例化,所以它有兩個子類:FragmentPagerAdapter 和 FragmentStatePagerAdapter,這兩個都是處理頁面為Fragment的情況2022-11-11
基于Viewpager2實(shí)現(xiàn)登錄注冊引導(dǎo)頁面
這篇文章主要為大家詳細(xì)介紹了基于Viewpager2實(shí)現(xiàn)登錄注冊引導(dǎo)頁面,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-09-09
Android音視頻開發(fā)之VideoView使用指南
VideoView組件內(nèi)部同樣是使用MediaPlayer+SurfaceView的形式控制MediaPlayer對視頻文件進(jìn)行播放,本文就來詳細(xì)講講它的使用方法,需要的可以參考一下2022-04-04
Android自定義View實(shí)現(xiàn)餅狀圖帶動畫效果
這篇文章主要為大家詳細(xì)介紹了Android自定義View實(shí)現(xiàn)餅狀圖帶動畫效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-12-12
如何使用Matrix對bitmap的旋轉(zhuǎn)與鏡像水平垂直翻轉(zhuǎn)
本篇文章是對使用Matrix對bitmap的旋轉(zhuǎn)與鏡像水平垂直翻轉(zhuǎn)進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-06-06
Android之自定義實(shí)現(xiàn)BaseAdapter(通用適配器二)
這篇文章主要為大家詳細(xì)介紹了Android之自定義實(shí)現(xiàn)BaseAdapter通用適配器第二篇,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-08-08

