SoFunction
Updated on 2025-03-08

Detailed code for realizing background color sliding gradient effect by building Android live broadcast software

Android live broadcast software builds related codes to realize the background color sliding gradient effect

1. Introduction to GradientDrawable

GradientDrawable supports gradient color Drawable, which is similar to shapeDrawable, and supports gradient color.
The GradientDrawable in the code is more specific than the Gradient attribute under shape in xml. The Gradient attribute under shape only supports three-color level gradients, while GradientDrawable can have more color level gradients (GradientDrawable is the code implementation of shape tags in Android).

2. Realization

1. Put a ScrollView into the layout and then make sure that the content inside can achieve the sliding effect.

2. Get the height of the screen

//Get screen heightprivate float getScreenHeight(){
DisplayMetrics metric = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metric);
int width = ; // Screen width (pixels)int height = ; // Screen height (pixels)return height;
}

3. Get the control height (this case is the first child control wrapped in ScrollView).
4. Set the color (to facilitate the color to be written out)

Orientation.TOP_BOTTOMFor vertical,Just change the parameters horizontally
GradientDrawable aDrawable = new GradientDrawable(.TOP_BOTTOM,
new int[]{("#ffffff"), ("#009966"),("#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 screenprivate 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) {
(savedInstanceState);
setContentView(.activity_base);
initView();
}


private void initView() {
ll_base = (LinearLayout) findViewById(.ll_base);
}

 

@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
@Override
public void onWindowFocusChanged(boolean hasFocus) {
(hasFocus);

heights = ll_base.getMeasuredHeight();
float coloramount=getScreenHeightScale(heights);
if (coloramount>=0&&coloramount<1.5f){
GradientDrawable aDrawable = new GradientDrawable(.TOP_BOTTOM,
new int[]{("#ffffff"), ("#009966")});
ll_base.setBackground(aDrawable);
}
if (coloramount>=1.5f&&coloramount<3.0f){
GradientDrawable aDrawable = new GradientDrawable(.TOP_BOTTOM,
new int[]{("#ffffff"), ("#009966"), ("#00ff00")});
ll_base.setBackground(aDrawable);
}
if (coloramount>=3.0f&&coloramount<4.5f){
GradientDrawable aDrawable = new GradientDrawable(.TOP_BOTTOM,
new int[]{("#ffffff"), ("#009966"), ("#00ff00"),("#000000")});
ll_base.setBackground(aDrawable);
}
// .................
}

//Get the ratio of the height of the control to the height of the screenprivate float getScreenHeightScale(int height){
return height/getScreenHeight();
}
//Get screen heightprivate float getScreenHeight(){
DisplayMetrics metric = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metric);
int width = ; // Screen width (pixels)int height = ; // Screen height (pixels)return height;
}
}

The above is the relevant code for building Android live broadcast software to achieve the background color sliding gradient effect. For more content, please follow the articles afterwards.

This is the article about the detailed code for the construction of Android live broadcast software to realize the background color sliding gradient effect. For more related content on Android background color sliding gradient, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!