This is the 28th day of my participation in the August Text Challenge.More challenges in August

Series of articles

Android background color slide gradient effect


preface

Today, I talked with my friends about this function. At first, I had the idea of customizing the view and how to slide monitor it. After completing the effect through a series of operations, I found a simple implementation effect as follows (there is runnable code behind the old rules).

Effect:


GradientDrawable

GradientDrawable supports GradientDrawable, similar to shapeDrawable, with additional support for GradientDrawable.

GradientDrawable in code is more specific than the Shape gradient attribute in XML, which only supports tricolor gradients, GradientDrawable allows for more GradientDrawable (which in Android is a coded implementation of the Shape tag).

Second, the implementation

1. Place a ScrollView in the layout and make sure the contents inside slide.

2. Get the screen height

    // Get the screen height
     private float getScreenHeight(a){
         DisplayMetrics metric = new DisplayMetrics();
         getWindowManager().getDefaultDisplay().getMetrics(metric);
         int width = metric.widthPixels;     // Screen width (pixels)
         int height = metric.heightPixels;   // Screen height (pixels)
         return  height;
     }
Copy the code

3. Get the height of the control (in this case, the first child control wrapped in ScrollView).

4, set the color (to facilitate the color self-write)

Orientation.TOP_BOTTOM = “vertical” and change the parameter “horizontal”

  GradientDrawable aDrawable = new GradientDrawable(GradientDrawable.Orientation.TOP_BOTTOM,
                    new int[]{Color.parseColor("#ffffff"), Color.parseColor("# 009966"),Color.parseColor("#00ff00")});
            ll_base.setBackground(aDrawable);
Copy the code

5, get control and screen height (width) ratio, set the number of colors according to the ratio

   // Get the ratio of control height to screen height
    private float getScreenHeightScale(int height){
        return height/getScreenHeight();
    }
Copy the code

Three, source code:

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(a) {
        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.5 f){
            GradientDrawable aDrawable = new GradientDrawable(GradientDrawable.Orientation.TOP_BOTTOM,
                    new int[]{Color.parseColor("#ffffff"), Color.parseColor("# 009966")});
            ll_base.setBackground(aDrawable);
        }
        if (coloramount>=1.5 f&&coloramount<3.0 f){
            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.0 f&&coloramount<4.5 f){
            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);
        }
       / /...
    }

    // Get the ratio of control height to screen height
    private float getScreenHeightScale(int height){
        return height/getScreenHeight();
    }
     // Get the screen height
     private float getScreenHeight(a){
         DisplayMetrics metric = new DisplayMetrics();
         getWindowManager().getDefaultDisplay().getMetrics(metric);
         int width = metric.widthPixels;     // Screen width (pixels)
         int height = metric.heightPixels;   // Screen height (pixels)
         returnheight; }}Copy the code