When I see the title RoundRects of this example, I first introduced RoundRectShape. I opened the example and read the code, but it was implemented using GradientDrawable.
GradientDrawable supports the use of gradient color to draw graphics, usually can be used as a Button or background image.
GradientDrawable allows you to specify the type of the drawn image: LINE, OVAL, RECTANGLE or RING. The color gradient supports LINEAR_GRADIENT, RADIAL_GRADIENT, and SWEEP_GRADIENT.
The RECTANGLE (RECTANGLE) can also be used to set the four corners of the RECTANGLE to the rounded corner. The radius of each rounded corner can be set separately:
Public void setCornerRadii (float [] radii)
The radii array specifies the radius of the four rounded corners. You can specify [X_Radius, Y_Radius] for each corner. The order of the four rounded corners is top left, top right, bottom right, and bottom left. If X_Radius and Y_Radius are 0, the right angle is still displayed.
The direction of the color gradient is defined by GradientDrawable. Orientation as follows:
Let's take a look at the GradientDrawable constructor:
Public GradientDrawable (GradientDrawable. Orientation orientation, int [] colors)
Orientation specifies the gradient direction (the above eight types). The gradient color is specified by the colors array, and each value in the array is a color.
This example defines a gradient direction from top left to bottom right of the Group. The gradient colors are red, green, and blue.
[Java]
MDrawable = new GradientDrawable (GradientDrawable. Orientation. TL_BR,
New int [] {0xFFFF0000, 0xFF00FF00,
0xFF0000FF });
MDrawable = new GradientDrawable (GradientDrawable. Orientation. TL_BR,
New int [] {0xFFFF0000, 0xFF00FF00,
0xFF0000FF}); uses three gradient modes: Liner, Radial, and Sweep, and works with the Four Corner radius of the specified rectangle to display different GradientDrawable usage:
[Java]
MDrawable. setGradientType (GradientDrawable. LINEAR_GRADIENT );
SetCornerRadii (mDrawable, r, r, 0, 0 );
MDrawable. draw (canvas );
...
MDrawable. setGradientType (GradientDrawable. RADIAL_GRADIENT );
SetCornerRadii (mDrawable, 0, 0, r, r );
MDrawable. draw (canvas );
...
MDrawable. setGradientType (GradientDrawable. SWEEP_GRADIENT );
SetCornerRadii (mDrawable, 0, r, r, 0 );
MDrawable. draw (canvas );
...
MDrawable. setGradientType (GradientDrawable. LINEAR_GRADIENT );
SetCornerRadii (mDrawable, r, 0, 0, r );
MDrawable. draw (canvas );
...
MDrawable. setGradientType (GradientDrawable. RADIAL_GRADIENT );
SetCornerRadii (mDrawable, r, 0, r, 0 );
MDrawable. draw (canvas );
...
MDrawable. setGradientType (GradientDrawable. SWEEP_GRADIENT );
SetCornerRadii (mDrawable, 0, r, 0, r );
MDrawable. draw (canvas );
...
Static void setCornerRadii (GradientDrawable drawable,
Float r0, float r1, float r2, float r3 ){
Drawable. setCornerRadii (new float [] {r0, r0, r1, r1,
R2, r2, r3, r3 });
}
MDrawable. setGradientType (GradientDrawable. LINEAR_GRADIENT );
SetCornerRadii (mDrawable, r, r, 0, 0 );
MDrawable. draw (canvas );
...
MDrawable. setGradientType (GradientDrawable. RADIAL_GRADIENT );
SetCornerRadii (mDrawable, 0, 0, r, r );
MDrawable. draw (canvas );
...
MDrawable. setGradientType (GradientDrawable. SWEEP_GRADIENT );
SetCornerRadii (mDrawable, 0, r, r, 0 );
MDrawable. draw (canvas );
...
MDrawable. setGradientType (GradientDrawable. LINEAR_GRADIENT );
SetCornerRadii (mDrawable, r, 0, 0, r );
MDrawable. draw (canvas );
...
MDrawable. setGradientType (GradientDrawable. RADIAL_GRADIENT );
SetCornerRadii (mDrawable, r, 0, r, 0 );
MDrawable. draw (canvas );
...
MDrawable. setGradientType (GradientDrawable. SWEEP_GRADIENT );
SetCornerRadii (mDrawable, 0, r, 0, r );
MDrawable. draw (canvas );
...
Static void setCornerRadii (GradientDrawable drawable,
Float r0, float r1, float r2, float r3 ){
Drawable. setCornerRadii (new float [] {r0, r0, r1, r1,
R2, r2, r3, r3 });
}
Author: mapdigit