Android live broadcast software to achieve background color sliding gradient effect of the detailed code

  • 2021-12-13 17:15:48
  • OfStack

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

1. Introduce GradientDrawable under 1

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

2. Implement

1. Put an ScrollView in the layout, and then make sure that the contents inside can achieve the sliding effect.

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 (in this case, the first child control wrapped in ScrollView).
4. Set the color (for the convenience of writing the color itself)


Orientation.TOP_BOTTOM It is longitudinal, and the parameters can be changed horizontally 
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 control to 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:


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);
}
// .................
}

// Get the ratio of the height of the control to the height of the screen 
private float getScreenHeightScale(int height){
return height/getScreenHeight();
}
// 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;
}
}

The above is the relevant code of Android live broadcast software to realize the background color sliding gradient effect. Welcome to pay attention to the following articles for more content


Related articles: