<1> Introduction
Take a look at the blog post: Android Learning notes advanced 15 shader rendering
Public Bitmapshader(Bitmap bitmap,shader.tilemode tilex,shader.tilemode Tiley)
Call this method to produce a renderer (Shader) that draws a bitmap.
Bitmap bitmaps used within the renderer
Tilex the tiling mode for x to draw the bitmap in. X-Directional tile pattern on the bitmap
Tiley the tiling mode for Y-draw the bitmap in. Y-Direction tile pattern on the bitmap
Tilemode: (a total of three kinds)
CLAMP: If the renderer exceeds the original boundary range, the range edge staining is copied.
REPEAT: Landscape and portrait Repeat renderer picture, tiled.
MIRROR: Horizontal and vertical duplicate renderer picture, this is not the same as the REPEAT repetition, he is tiled in a mirrored manner.
Still don't understand? Let's see it!
REPEAT MIRROR
<2> Concrete Implementation
[Java]View PlainCopy
- Package Xiaosi. Bitmapshader;
- Import android.app.Activity;
- Import Android.os.Bundle;
- Public class Bitmapshaderactivity extends Activity {
- /** Called when the activity is first created. * /
- private Bitmapshaders bitmapshaders = null;
- @Override
- public void OnCreate (Bundle savedinstancestate) {
- super.oncreate (savedinstancestate);
- Bitmapshaders = New Bitmapshaders (this);
- Setcontentview (bitmapshaders);
- }
- }
Bitmapshaders.java
[Java]View PlainCopy
- Package Xiaosi. Bitmapshader;
- Import Android.content.Context;
- Import Android.graphics.Bitmap;
- Import Android.graphics.BitmapShader;
- Import Android.graphics.Canvas;
- Import Android.graphics.Paint;
- Import Android.graphics.Shader;
- Import android.graphics.drawable.BitmapDrawable;
- Import android.graphics.drawable.ShapeDrawable;
- Import Android.graphics.drawable.shapes.OvalShape;
- Import Android.view.View;
- Public class Bitmapshaders extends View
- {
- private Bitmapshader bitmapshader = null;
- private Bitmap Bitmap = null;
- Private Paint paint = null;
- private Shapedrawable shapedrawable = null;
- private int bitmapwidth = 0;
- private int bitmapheight = 0;
- Public bitmapshaders (context context)
- {
- super (context);
- //Get images
- Bitmap = ((bitmapdrawable) getresources (). getdrawable (R.drawable.h)). Getbitmap ();
- Bitmapwidth = Bitmap.getwidth ();
- Bitmapheight = Bitmap.getheight ();
- //Construct renderer Bitmapshader
- Bitmapshader = new Bitmapshader (bitmap,shader.tilemode.mirror,shader.tilemode.repeat);
- }
- @Override
- protected void OnDraw (canvas canvas)
- {
- Super.ondraw (canvas);
- //Crop the picture to an ellipse
- //Build Shapedrawable object and define shape as ellipse
- shapedrawable = New Shapedrawable (new OvalShape ());
- //Get brushes and set renderer
- Shapedrawable.getpaint (). Setshader (Bitmapshader);
- //Set display area
- Shapedrawable.setbounds ( 20,bitmapwidth-60,bitmapheight-60);
- //Draw shapedrawable
- Shapedrawable.draw (canvas);
- }
- }
Android Learning notes Advanced 16 Bitmapshader