Android ApiDemos example (77): Graphics-& gt; RoundRects

Source: Internet
Author: User

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
 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.