<1> Introduction
For details, refer to the blog post: Android learning notes. Advanced 15: shader rendering.
PublicBitmapshader(Bitmap bitmap, shader. tilemode tilex, shader. tilemode tiley)
Call this method to generate a Renderer with a bitmap ).
Bitmap bitmap used in the Renderer
Tilex the tiling mode for X to draw the bitmap in. tile mode in the X direction on the in-place Graph
Tiley the tiling mode for y to draw the bitmap in. tile mode in the Y direction on the in-place Graph
Tilemode: (there are three types)
Clamp: If the Renderer is out of the original boundary range, the inner edge of the replication range is colored.
Repeat: Specifies the horizontal and vertical repeated Renderer image, tiled.
Mirror: Repeated horizontal and vertical Renderer Images, AndRepeatThe repeat method is different. The image is tiled.
Still not quite clear? Let's take a look!
RepeatMirror
<2> Implementation
Package xiaosi. bitmapshader; </P> <p> Import android. app. activity; <br/> Import android. OS. bundle; </P> <p> public class bitmapshaderactivity extends activity {<br/>/** called when the activity is first created. */<br/> private bitmapshaders = NULL; <br/> @ override <br/> Public void oncreate (bundle savedinstancestate) {<br/> super. oncreate (savedinstancestate); </P> <p> bitmapshaders = new bitmapshaders (this); <br/> setcontentview (bitmapshaders); <br/>}< br/>}
Bitmapshaders. Java
Package xiaosi. bitmapshader; </P> <p> Import android. content. context; <br/> Import android. graphics. bitmap; <br/> Import android. graphics. bitmapshader; <br/> Import android. graphics. canvas; <br/> Import android. graphics. paint; <br/> Import android. graphics. shader; <br/> Import android. graphics. drawable. bitmapdrawable; <br/> Import android. graphics. drawable. shapedrawable; <br/> Import android. graphics. drawable. shapes. ovalshape; <br/> Import android. view. view; </P> <p> public class bitmapshaders extends view <br/>{< br/> private bitmapshader = NULL; <br/> private Bitmap bitmap = NULL; <br/> private paint = NULL; <br/> private shapedrawable = NULL; <br/> private int bitmapwidth = 0; <br/> private int bitmapheight = 0; <br/> Public bitmapshaders (context) <br/>{< br/> super (context ); <br/> // obtain the image <br/> bitmap = (bitmapdrawable) getresources (). getdrawable (R. drawable. h )). getbitmap (); <br/> bitmapwidth = bitmap. getwidth (); <br/> bitmapheight = bitmap. getheight (); <br/> // construct the Renderer bitmapshader <br/> bitmapshader = new bitmapshader (bitmap, shader. tilemode. mirror, shader. tilemode. repeat); <br/>}< br/> @ override <br/> protected void ondraw (canvas) <br/>{< br/> super. ondraw (canvas ); <br/> // crop the image to an elliptical shape <br/> // create a shapedrawable object and define the shape as an elliptical shape <br/> shapedrawable = new shapedrawable (New ovalshape ()); <br/> // get the paint brush and set the Renderer <br/> shapedrawable. getpaint (). setshader (bitmapshader); <br/> // set the display area <br/> shapedrawable. setbounds (20, 20, BitmapWidth-60, BitmapHeight-60); <br/> // draw shapedrawable <br/> shapedrawable. draw (canvas); <br/>}< br/>