Android Color Rendering (vii) Radialgradient loop rendering for water ripple effect

Source: Internet
Author: User

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
  1. Package com.tony.testshader;
  2. Import Android.os.Bundle;
  3. Import android.app.Activity;
  4. Import Android.graphics.Bitmap;
  5. Import Android.graphics.BitmapFactory;
  6. Import Android.view.Menu;
  7. Import Android.widget.SeekBar;
  8. Import Android.widget.SeekBar.OnSeekBarChangeListener;
  9. Public class Mainactivity extends Activity implements onseekbarchangelistener{
  10. private Waterripplesview Waterripplesview;
  11. @Override
  12. protected void OnCreate (Bundle savedinstancestate) {
  13. super.oncreate (savedinstancestate);
  14. Waterripplesview = New Waterripplesview (this);
  15. Setcontentview (Waterripplesview);
  16. }
  17. @Override
  18. Public Boolean oncreateoptionsmenu (Menu menu) {
  19. Getmenuinflater (). Inflate (R.menu.main, menu);
  20. return true;
  21. }
  22. }


Waterripplesview:

[Java]View PlainCopy
    1. Package com.tony.testshader;
    2. Import Android.content.Context;
    3. Import Android.graphics.Bitmap;
    4. Import Android.graphics.BitmapShader;
    5. Import Android.graphics.Canvas;
    6. Import Android.graphics.Color;
    7. Import Android.graphics.Paint;
    8. Import android.graphics.RadialGradient;
    9. Import Android.graphics.Shader;
    10. Import android.graphics.drawable.BitmapDrawable;
    11. Import android.graphics.drawable.ShapeDrawable;
    12. Import Android.graphics.drawable.shapes.OvalShape;
    13. Import Android.util.AttributeSet;
    14. Import Android.util.DisplayMetrics;
    15. Import android.view.MotionEvent;
    16. Import Android.view.View;
    17. /**
    18. * Water Ripple Effect
    19. * @author Tony
    20. *
    21. */
    22. public class Waterripplesview extends View {
    23. Shader mbitmapshader = null;
    24. Bitmap mbitmappn = null;
    25. Paint mpaint = null;
    26. Shader mradialgradient = null;
    27. Canvas Mcanvas = null;
    28. shapedrawable mshapedrawable = null;
    29. Public Waterripplesview (context context) {
    30. super (context);
    31. //initialization work
    32. Bitmap bitmaptemp = ((bitmapdrawable) getresources (). Getdrawable (
    33. r.drawable.leaf)). Getbitmap ();
    34. Displaymetrics DM = getresources (). Getdisplaymetrics ();
    35. //Create a picture that matches the size of the currently used device window
    36. MBITMAPPN = Bitmap.createscaledbitmap (Bitmaptemp, Dm.widthpixels,
    37. Dm.heightpixels, true);
    38. //Create Bitmapshader Object
    39. Mbitmapshader = New Bitmapshader (Mbitmappn, Shader.TileMode.REPEAT,
    40. Shader.TileMode.MIRROR);
    41. Mpaint = new Paint ();
    42. }
    43. Public Waterripplesview (context context, AttributeSet attrs) {
    44. Super (context, attrs);
    45. }
    46. @Override
    47. protected void OnDraw (canvas canvas) {
    48. //TODO auto-generated method stub
    49. Super.ondraw (canvas);
    50. //Crop the picture to an oval shape
    51. //Create a Shapedrawable object and define the shape as an ellipse
    52. mshapedrawable = New Shapedrawable (new OvalShape ()); OvalShape: Oval
    53. //Set the ellipse to draw something for shapedrawable picture
    54. Mshapedrawable.getpaint (). Setshader (Mbitmapshader);
    55. //Set display area
    56. Mshapedrawable.setbounds (0, 0, Mbitmappn.getwidth (),
    57. Mbitmappn.getheight ());
    58. //Draw shapedrawable
    59. Mshapedrawable.draw (canvas);
    60. if (mradialgradient! = null) {
    61. Mpaint.setshader (mradialgradient);
    62. Canvas.drawcircle (0, 0, mpaint);
    63. }
    64. }
    65. //@ Overwrite touch screen event
    66. Public Boolean ontouchevent (Motionevent event) {
    67. //@ Set alpha channel (transparency)
    68. Mpaint.setalpha (400);
    69. Mradialgradient = New Radialgradient (Event.getx (), Event.gety (),
    70. new int[] {color.white, color.transparent},null, Shader.TileMode.REPEAT);
    71. //@ Redraw
    72. Postinvalidate ();
    73. return true;
    74. }
    75. }

Android Color Rendering (vii) Radialgradient loop rendering for water ripple effect

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.