Android shader Renderer

Source: Internet
Author: User

Android shader Renderer

The Renderer in android is mainly used as a Child class of shader. shader inherits from object and its Child classes include:

1. BitMapShader: The BitMapShader is a bitmap Renderer. You can find its name,

 

BitmapShader is a sub-class of Shader. You can use Paint. setShader (Shader shader) to set,

Here we only focus on BitmapShader. The constructor is as follows:

MBitmapShader = new BitmapShader (bitmap, TileMode. CLAMP, TileMode. CLAMP );

Parameter 1: bitmap

Parameter 2, parameter 3: TileMode;

There are three TileMode values:

CLAMP stretch

REPEAT already exists.

MIRROR Image

If you set screen saver for your computer screen and the image is too small, you can repeat, stretch, or mirror;

Repeat:This bitmap is repeated horizontally and vertically.

Image:Repeated horizontal flip and vertical flip;

Stretch:The screen saver mode should be somewhat different from the screen saver mode of the computer. This stretch is the last pixel of the image; the last horizontal pixel is rampant, repeated, and the column of pixels of the vertical item, constant repetition;

Now I understand that BitmapShader sets the image to mPaint and then colors the image area based on the TileMode you set when drawing the image with this mPaint.

Note that BitmapShader is drawn from the upper-left corner of your canvas, rather than a square in the lower-right corner of the view, it will not start in the upper-left corner of your square.


2. LinearGradient:

Linear Gradient also inherits from shader: LinearGradient lg = new LinearGradien (100,100, Color. RED, Color. BLUE, Shader. TileMode. MIRROR );
Parameter 1 is the initial coordinate x position of the gradient, parameter 2 is the y-axis position, parameter 3 and 4 are corresponding to the gradient end point, and the final parameter is the tile mode. Here the image is set

Gradient is based on the Shader class, so we use the setShader method of Paint to set this Gradient. The Code is as follows: mPaint. setShader (lg );
Canvas. drawCicle (200, mPaint); // parameter 3 is the radius of the circle, float type.

In addition to defining the start color and end color, it can also be defined as a piecewise gradient effect composed of multiple colors.
LinearGradient shader = new LinearGradient (0, 0, endX, endY, new int [] {startColor, midleColor, endColor}, new float [] {0, 0.5f, 1.0f}, TileMode. MIRROR );
The new int [] {startColor, midleColor, endColor} parameter is a set of colors involved in the gradient effect,
The new float [] {0, 0.5f, 1.0f} parameter defines the relative position of each color gradient,
This parameter can be null. If it is null, all colors are evenly distributed in order.


3. RadialGradient:

Ring rendering: similar to the above

// Create a ring rendering object and select repeat mode
Int mColorRadial [] = {Color. GREEN, Color. RED, Color. BLUE, Color. WHITE };
MRadialGradient = new RadialGradient (350, mBitmap. getHeight () * 3/4 + 75, 75, mColorRadial, null,
Shader. TileMode. REPEAT );

4. ComposeShader:

Combined rendering: different from the above, it can contain two different rendering methods and then combine them.

MComposeShader = new ComposeShader (mLinearGradient, mRadialGradient,
PorterDuff. Mode. DARKEN );

5. SweepGradient:

Trapezoid rendering: // create a trapezoid rendering object
Int mColorSweep [] = {Color. GREEN, Color. RED, Color. BLUE, Color. YELLOW, Color. GREEN };
MSweepGradient = new SweepGradient (540,750, mColorSweep, null); // The first parameter indicates where you want to start the gradient from the X axis, and the second parameter is the Y axis, the third is the gradient color array, and the fourth is the position. You can specify the absolute position of the gradient.

 

Let's look at the Code:

Package com. example. colorselect;
Import android. annotation. SuppressLint;
Import android. content. Context;
Import android. graphics. Bitmap;
Import android. graphics. BitmapShader;
Import android. graphics. Canvas;
Import android. graphics. Color;
Import android. graphics. ComposeShader;
Import android. graphics. LinearGradient;
Import android. graphics. Matrix;
Import android. graphics. Paint;
Import android. graphics. PorterDuff;
Import android. graphics. RadialGradient;
Import android. graphics. RectF;
Import android. graphics. Shader;
Import android. graphics. SweepGradient;
Import android. graphics. drawable. BitmapDrawable;
Import android. view. View;

@ SuppressLint ({DrawAllocation, DrawAllocation, DrawAllocation })
Public class BitMapShaderView extends View {

Bitmap mBitmap = null; // Bitmap object
Shader mBitmapShader = null; // Bitmap rendering object
Shader mLinearGradient = null; // linear gradient rendering object
Shader mComposeShader = null; // mixed rendering object
Shader mRadialGradient = null; // ring rendering object
Shader mSweepGradient = null; // gradient rendering object


/* There are three TileMode values:

CLAMP stretch

REPEAT already exists.

MIRROR Image */
Public BitMapShaderView (Context context ){
Super (context );

// Load image resources and obtain the bitmap object
MBitmap = (BitmapDrawable) getResources ().
GetDrawable (R. drawable. mate5). getBitmap ();
// Create a Bitmap rendering object and select the tiled X axis. The Y axis repeats the two attributes.
MBitmapShader = new BitmapShader (mBitmap, Shader. TileMode. MIRROR,
Shader. TileMode. REPEAT );
// Create a linear rendering object and select Tile
Int mColorLinear [] = {Color. RED, Color. GREEN, Color. BLUE, Color. WHITE}; // initializes an array of gradient colors.
MLinearGradient = new LinearGradient (0, 0,100,100, mColorLinear, null,
Shader. TileMode. MIRROR );

// Create a ring rendering object and select repeat mode
Int mColorRadial [] = {Color. GREEN, Color. RED, Color. BLUE, Color. WHITE };
MRadialGradient = new RadialGradient (350, mBitmap. getHeight () * 3/4 + 75, 75, mColorRadial, null,
Shader. TileMode. REPEAT );
Shader LinearGradient = new LinearGradient (430,800,600,100 0, mColorLinear, null,
Shader. TileMode. MIRROR );

Shader RadialGradient = new RadialGradient (430, mBitmap. getHeight () * 3/4 + 75, 75, mColorRadial, null,
Shader. TileMode. REPEAT );

// Create a hybrid rendering object
MComposeShader = new ComposeShader (mLinearGradient, mRadialGradient,
PorterDuff. Mode. DARKEN );


// Create a trapezoid rendering object
Int mColorSweep [] = {Color. GREEN, Color. RED, Color. BLUE, Color. YELLOW, Color. GREEN };
MSweepGradient = new SweepGradient (540,750, mColorSweep, null); // The first parameter indicates where you want to start the gradient from the X axis, and the second parameter is the Y axis, the third is the gradient color array, and the fourth is the position. You can specify the absolute position of the gradient.
}

Public void onDraw (Canvas canvas ){
Super. onDraw (canvas );

Paint mPaint = new Paint ();
Canvas. drawColor (Color. WHITE); // set the background to Gray.
Float scale = 1.0f;
Matrix matrix = new Matrix ();
// Obtain the bitmap width or height.
Int bSize = Math. min (mBitmap. getWidth (), mBitmap. getHeight ());
Scale = 500 * 1.0f/bSize;

Matrix. setScale (scale, scale );
MBitmapShader. setLocalMatrix (matrix );
// Draw the Bitmap rendered elliptic
MPaint. setShader (mBitmapShader );

Canvas. drawRoundRect (new RectF (20, 20, mBitmap. getWidth () * 3/4,
MBitmap. getHeight () * 3/4), 200,200, mPaint );

// Draw a linear gradient rectangle
MPaint. setShader (mLinearGradient );
Canvas. drawRect (10, mBitmap. getHeight () * 3/4, 250, mBitmap. getHeight () * 3/4 + 100, mPaint );

// Draw a circle with a ring gradient
MPaint. setShader (mRadialGradient );
Canvas. drawCircle (350, mBitmap. getHeight () * 3/4 + 75, 75, mPaint );

// Draw a rectangle with a mixed gradient (linear and annular)
MPaint. setShader (mComposeShader );
Canvas. drawRect (430,800,600,100 0, mPaint );
//
// Draw a trapezoid gradient rectangle
MPaint. setShader (mSweepGradient );
Canvas. drawRect (430,700,630,800, mPaint );
}
}

 

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.