Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Android Color processing (quad) Bitmapshader bitmap 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-Direction renderer tiling mode on bitmap
Tiley The tiling mode for Y-draw the bitmap in. The Y-direction renderer tiling mode on the bitmap
Tilemode:
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.
First Look at:
or directly on the code:
Mainactivity:
[Java]View PlainCopy
- Package com.tony.shader;
- Import Android.os.Bundle;
- Import android.app.Activity;
- Public class Mainactivity extends Activity {
- private Bitmapshaderview Shaderview;
- @Override
- protected void OnCreate (Bundle savedinstancestate) {
- super.oncreate (savedinstancestate);
- Shaderview = New Bitmapshaderview (this);
- Setcontentview (Shaderview);
- }
- }
Bitmapshaderview:
[Java]View PlainCopy
- Package com.tony.shader;
- 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.util.AttributeSet;
- Import Android.view.View;
- Public class Bitmapshaderview 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 Bitmapshaderview (context context) {
- super (context);
- //Get images
- Bitmap = ((bitmapdrawable) getresources (). getdrawable (R.drawable.cat))
- . Getbitmap ();
- Bitmapwidth = Bitmap.getwidth ();
- Bitmapheight = Bitmap.getheight ();
- //Construct renderer Bitmapshader
- Bitmapshader = New Bitmapshader (bitmap, shader.tilemode.mirror,shader.tilemode.repeat);
- }
- Public Bitmapshaderview (Context context,attributeset set) {
- Super (context, set);
- }
- @Override
- protected void OnDraw (canvas canvas) {
- //TODO auto-generated method stub
- 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-140,bitmapheight);
- //Draw shapedrawable
- Shapedrawable.draw (canvas);
- }
- }
Android Color Rendering (quad) Bitmapshader bitmap rendering