Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Understanding Shader in Android?

Tags:

android

shader

According to Android developer docs :

Shader is the based class for objects that return horizontal spans of colors during drawing. A subclass of Shader is installed in a Paint calling paint.setShader(shader). After that any object (other than a bitmap) that is drawn with that paint will get its color(s) from the shader.

But I don't understand that definition. Could you guys please tell me what is shader in android in simple terms? Also, what is the meaning of "horizontal spans of colors"?

Thanks a lot.

like image 798
Nguyen Hoai Thanh Avatar asked Mar 20 '14 15:03

Nguyen Hoai Thanh


People also ask

How does a shader work?

A shader is a piece of code that is executed on the Graphics Processing Unit (GPU), usually found on a graphics card, to manipulate an image before it is drawn to the screen. Shaders allow for various kinds of rendering effect, ranging from adding an X-Ray view to adding cartoony outlines to rendering output.

What are the two main types of shaders and what do they do?

There are several kinds of shaders, but two are commonly used to create graphics on the web: Vertex Shaders and Fragment (Pixel) Shaders. Vertex Shaders transform shape positions into 3D drawing coordinates. Fragment Shaders compute the renderings of a shape's colors and other attributes.

What is vertex shader and pixel shader?

A vertex shader is a program executed on the graphics card's GPU which operates on each vertex individually. This facilitates we can write our own custom algorithm to work with the vertex's. Pixel Shaders: A pixel shader is a program executed on the graphics card's GPU during the rasterization process for each pixel.


1 Answers

A shader allows to define for a Paint object the content which should be drawn.

For example you can use a BitmapShader to define that a bitmap should be used to draw. This allows you for example to draw an image with rounded corners. Simply define a BitmapShader for your Paint object and use the drawRoundRect() method to draw a rectancle with rounded corners.

Other Shaders provided by the Android platform are LinearGradient, RadialGradient and SweepGradient for drawing color gradients.

To use a Shaders assign it to your Paint object via the setShader() method.

If the area which is filled is larger than the Shaders you can define via the Shader tile mode how the rest should be filled.

The Shader.TileMode.CLAMP constant defines that the edge corners should be used to fill the extra space. 
The Shader.TileMode.MIRROR constant defines that the image is mirrored.
The Shader.TileMode.REPEAT defines that the image will be repeated. 

Source: Vogella

like image 186
Ahmed Avatar answered Oct 12 '22 19:10

Ahmed