Get the App
SLTechnology News&Howtos  ›  Development  › 

How to realize the sliding gradient effect of background Color by Android

Shulou Source: shulou.com Published: 2022-06-03 19:37:24 11月12日 Update

This article introduces the knowledge of "how to achieve the background color sliding gradient effect by Android". In the actual case operation, many people will encounter such a dilemma. Next, let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Catalogue

Preface

First, introduce GradientDrawable.

II. Realization

3. Source code:

Summary

Preface

Today, I talked about this feature with my friends. The initial idea was to customize view and how to perform sliding listening. After completing a list of operations, I found a simple implementation effect of a thief, as follows (there is runnable code behind the old rules).

Effect picture:

First, introduce GradientDrawable.

GradientDrawable supports gradient Drawable, which is similar to shapeDrawable, with more support for gradient.

The GradientDrawable in the code is more specific than the gradient attribute under shape in xml, the gradient attribute under shape only supports trichromatic gradients, while GradientDrawable can have more gradients (GradientDrawable is the code implementation of the shape tag in Android).

II. Realization

1, put a ScrollView in the layout, and then make sure that the content inside can achieve the effect of sliding.

2. Get the height of the screen

/ / get screen height private float getScreenHeight () {DisplayMetrics metric = new DisplayMetrics (); getWindowManager () .getDefaultDisplay () .getMetrics (metric); int width = metric.widthPixels; / / screen width (pixels) int height = metric.heightPixels; / / screen height (pixels) return height;}

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

4. Set the color (write it in order to facilitate the color)

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

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

/ / get the ratio of the height of the control to the height of the screen private float getScreenHeightScale (int height) {return height/getScreenHeight (); 3. 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 () {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&&coloramount=3.0f&&coloramount

Tags: Height screen effects color controls pixels scale code content width more support background preface attributes source code knowledge discoloration practicality learning Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Docker Apple Shulou Technology NVidia Huawei