SoFunction
Updated on 2025-04-08

Example of Android programming to implement rounded rectangles with gradient effect

This article describes the implementation of rounded rectangles with gradient effects by Android programming. Share it for your reference, as follows:

/**
  * Rounded rectangle with gradient effect
  *
  * @description:
  * @author ldm
  * @date 2016-4-26 3:47:12 pm
  */
public class RoundRectsActivity extends Activity {
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    (savedInstanceState);
    setContentView(new RoundRectView(this));
  }
  private static class RoundRectView extends View {
    // The Rect class is mainly used to represent a rectangular area in the coordinate system    private Rect mRect;
    // GradientDrawable supports the use of gradient colors to draw graphics, which can usually be used as a Button or background graphics.    private GradientDrawable mDrawable;
    public RoundRectView(Context context) {
      super(context);
      setFocusable(true);
      initView();
    }
    /**
      * Initialize data
      *
      * @description:
      * @author ldm
      * @date 2016-4-26 3:56:06 pm
      */
    private void initView() {
      mRect = new Rect(0, 0, 240, 240);
      // Orientation specifies the direction of the gradient, int[]colors specifies that the color of the gradient is specified by the colors array, and each value in the array is a color.      mDrawable = new GradientDrawable(
          .TL_BR, new int[] { 0xFFFF0000,
              0xFF00FF00, 0xFF0000FF });
      // Set the shape of the Drawable to a rectangle      ();
      // Set the radius of the gradient      ((float) ((2) * 120));
    }
    /**
      * Set the four corners and round radii of the picture
      *
      * @description:
      * @author ldm
      * @date 2016-4-26 4:08:17 pm
      */
    static void setCornersRadii(GradientDrawable drawable, float r0,
        float r1, float r2, float r3) {
      // Set the circular radius of the four corners of the picture: 1 and 2 parameters represent the upper left corner, 3 and 4 represent the upper right corner, 5 and 6 represent the lower right corner, 7 and 8 represent the lower left corner      (new float[] { r0, r0, r1, r1, r2, r2, r3,
          r3 });
    }
    @Override
    protected void onDraw(Canvas canvas) {
      (mRect);
      float r = 16;
      ();
      (10, 10);//Picture translation      // Set gradient mode: linear gradient      (GradientDrawable.LINEAR_GRADIENT);
      setCornersRadii(mDrawable, r, r, 0, 0);
      (canvas);
      ();
      ();
      (10 + () + 10, 10);
      // Set gradient mode: diameter drawing gradient      (GradientDrawable.RADIAL_GRADIENT);
      setCornersRadii(mDrawable, 0, 0, r, r);
      (canvas);
      ();
      (0, () + 10);
      ();
      (10, 10);
      // Set the gradient mode: the color gradient direction is not ring-shaped, but swept through the fan-shaped shape with a point as the center.      (GradientDrawable.SWEEP_GRADIENT);
      setCornersRadii(mDrawable, 0, r, r, 0);
      (canvas);
      ();
      ();
      (10 + () + 10, 10);
      (GradientDrawable.LINEAR_GRADIENT);
      setCornersRadii(mDrawable, r, 0, 0, r);
      (canvas);
      ();
      (0, () + 10);
      ();
      (10, 10);
      (GradientDrawable.RADIAL_GRADIENT);
      setCornersRadii(mDrawable, r, 0, r, 0);
      (canvas);
      ();
      ();
      (10 + () + 10, 10);
      (GradientDrawable.SWEEP_GRADIENT);
      setCornersRadii(mDrawable, 0, r, 0, r);
      (canvas);
      ();
    }
  }
}

Open source code:/ldm520/ANDROID_API_DEMOS

Or click hereDownload this site

For more information about Android related content, please check out the topic of this site:Android layout layout tips summary》、《Android development introduction and advanced tutorial》、《Android debugging skills and solutions to common problems》、《Summary of the usage of basic Android components》、《Android View View Tips Summary"and"Android control usage summary

I hope this article will be helpful to everyone's Android programming design.