Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
What can we do with circular rendering? In fact, many are very common, such as the help of the previous implementation of the sugar effect, colored hot air balloon, such as here to talk about the water ripple effect, or can also be understood as a diffuse color rendering effect
First Look at:
Touch the screen to see the corresponding effect, you can see, to achieve this effect, the use of radialgradient, just a few simple lines of code:
Mainactivity:
[Java]View PlainCopy
- Package com.tony.testshader;
- Import Android.os.Bundle;
- Import android.app.Activity;
- Import Android.graphics.Bitmap;
- Import Android.graphics.BitmapFactory;
- Import Android.view.Menu;
- Import Android.widget.SeekBar;
- Import Android.widget.SeekBar.OnSeekBarChangeListener;
- Public class Mainactivity extends Activity implements onseekbarchangelistener{
- private Waterripplesview Waterripplesview;
- @Override
- protected void OnCreate (Bundle savedinstancestate) {
- super.oncreate (savedinstancestate);
- Waterripplesview = New Waterripplesview (this);
- Setcontentview (Waterripplesview);
- }
- @Override
- Public Boolean oncreateoptionsmenu (Menu menu) {
- Getmenuinflater (). Inflate (R.menu.main, menu);
- return true;
- }
- }
Waterripplesview:
[Java]View PlainCopy
- Package com.tony.testshader;
- Import Android.content.Context;
- Import Android.graphics.Bitmap;
- Import Android.graphics.BitmapShader;
- Import Android.graphics.Canvas;
- Import Android.graphics.Color;
- Import Android.graphics.Paint;
- Import android.graphics.RadialGradient;
- 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.util.DisplayMetrics;
- Import android.view.MotionEvent;
- Import Android.view.View;
- /**
- * Water Ripple Effect
- * @author Tony
- *
- */
- public class Waterripplesview extends View {
- Shader mbitmapshader = null;
- Bitmap mbitmappn = null;
- Paint mpaint = null;
- Shader mradialgradient = null;
- Canvas Mcanvas = null;
- shapedrawable mshapedrawable = null;
- Public Waterripplesview (context context) {
- super (context);
- //initialization work
- Bitmap bitmaptemp = ((bitmapdrawable) getresources (). Getdrawable (
- r.drawable.leaf)). Getbitmap ();
- Displaymetrics DM = getresources (). Getdisplaymetrics ();
- //Create a picture that matches the size of the currently used device window
- MBITMAPPN = Bitmap.createscaledbitmap (Bitmaptemp, Dm.widthpixels,
- Dm.heightpixels, true);
- //Create Bitmapshader Object
- Mbitmapshader = New Bitmapshader (Mbitmappn, Shader.TileMode.REPEAT,
- Shader.TileMode.MIRROR);
- Mpaint = new Paint ();
- }
- Public Waterripplesview (context context, AttributeSet attrs) {
- Super (context, attrs);
- }
- @Override
- protected void OnDraw (canvas canvas) {
- //TODO auto-generated method stub
- Super.ondraw (canvas);
- //Crop the picture to an oval shape
- //Create a Shapedrawable object and define the shape as an ellipse
- mshapedrawable = New Shapedrawable (new OvalShape ()); OvalShape: Oval
- //Set the ellipse to draw something for shapedrawable picture
- Mshapedrawable.getpaint (). Setshader (Mbitmapshader);
- //Set display area
- Mshapedrawable.setbounds (0, 0, Mbitmappn.getwidth (),
- Mbitmappn.getheight ());
- //Draw shapedrawable
- Mshapedrawable.draw (canvas);
- if (mradialgradient! = null) {
- Mpaint.setshader (mradialgradient);
- Canvas.drawcircle (0, 0, mpaint);
- }
- }
- //@ Overwrite touch screen event
- Public Boolean ontouchevent (Motionevent event) {
- //@ Set alpha channel (transparency)
- Mpaint.setalpha (400);
- Mradialgradient = New Radialgradient (Event.getx (), Event.gety (),
- new int[] {color.white, color.transparent},null, Shader.TileMode.REPEAT);
- //@ Redraw
- Postinvalidate ();
- return true;
- }
- }
Android Color Rendering (vii) Radialgradient loop rendering for water ripple effect