Shader Renderer for Android

Source: Internet
Author: User

The use of the renderer inside Android is mainly shader subclasses, shader inherits from object, and his subclasses are:

1, Bitmapshader:bitmapshader is bitmap renderer, look at the name will know,

Bitmapshader is a subclass of Shader that can be set by Paint.setshader (Shader Shader),

Here we only focus on Bitmapshader, the construction method:

Mbitmapshader = new Bitmapshader (bitmap, Tilemode.clamp, Tilemode.clamp);

Parameter 1:bitmap

Parameter 2, parameter 3:tilemode;

There are three types of Tilemode:

CLAMP stretching

REPEAT Repeat

MIRROR Mirroring

If you set the screen screensaver, if the picture is too small, you can choose to repeat, stretch, mirror;

repeat: this bitmap is repeated horizontally and vertically.

Mirror: The cross is constantly flipping and repeating;

Stretch: This and the computer screensaver should be a little different, this stretch is the last pixel of the picture, the last horizontal row of pixels, continuous repetition, the column of the vertical line of pixels, constantly repeating;

Now you probably understand, bitmapshader by setting it to Mpaint, and then drawing with this mpaint, the drawing area is colored according to the tilemode you set.

One thing to note here is that Bitmapshader is drawn from the upper-left corner of your canvas and does not draw a square in the lower-right corner of the view, and it does not start in the upper-left corner of your square.


2, LinearGradient:

Linear gradients are also inherited with shader:lineargradient lg=new lineargradien (0,0,100,100,color.red,color.blue, Shader.TileMode.MIRROR);
Parameter one is the beginning point coordinate of the gradient x position, the parameter two is the y-axis position, the parameter three and four resolution corresponds to the gradient end point, the last parameter is tiled, here is set as mirror

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 (0,0,200,mpaint); Parameter 3 is the radius of the circle, and the type is float type.

 

It can be defined in addition to the start and end colors, and the segmented gradient effect 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);
Where parameters new Int[]{startcolor, Midlecolor, EndColor} are a collection of colors that participate in the gradient effect,
Where parameter new float[]{0, 0.5f, 1.0f} is the relative position of the gradient where each color is defined,
This parameter can be null if NULL indicates that all colors are evenly distributed in order


3, Radialgradient:

Ring rendering: Almost as basic as the top

Create a ring Render object, select repeating mode
int mcolorradial[] = {color.green, color.red, Color.Blue, color.white};
Mradialgradient = new Radialgradient (, Mbitmap.getheight () *3/4+75,, mcolorradial, NULL,
Shader.TileMode.REPEAT);

4, Composeshader:

Combined rendering: The difference between him and the top is that he can include two different rendering styles and then combine them.

Mcomposeshader = new Composeshader (mlineargradient, Mradialgradient,
PorterDuff.Mode.DARKEN);

5, Sweepgradient:

Trapezoid rendering://creating Trapezoid render objects
int mcolorsweep[] = {color.green, color.red, Color.Blue, Color.yellow, color.green};
Msweepgradient = new Sweepgradient (540, N, mcolorsweep, NULL); The first argument is where you want to start the gradient from where the x axis is, the second is the Y axis, the third is the gradient array, the fourth is the position, you can specify the absolute position of the gradient


Or 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 Render Objects
Shader mlineargradient = null; Linear gradient Render Object
Shader mcomposeshader = null; Blending Render Objects
Shader mradialgradient = null; Ring Render Object
Shader msweepgradient = null; Gradient Render Object


/* Tilemode has three values:

CLAMP stretching

REPEAT Repeat

MIRROR Mirror */
Public Bitmapshaderview (Context context) {
Super (context);

Load image resource, get to bitmap object
Mbitmap = ((bitmapdrawable) getresources ().
Getdrawable (R.drawable.mate5)). Getbitmap ();
Creates a bitmap render object, selects the tiled x-axis, and the y-axis repeats two properties
Mbitmapshader = new Bitmapshader (Mbitmap, Shader.TileMode.MIRROR,
Shader.TileMode.REPEAT);
Create a linear render object, select Tile
int mcolorlinear[] = {color.red, color.green, Color.Blue, color.white}; Initializes an array of gradient colors
Mlineargradient = new LinearGradient (0, 0, +, +, mcolorlinear, NULL,
Shader.TileMode.MIRROR);

Create a ring Render object, select repeating mode
int mcolorradial[] = {color.green, color.red, Color.Blue, color.white};
Mradialgradient = new Radialgradient (, Mbitmap.getheight () *3/4+75,, mcolorradial, NULL,
Shader.TileMode.REPEAT);
Shader lineargradient = new LinearGradient (430, +, +, mcolorlinear, NULL,
Shader.TileMode.MIRROR);

Shader radialgradient = new Radialgradient (430, Mbitmap.getheight () *3/4+75,, mcolorradial, NULL,
Shader.TileMode.REPEAT);

Create a mixed Render object
Mcomposeshader = new Composeshader (mlineargradient, Mradialgradient,
PorterDuff.Mode.DARKEN);


Creating Trapezoid Render Objects
int mcolorsweep[] = {color.green, color.red, Color.Blue, Color.yellow, color.green};
Msweepgradient = new Sweepgradient (540, N, mcolorsweep, NULL); The first argument is where you want to start the gradient from where the x axis is, the second is the Y axis, the third is the gradient array, 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); Background is gray
float scale = 1.0f;
Matrix Matrix =new Matrix ();
Get a small value of bitmap wide or high
int bsize = Math.min (Mbitmap.getwidth (), Mbitmap.getheight ());
Scale = * 1.0F/BSIZE;

Matrix.setscale (scale, scale);
Mbitmapshader.setlocalmatrix (matrix);
Draw an ellipse bitmap rendered
Mpaint.setshader (Mbitmapshader);

Canvas.drawroundrect (New RECTF, Mbitmap.getwidth () *3/4,
Mbitmap.getheight () *3/4), 200,200, mpaint);

Draw a linear gradient rectangle
Mpaint.setshader (mlineargradient);
Canvas.drawrect (Mbitmap.getheight () *3/4, 250,mbitmap.getheight () *3/4+100, mpaint);

Draw a circle with a circular gradient
Mpaint.setshader (mradialgradient);
Canvas.drawcircle (Mbitmap.getheight () *3/4+75, mpaint);

Draw a rectangle with mixed gradients (linear and ring blending)
Mpaint.setshader (Mcomposeshader);
Canvas.drawrect (430, +, +, mpaint);
//
Draw a trapezoidal gradient rectangle
Mpaint.setshader (msweepgradient);
Canvas.drawrect (430, 630,800, mpaint);
}
}

demo:http://download.csdn.net/detail/u012808234/8597197

Shader Renderer for Android

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.