Custom Controls (shadows, gradients)

Source: Internet
Author: User
Tags drawtext

First, the Shadow:
The Paint class defines a method named Setshadowlayer:
Public void Setshadowlayer (float radius,float dx,float dy,int shadowcolor), the parameters are as follows:
Radius: Shadow Radius
Offset of dx:x direction shadow
Offset of dy:y direction shadow
Shadowcolor: The color of the shadow
When the shadow LAYER is shaded, there are two types of Shaderlayer: View.layer_type_software and View.layer_type_hardware, the default type of layer is Layer_type_hardware, But the shadow can only be
View.layer_type_software environment, so we need to invoke the public of the View classvoidThe Setlayertype (int layertype,paint paint) method Specifies the type of layer for the Paint object:
Setlayertype (View.layer_type_software, Paint). If you want to remove the shadow, set radius to 0.

public class Shaderview extends View {    private paint paint;    Public Shaderview (context context, AttributeSet Attrs) {        Super (context, attrs);    }    Public Shaderview (context context, AttributeSet attrs, int defstyleattr) {        Super (context, attrs, defstyleattr);    }    @Override    protected void OnDraw (canvas canvas) {        super.ondraw (canvas);        Paint = new Paint (paint.anti_alias_flag);        Paint.setstrokewidth (5);        Paint.settextsize (+);        This.setlayertype (view.layer_type_software,paint);//set to software to achieve Shadow        Paint.setshadowlayer (10,1,1, Color.Blue);//The offset is very small        canvas.drawtext ("Android Development", 100,100,paint);        Canvas.restore ();        Paint.setshadowlayer (10,5,5,;//If you want to remove the shadow, set radius to 0 to        canvas.drawtext ("Android, hello", 100,400, paint);}    }

Second, the gradient
The gradient types are:
1. Linear gradient: LinearGradient
Linear gradient (LinearGradient) fills the drawing area with a gradient color based on the specified angle, color, and mode. We must define two points (x0, y0) and (x1, y1), and the direction of the gradient is perpendicular to the connection of the two points,
The lineargradient is constructed as follows:
Public LinearGradient (float x0,float y0,float x1,float y1,int color0,int color1,tilemode Tile): This method is used for two colors of the gradient, each parameter meaning is as follows:
X0, y0: coordinates (x0, y0) used to determine the first point in a linear direction;
x1, y1: coordinates for determining the second point in a linear direction (x1, y1);
COLOR0: First color;
Color1: Second color;
Tile: Gradient Mode
Public LinearGradient (float x0,float y0,float x1,floatY1,int colors[],float positions[],tilemode Tile), this is a more powerful construction method, let's look at the function of this constructor method parameter:
X0, y0: coordinates of the starting point
X1, y1: The coordinates of the terminating point
Colors: Multiple Colors
Positions: Position of color (scale)
Tilemode: Gradient Mode
Parameters colors and positions are arrays that specify multiple colors, which are used to specify the starting scale position for each color. The number of elements in the positions array is the same as the colors, and is a value from 0 to 1, [0,1] is a critical section,
If less than 0 is treated when 0 is processed, if greater than 1 is treated as 1. If the drawing area and the gradient area are the same size, colors contains three colors: red, yellow, green, and in the gradient region, the starting scale position of the three colors is 0, 0.3,

2. Radial gradient: radialgradient
A radial gradient is centered around a specified point, with a gradient color surrounding the perimeter, and as with a linear gradient, two or more colors are supported.
<1>public Radialgradient (float X,floatY,floatRadiusint Color0,intColor1,Tilemode tile), the construction method supports two colors, the following is the function of the parameter:
X, y: center point coordinates
Radius: Gradient Radius
COLOR0: Starting Color
Color1: End Color
Tilemode: Gradient Mode
<2>public Radialgradient (float X,floatY,floatRadiusint Colors[],float positions[],Tilemodetile), this construction method supports gradients of 3 or more than 3 colors, and the functions of each parameter are as follows:
X, y: center point coordinates
Radius: Gradient Radius
Colors: Multiple Colors
Positions: Position of color (scale)
Tilemode: Gradient Mode
Constructor 2 usage as above;

3. Scan gradient: sweepgradient
Sweepgradient similar to the radar sweep effect in military films, fixed the center of the circle, and assumed the radius as a tangible and rotating
The gradient color drawn for one week. Sweepgradient defines two main construction methods:
<1>public sweepgradient (float cx,float cy,int color0,int color1)
Supports two colors of the scan gradient, the role of the parameters are as follows:
CX, CY: DOT coordinates;
COLOR0: Starting color;
Color1: End color.
<2>public sweepgradient (float cx,float cy,int colors[],float positions[])
supports a variety of colors of the scanning gradient, the role of the parameters are as follows:
CX, CY: DOT coordinates;
Colors: multiple colors;
Positions: the position (scale) of the color.

4. Bitmap gradient: Bitmapshader
The bitmap gradient is actually the specified bitmap as the background in the drawing drawing, and if the shape is smaller than the bitmap, it is tiled by the gradient mode, Tilemode.clamp mode is uneven, tilemode.repeat mode represents tiling, Tilemode.mirror
Patterns also represent tiling, but interlaced bitmaps are mirrored to each other, in the opposite direction. You can specify the gradient mode in both horizontal and vertical two directions.
Construction Method:
Public Bitmapshader (Bitmap bitmap,tilemode tilex,tilemode Tiley), the parameters are as follows:
Bitmap: Bitmap;
Repetition mode of tilex:x direction;
The repeating mode of the tiley:y direction.

5. Mixed gradient: Composeshader
Blending gradient Composeshader is a more complex gradient that is obtained after two different gradients are calculated through a bitmap.
Construction Method:
<1>public Composeshader (Shader shadera,shader shaderb,xfermode mode)
<2>public Composeshader (Shader shadera,shader shaderb,mode Mode)
Shadera and Shaderb are two gradient objects, mode is a bitmap operation type, 16 operation modes 5-15
As shown:

In the code, Shadera is a bitmap gradient, Shaderb is a linear gradient, and according to Mode.src_atop's arithmetic rules,
Shadera will show all (here is a small robot), Shaderb only show the intersection of the two and on the top (top).

Gradient Effect Code:

public class Radialgradientview extends View {private int color[]={,,};    private float position[]={0,0.5f,1};    Public Radialgradientview (context context, AttributeSet Attrs) {Super (context, attrs); } public Radialgradientview (context context, AttributeSet attrs, int defstyleattr) {Super (context, Attrs, defst    YLEATTR);        } @Override protected void OnDraw (canvas canvas) {super.ondraw (canvas);        Rect rect=new rect (100,100,600,600);        1. Radial gradient radialgradient r=new radialgradient (350,350,250,,color.yellow, Shader.TileMode.MIRROR);        2. Linear gradient//lineargradient r=new lineargradient (100,100,600,600,,, Shader.TileMode.CLAMP);        3. Scan gradient//sweepgradient r=new sweepgradient (350,350,,;        Sweepgradient r=new sweepgradient (350,350,color,null); 4. Bitmap gradient//Bitmap bitmap= Bitmapfactory.decoderesource (getresources (), R. drawable.boy);//Bitmapshader r=new bitmapshader (Bitmap, Shader.TileMode.REPEAT, Shader.TileMode.MIRROR);        5. Mixed gradient//lineargradient lg=new lineargradient (100,100,600,600,,, Shader.TileMode.CLAMP);// Bitmap bitmap= Bitmapfactory.decoderesource (getresources (), r.drawable.boy);//Bitmapshader bs=new BitmapShader (Bitmap, Shader.TileMode.REPEAT, Shader.TileMode.MIRROR);//Composeshader r=new Composeshader (BS,LG, Porterduff.mod        E.src_atop);        Common part paint paint=new paint (paint.anti_alias_flag);        Paint.setshader (R);        Canvas.drawrect (Rect,paint);        Canvas.translate (0,500);    Canvas.drawoval (New RECTF (rect), paint); }}

Gradients and Matrix
public void Setlocalmatrix (Matrix localm), which can be combined with gradients to shift, rotate, scale, and skew as you fill the gradient colors.

We make a rotating circle, filled with a sweepgradient gradient inside the circle, which looks like a disc. First, we created a Matrix object Mmatrix, Mmatrix defines the rotation effect of a dot-centered gradient
Rotate the sweepgradient instead of rotating the Canvas. The OnDraw () method constantly calls invalidate () to redraw itself, spinning 2 degrees each time it is redrawn, thus forming a spinning animation.

public class Sweepmatrixview extends View {private paint mpaint=new paint (paint.anti_alias_flag);    private float mrotate;    Private matrix matrix=new matrix ();    Private Shader Mshader;    Private int[] color={,,};        Public Sweepmatrixview (context context, AttributeSet Attrs) {Super (context, attrs);        Setfocusable (TRUE);        Setfocusableintouchmode (TRUE);        float x=100;        float y=100;        Mshader=new sweepgradient (x,y,color,null);    Mpaint.setshader (Mshader); } public Sweepmatrixview (context context, AttributeSet attrs, int defstyleattr) {Super (context, Attrs, Defstyle    ATTR);        } @Override protected void OnDraw (canvas canvas) {super.ondraw (canvas);        Paint Paint =mpaint;        float x=100;        float y=100;        Canvas.translate (100,100);        Canvas.drawcolor (Color.White);        Matrix.setrotate (Mrotate,x,y);        Mshader.setlocalmatrix (matrix);      mrotate+=2;  if (mrotate>=360) {mrotate=0;        } invalidate ();    Canvas.drawcircle (X,y,100,paint); }}
You can implement a scan gradient like a disc rotation.

Custom Controls (shadows, gradients)

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: 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.