Import android. content. context; import android. graphics. canvas; import android. graphics. color; import android. graphics. lineargradient; import android. graphics. paint; import android. graphics. shader; import android. view. view;/*** @ version 10:45:28 **/public class mygameview extends view implements runnable {int bitwidth = 0; int bitheight = 0; paint mpaint = NULL; // linear gradient rendering shader mlineargrad Ient1 = NULL; // linear gradient rendering shader mlineargradient2 = NULL; // linear gradient rendering shader mlineargradient3 = NULL; Public mygameview (context) {super (context ); // create a lineargradient and set the gradient color array // The first vertex. The second vertex number indicates that the gradient start vertex can be set to any position on the diagonal corner and the third vertex, the fourth vertex number indicates the gradient end point // The Fifth vertex number indicates the gradient color // The sixth vertex number can be blank, indicating the coordinates, the value is 0-1 new float [] {0.25f, 0.5f, 0.75f, 1} // If This Is null, the color is evenly distributed along the ladder. // The Seventh color indicates the tile method. // clamp repeats the last color until the last color. // the image that is repeatedly colored by mirror is filled in either the horizontal or vertical direction with an image with a flip effect. // repeat repeatedly colored image horizontal or vertical direction mlineargradient1 = new lineargradient (0, 0, 0,100, new int [] {color. red, color. green, color. blue, color. white}, null, shader. tilemode. clamp); mlineargradient2 = new lineargradient (0, 0, 0,100, new int [] {color. red, color. green, color. blue, color. white}, null, shader. tilemode. mirror); mlineargradient 3 = new lineargradient (0, 0, 0,100, new int [] {color. red, color. green, color. blue, color. white}, null, shader. tilemode. repeat); mpaint = new paint (); New thread (this ). start () ;}@ override protected void ondraw (canvas) {// The height of lineargradient is only 100, and the rectangle drawn is 200. So it will be repeated. // assuming that the height of the two is the same as that of clamp, it is impossible to see the effect of painting = new paint (); paint. settextsize (20); paint. setcolor (color. white); // draw the gradient rectangle mpaint. set Shader (mlineargradient1); canvas. drawrect (0, 0,200,200, mpaint); canvas. drawtext ("Clamp", 0,200 + 20, paint); // draw the gradient rectangle mpaint. setshader (mlineargradient2); canvas. drawrect (0,250,200,450, mpaint); canvas. drawtext ("mirror", 0,450 + 20, paint); // draw the gradient rectangle mpaint. setshader (mlineargradient3); canvas. drawrect (0,500,200,700, mpaint); canvas. drawtext ("repeat", 0,700 + 20, paint) ;}@ override P Ublic void run () {While (! Thread. currentthread (). isinterrupted () {try {thread. sleep (100);} catch (exception e) {thread. currentthread (). interrupt () ;}postinvalidate ();}}}
Lineargradient linear Rendering