Basic tutorial for Android -- 8.3.13 Paint API -- Shader (Image Rendering)

Source: Internet
Author: User

Basic tutorial for Android -- 8.3.13 Paint API -- Shader (Image Rendering)
This section introduces:

Recently, my blog updates have been delayed due to work events and interviews. I apologize for your failure ~
This afternoon, I went to the dream catcher network for an interview. The great gods gave me a lesson, increasing my knowledge,
As well as the planning of some growth routes in the future, although the probability of going in is not big, I 'd like to thank you,
In addition, I once again felt the importance of my graduation certificate and missed many opportunities when I was looking for a job,
Readers who are still studying at the university are advised not to go to science... Okay, no,
This section introduces another painting API-Shader (Image Rendering). We can call the setShader () method of painting
To set the Shader rendering effect for the paint brush, we can open the official API documentation: Shader

Like MaskFilter, ColorFilter, and PathEffect, we
Generally, Shader is not used directly, but its five sub-classes are used:
BitmapShader (Image Rendering), ComposeShader (hybrid rendering), LinearGradient (linear rendering)
RadialGradient (ring rendering), SweepGradient (gradient rendering)
Let's introduce their construction methods one by one ~

1. Detailed description of the constructor 1) BitmapShader (Image Rendering)

BitmapShader (Bitmap bitmap, Shader. TileMode tileX, Shader. TileMode tileY)

Fill a region with a bitmap as the texture. The parameters are as follows:
Bitmap: Used as a filled bitmap;
TileX: The link form of the bitmap on the X axis;
TileY: The link form of the bitmap on the Y axis;
The Shader. TileMode has three types:
CLAMPIf the Renderer is out of the original boundary range, the edge color will be copied to color the area beyond the range,
REPEATThe rendering is repeated in Tiled form,MIRRORThe bitmap is repeatedly rendered in the form of an image either horizontally or vertically.

2) ComposeShader (hybrid rendering)

ComposeShader (Shader shaderA, Shader shaderB, PorterDuff. Mode mode)

The rendering effect overlay. What do I know when I see PorterDuff? For example, mixed rendering of BitmapShader and LinearGradient
Effect. Parameters:
ShaderA: First rendering Effect
ShaderB: Second rendering Effect
Mode: Overlapping modes of two rendering Effects

3) LinearGradient (linear rendering)

LinearGradient (float x0, float y0, float x1, float y1, int [] colors, float [] positions, Shader. TileMode tile );

Implement linear gradient effects of colors in a certain area. The parameters are as follows:
X0: Gradient start point x coordinate
Y0: Gradient start point y coordinate
X1: X coordinate of the gradient end point
Y1: Y coordinate of the gradient end point
Colors: Gradient color array
Positions: Relative position of the color array
Tile: Tile Mode

4) RadialGradient (ring rendering)

Public RadialGradient (float x, float y, float radius, int [] colors, float [] positions, Shader. TileMode tile );

Implement the ring gradient effect of colors in a certain area. The parameters are as follows:
X: X coordinate of the center of the ring
Y: Y coordinate of the center of the ring
Radius: Radius of the ring
Colors: Color array of ring gradient
Positions: Specifies the relative position of the color array.
Tile: Tile Mode

5) SweepGradient (gradient rendering)

Public SweepGradient (float cx, float cy, int [] colors, float [] positions)

Scan rendering is the effect of rotating around a point for one week! The parameters are as follows:
Cx: Scanned center x coordinate
Cy: Y coordinate of the scan Center
Colors: Gradient color array
Positions: Specifies the relative position of the color array.

We may be able to understand a general role of the corresponding text, but we still need to write a code
Verify their role. After all, is there a code (picture) to tell the truth ~

2. Sample Code:

Run:

Implementation Code:

BitmapShaderView. java:

/*** Created by Jay on 0030. */public class BitmapShaderView extends View {private Bitmap mBitmap = null; private ShapeDrawable sDrawable = null; private Paint mPaint = null; private int bitW = 0, bitH = 0; // Bitmap width and height private Shader mBitmapShader = null; // Bitmap rendering private Shader mLinearGradient = null; // linear gradient rendering private Shader mComposeShader = null; // hybrid rendering private Shader mRadialGradient = Null; // ring gradient rendering private Shader mSweepGradient = null; // gradient rendering public BitmapShaderView (Context context) {this (context, null);} public BitmapShaderView (Context context, attributeSet attrs) {super (context, attrs); init ();} public BitmapShaderView (Context context, AttributeSet attrs, int defStyleAttr) {super (context, attrs, defStyleAttr );} private void init () {mBitmap = BitmapFactory. decodeRes Ource (getResources (), R. mipmap. img_cat); bitW = mBitmap. getWidth (); bitH = mBitmap. getHeight (); mPaint = new Paint (); // create BitmapShader mBitmapShader = new BitmapShader (mBitmap, Shader. tileMode. MIRROR, Shader. tileMode. MIRROR); // create LinearGradient and set the gradient Color array mLinearGradient = new LinearGradient (0, 0,100,100, new int [] {Color. RED, Color. GREEN, Color. BLUE, Color. WHITE}, null, Shader. tileMode. REPEAT );/ /Hybrid rendering. Here BitmapShader and LinearGradient are used for hybrid rendering. Try other functions ~ MComposeShader = new ComposeShader (mBitmapShader, mLinearGradient, PorterDuff. mode. DARKEN); // ring gradient rendering mRadialGradient = new RadialGradient (50,200, 50, new int [] {Color. GREEN, Color. RED, Color. BLUE, Color. WHITE}, null, Shader. tileMode. REPEAT); // gradient rendering mSweepGradient = new SweepGradient (30, 30, new int [] {Color. GREEN, Color. RED, Color. BLUE, Color. WHITE}, null) ;}@ Override protected void onDraw (Canvas canvas) {super. onDraw (canvas); // crop the image to an elliptical shape sDrawable = new ShapeDrawable (new OvalShape (); sDrawable. getPaint (). setShader (mBitmapShader); sDrawable. setBounds (0, 0, bitW, bitH); sDrawable. draw (canvas); // draw the linear gradient rectangle mPaint. setShader (mLinearGradient); canvas. drawRect (bitW, 0, bitW * 2, bitH, mPaint); // draw the mPaint for mixed rendering. setShader (mComposeShader); canvas. drawRect (0, bitH, bitW, bitH * 2, mPaint); // draw a ring gradient mPaint. setShader (mRadialGradient); canvas. drawCircle (bitW * 2.8f, bitH/2, bitH/2, mPaint); // draw a gradient mPaint. setShader (mSweepGradient); canvas. drawRect (bitW, bitH, bitW * 2, bitH * 2, mPaint );}}

For example, if you have one hundred lines of code, you don't need to explain it. If you are confused, try it ~

 

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.