Custom Controls --- inherit the View class (colorful water ripple case), view water ripple

Source: Internet
Author: User

Custom Controls --- inherit the View class (colorful water ripple case), view water ripple

--------------------------------------- View the effect (and animation effect )----------------------------------------------------


Activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent" >    <com.atguigu.myware.MyWave        android:layout_width="match_parent"        android:layout_height="match_parent" /></RelativeLayout>

MainActivity. java

package com.atguigu.myware;import android.os.Bundle;import android.app.Activity;import android.view.Menu;public class MainActivity extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);}}

MyWave. java

Package com. atguigu. myware; import java. util. arrayList; import android. content. context; import android. graphics. canvas; import android. graphics. color; import android. graphics. paint; import android. graphics. paint. style; import android. OS. handler; import android. util. attributeSet; import android. view. motionEvent; import android. view. view;/*** water ripple effect */public class MyWave extends View {/*** defines a water ripple class (a circle includes a circle center, radius, and paint brush (to be drawn )) */private class Wave {// center int pointX; int pointY; // radius int radius; // Paint paint ;} // The minimum distance between the two adjacent wave centers is private static final int DIS_SOLP = 13; // whether there is water wave protected boolean isRunning = false; /*** Step 1 constructor * // defines the set of Water ripple private ArrayList <Wave> wList; public MyWave (Context context, AttributeSet attrs) {super (context, attrs ); // initialize and create a ripple collection wList = new ArrayList <MyWave. wave> ();}/*** touch event */@ Overridepublic boolean onTouchEvent (MotionEvent event) {super. onTouchEvent (event); switch (event. getAction () {// press the screen and slide to generate a water wave case MotionEvent. ACTION_DOWN: case MotionEvent. ACTION_MOVE: // get the int x = (int) event of the Center of down and move. getX (); int y = (int) event. getY (); // if the water ripple set is empty, draw a water ripple if (wList. size () = 0) {// draw the water ripple addPoint2List (x, y); // modify the status, indicating that the water wave generates isRunning = true; // send a message at the moment, it indicates that a water wave handler is drawn. sendEmptyMessage (0);} else {// if the water Wave set is not empty, go to the last drawn ring water Wave w = wList. get (wList. size ()-1); if (Math. abs (w. pointX-x)> DIS_SOLP | Math. abs (w. pointY-y)> DIS_SOLP) {addPoint2List (x, y) ;}; break; default: break;} return true ;} /** define four colors of water waves */private int [] colors = new int [] {Color. BLUE, Color. RED, Color. YELLOW, Color. GREEN};/*** Add a Wave */private void addPoint2List (int x, int y) {// create a water Wave w = new Wave (); // assign the coordinates of sliding or pressing to the water wave class w. pointX = x; w. pointY = y; // create a Paint brush Paint pa = new Paint (); // draw a water wave (ring) to set the color, anti-tooth, shape pa. setColor (colors [(int) (Math. random () * 4)]); pa. setAntiAlias (true); pa. setStyle (Style. STROKE); // assign a value to w. paint = pa; // Add a circular water wList to the set. add (w);}/*** draw water ripple */@ Overrideprotected void onDraw (Canvas canvas) {for (int I = 0; I <wList. size (); I ++) {Wave wave = wList. get (I); canvas. drawCircle (wave. pointX, wave. pointY, wave. radius, wave. paint) ;}}/** use message mechanism */private Handler handler = new Handler () {public void handleMessage (android. OS. message msg) {// receives the sent Message for the corresponding operation // refresh the data flushData (); // refresh the page invalidate (); // cyclically animated if (isRunning) {handler. sendEmptyMessageDelayed (0, 50) ;};};/*** refresh data (operate on the transparency and radius of the annular water wave) */private void flushData () {for (int I = 0; I <wList. size (); I ++) {Wave w = wList. get (I); // If the transparency is 0, delete int alpha = w from the set. paint. getAlpha (); if (alpha = 0) {wList. remove (I); continue;} // opacity values minus 5 alpha-= 5 each time; if (alpha <5) {alpha = 0 ;}// opacity reduction w. paint. setAlpha (alpha); // expand the radius w. radius = w. radius + 3; // set the radius thickness w. paint. setStrokeWidth (w. radius/3);}/** if the set is cleared, stop refreshing the animation */if (wList. size () = 0) {isRunning = false ;}}}

--------------------------- View a simple version ---------------------- the effect is gradually increased until it disappears --------------------------------


Activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent" >    <com.atguigu.myware.MyWave        android:layout_width="match_parent"        android:layout_height="match_parent" /></RelativeLayout>

MainActivity. java

package com.atguigu.myware;import android.os.Bundle;import android.app.Activity;import android.view.Menu;public class MainActivity extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);}}

MyWave. java

Package com. atguigu. myware; import android. content. context; import android. graphics. canvas; import android. graphics. color; import android. graphics. paint; import android. graphics. paint. style; import android. OS. handler; import android. util. attributeSet; import android. view. motionEvent; import android. view. view;/*** watermark effect View **/public class MyWave extends View {private Paint paint; @ Overridepublic boolean on TouchEvent (MotionEvent event) {super. onTouchEvent (event); switch (event. getAction () {/** start to draw the Ring Water ripple when pressing */case MotionEvent. ACTION_DOWN: // 1. set startX = event. getX (); startY = event. getY (); // 2. create a new initView () based on the center of the circle; // 3. draw invalidate (); break; default: break;} return true;} private Handler handler = new Handler () {public void handleMessage (android. OS. message msg) {// 1. transparency changed-small int alpha = paint. getAlpha (); Alpha-= 5; if (alpha <0) {alpha = 0;} // transparency: 0 ~ Between 255 paint. setAlpha (alpha); // 2. larger radius: radius + = 5; paint. setStrokeWidth (radius/3); // 3. draw one more and execute the onDraw method invalidate () ;};/ ** step 1, constructor */public MyWave (Context context, AttributeSet attrs) {super (context, attrs); initView ();}/*** initialize View */private int radius; // radius private void initView () {paint = new Paint (); // set the color to paint. setColor (Color. RED); // set the anti-sawtooth paint. setAntiAlias (true); // set the ring style paint. setStyle (Style. STROKE); // The radius is 5 radius = 5; paint. setStrokeWidth (radius/3);} // circular coordinate private float startX; private float startY;/*** draw circular water ripple */@ Overrideprotected void onDraw (Canvas canvas Canvas) {if (paint. getAlpha ()> 0) {if (startX> 0 & startY> 0) {// draw a circle canvas. drawCircle (startX, startY, radius, paint); // send a delayed message handler while plotting. sendEmptyMessageDelayed (0, 50 );}}}}



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.