Android Learning Note 12: Image rendering (Shader)

Source: Internet
Author: User

In Android, the shader class is provided specifically for rendering images and some geometry.

The shader class includes 5 direct subclasses, namely: Bitmapshader, Composeshader, LinearGradient, Radialgradient, and Sweepgradient. Where Bitmapshader is used for image rendering, composeshader for mixed rendering, lineargradient for linear rendering, radialgradient for ring rendering, and sweepgradient for gradient rendering.

When using the shader class for image rendering, you first need to build the shader object, then set the render object through the paint's Setshader () method, and finally draw the paint object onto the screen.

It is important to note that different objects need to be built when rendering images in different ways.

1.BitmapShader (Image rendering)

The role of Bitmapshader is to populate an area with a bitmap as a texture. You can imagine tiling in a single area, but the tile here is a bitmap.

The Bitmapshader function prototype is:

  Public Bitmapshader (Bitmap Bitmap, Shader.tilemode Tilex, Shader.tilemode Tiley);

Where the parameter bitmap represents the bitmap to be filled as a texture, and the parameter Tilex represents the upper-bound form in the X-direction of the bitmap, and the parameter Tiley represents the upper-graph cohesion form of the bitmap y-direction.

Shader.tilemode has 3 parameters to choose from, namely clamp, repeat and mirror.

The effect of clamp is that if the renderer is out of the original boundary range, the edge color is copied to colorize the out-of-range area. The role of repeat is to repeatedly render bitmaps in a tiled form, both horizontally and vertically. The role of mirror is to repeatedly render bitmaps in a mirrored manner, both horizontally and vertically.

2.LinearGradient (linear rendering)

The function of LinearGradient is to realize the linear gradient effect of color in an area.

The function prototypes for LinearGradient are:

  Public lineargradient (float x0, float y0, float x1, float y1, int[] colors, float[] positions, shader.tilemode tile);

Where the parameter x0 represents the starting point x coordinate of the gradient, the parameter y0 represents the starting point y-coordinate of the gradient, the parameter X1 represents the end point x coordinate of the gradient, the parameter Y1 represents the end point Y coordinate of the gradient, the parameter colors the color array for the gradient, and the parameter positions to specify the relative position of the color array ; The parameter tile represents the tiling method.

Typically, the parameter positions is set to NULL, which indicates that the color array is evenly distributed as a ramp line.

3.ComposeShader (mixed rendering)

The function of Composeshader is to achieve the overlay of rendering effect, such as the mixed rendering effect of bitmapshader and lineargradient.

The function prototypes for Composeshader are:

  Public Composeshader (Shader Shadera, Shader shaderb, Porterduff.mode Mode);

Where the parameter Shadera represents a rendering effect, the parameter Shaderb also represents a rendering effect, and the parameter mode represents the overlay mode of the two rendering effects.

Porterduff.mode has 16 parameters to choose from: CLEAR, SRC, DST, Src_over, Dst_over, src_in, dst_in, Src_out, Dst_out, Src_atop, Dst_atop, XOR, darken, lighten, MULTIPLY, screen.

The specific overlay effect of these 16 overlay modes is shown in 1.

Figure 1 Overlay Effect

4.RadialGradient (Ring rendering)

The role of radialgradient is to achieve a circular gradient within a region.

The function prototypes for radialgradient are:

  Public radialgradient (float x, float y, float radius, int[] colors, float[] positions, shader.tilemode tile);

Where the parameter x represents the center x coordinate of the ring, the parameter Y represents the center y coordinate of the ring; The parameter radius represents the radius of the ring, the parameter colors represents the color array of the annular gradient, the parameter positions is used to specify the relative position of the color array, and the parameter tile means the tile.

5.SweepGradient (Gradient rendering)

Sweepgradient, also known as scan rendering, refers to the rendering of a scanning effect in a center that rotates counterclockwise by one week in the positive direction of the x-axis.

  The function prototypes for sweepgradient are:

  Public sweepgradient (float CX, float CY, int[] colors, float[] positions);

Where the parameter CX represents the center x coordinate of the scan, the parameter Cy represents the central y-coordinate of the sweep, the parameter colors represents the gradient gradient of the color array, and the parameter positions is used to specify the relative position of the color array.

6. Example

In this example, the above 5 rendering effects are realized respectively. As shown in effect 2. Among them, the top is Bitmapshader, the second row of the left is lineargradient, the second row of the right is Radialgradient The left side of the third row is the Composeshader (LinearGradient and Radialgradient), and the third row to the right is sweepgradient.

Figure 2 Rendering effect

Define the MyView class, used to draw a variety of rendering effects, Myview.java source code as follows.

Myview.java Source PackageCom.example.android_imageshader;ImportAndroid.annotation.SuppressLint;ImportAndroid.content.Context;ImportAndroid.graphics.Bitmap;ImportAndroid.graphics.BitmapShader;ImportAndroid.graphics.Canvas;ImportAndroid.graphics.Color;ImportAndroid.graphics.ComposeShader;Importandroid.graphics.LinearGradient;ImportAndroid.graphics.Paint;ImportAndroid.graphics.PorterDuff;Importandroid.graphics.RadialGradient;ImportAndroid.graphics.RectF;ImportAndroid.graphics.Shader;Importandroid.graphics.SweepGradient;Importandroid.graphics.drawable.BitmapDrawable;ImportAndroid.view.View; @SuppressLint ({"Drawallocation", "Drawallocation", "Drawallocation" }) Public classMyViewextendsView {Bitmap Mbitmap=NULL;//Bitmap ObjectShader Mbitmapshader =NULL;//Bitmap Render ObjectsShader mlineargradient =NULL;//linear Gradient Render objectShader Mcomposeshader =NULL;//Blending Render ObjectsShader mradialgradient =NULL;//Ring Render ObjectShader msweepgradient =NULL;//Gradient Render Object                 PublicMyView (Context context) {Super(context); //Loading Image ResourcesMbitmap =( (bitmapdrawable) getresources ().                Getdrawable (R.drawable.snow)). Getbitmap (); //creating bitmap Render ObjectsMbitmapshader =NewBitmapshader (Mbitmap, Shader.TileMode.REPEAT, Shader.TileMode.MIRROR); //Create a linear render object        intMcolorlinear[] ={color.red, Color.green, Color.Blue, color.white}; Mlineargradient=NewLinearGradient (0, 0, Mcolorlinear,NULL, Shader.TileMode.REPEAT); //creating a ring Render object        intMcolorradial[] ={color.green, color.red, Color.Blue, color.white}; Mradialgradient=NewRadialgradient (325, mcolorradial,NULL, Shader.TileMode.REPEAT); //Create a mixed render objectMcomposeshader =NewComposeshader (mlineargradient, mradialgradient, PorterDuff.Mode.DARKEN); //creating trapezoid Render Objects        intMcolorsweep[] ={color.green, color.red, Color.Blue, Color.yellow, color.green}; Msweepgradient=NewSweepgradient (370, 495, Mcolorsweep,NULL); }     Public voidOnDraw (canvas canvas) {Super. OnDraw (canvas); Paint Mpaint=NewPaint ();      Canvas.drawcolor (Color.gray); //background is gray//draw an ellipse bitmap renderedMpaint.setshader (Mbitmapshader); Canvas.drawoval (NewRECTF (90, 20, 90+mbitmap.getwidth (),20+mbitmap.getheight ()), mpaint); //draw a linear gradient rectangleMpaint.setshader (mlineargradient); Canvas.drawrect (10, 250, 250, 400, Mpaint); //draw a circle with a circular gradientMpaint.setshader (mradialgradient); Canvas.drawcircle (350, 325, 75, Mpaint); //draw a rectangle with mixed gradients (linear and ring blending)Mpaint.setshader (Mcomposeshader); Canvas.drawrect (10, 420, 250, 570, Mpaint); //draw a trapezoidal gradient rectangleMpaint.setshader (msweepgradient); Canvas.drawrect (270, 420, 470, 570, Mpaint); }    }

Android Learning Note 12: Image rendering (Shader)

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.