本篇內容介紹了“Android怎么實現背景顏色滑動漸變效果”的有關知識,在實際案例的操作過程中,不少人都會遇到這樣的困境,接下來就讓小編帶領大家學習一下如何處理這些情況吧!希望大家仔細閱讀,能夠學有所成!
前言
一、介紹一下GradientDrawable
二、實現
三、源碼:
總結
今天和朋友聊到這個功能,剛開始的想法是自定義view,如何進行滑動監聽,經過一列操作完成效果后,發現了一個賊簡單的實現效果,如下(老規矩后面有可運行代碼)。
效果圖:

GradientDrawable 支持漸變色的Drawable,與shapeDrawable是類似的,多了支持漸變色。
代碼中的GradientDrawable比xml中的shape下gradient屬性更加具體,shape下gradient屬性只支持三色階漸變,而GradientDrawable可以有更多的色階漸變(GradientDrawable在Android中便是shape標簽的代碼實現)。
1、在布局中放入一個ScrollView,然后確保里面的內容能夠達到滑動的效果。

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、設置顏色(為了方便顏色自接寫出來)
GradientDrawable aDrawable = new GradientDrawable(GradientDrawable.Orientation.TOP_BOTTOM,
new int[]{Color.parseColor("#ffffff"), Color.parseColor("#009966"),Color.parseColor("#00ff00")});
ll_base.setBackground(aDrawable);5、獲取控件與屏幕高度(寬度)的比例,根據比例設置顏色個數
//得到控件的高度與屏幕高度的比例
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怎么實現背景顏色滑動漸變效果”的內容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業相關的知識可以關注億速云網站,小編將為大家輸出更多高質量的實用文章!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。