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

Android實(shí)現(xiàn)背景顏色滑動(dòng)漸變效果的全過(guò)程

 更新時(shí)間:2021年09月02日 16:27:41   作者:計(jì)蒙不吃魚  
在Android開(kāi)發(fā)中,經(jīng)常需要設(shè)置控件的背景顏色或者圖片的src顏色,下面這篇文章主要給大家介紹了關(guān)于Android實(shí)現(xiàn)背景顏色滑動(dòng)漸變效果的相關(guān)資料,本文通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友可以參考下

前言

今天和朋友聊到這個(gè)功能,剛開(kāi)始的想法是自定義view,如何進(jìn)行滑動(dòng)監(jiān)聽(tīng),經(jīng)過(guò)一列操作完成效果后,發(fā)現(xiàn)了一個(gè)賊簡(jiǎn)單的實(shí)現(xiàn)效果,如下(老規(guī)矩后面有可運(yùn)行代碼)。

效果圖:

一、介紹一下GradientDrawable

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

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

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

2、獲取屏幕的高度

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

3、獲取控件高度(此案例為ScrollView中包裹的第一個(gè)子控件)。

4、設(shè)置顏色(為了方便顏色自接寫出來(lái))

  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è)置顏色個(gè)數(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;
     }
}

總結(jié)

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

相關(guān)文章

最新評(píng)論