Android special effect album (10)-click the ripple effect to achieve clear logic and simple implementation

Source: Internet
Author: User

Android special effect album (10)-click the ripple effect to achieve clear logic and simple implementation
Android special effect album (10)-click the ripple effect to achieve clear logic and simple implementation

What I did this time is a bit similar to the previous article, that is, using simple logic to implement some interesting special effects. Recently, I was busy, therefore, the speed of blog updates has to be deducted by time, but it can be ensured that three times a week will look better. Currently, it is dominated by small functions, or some small entry-level blogs, I am not a very good person, and many details of support processing are still not in place, so I have been making up for it. If you don't talk about it, let's take a look at today's results.

It's easy to implement. Let's take a look at how it is implemented!

OnclickRuning
Package com. lgl. onclickruning; 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. util. log; import android. view. motionEvent; import android. view. view;/*** click the ripple animation effect ** @ author LGL **/public class Runing extends View {// Brush private Paint mPaint; // inner circle width private int strokeWidth; // center x private int cx; // center y private int cy; // radius private int radius; // Handler message private static final int FLUSH = 0; public Runing (Context context, AttributeSet attrs) {super (context, attrs); init ();} private void init () {// initialize the Paint brush mPaint = new Paint (); // anti-sawtooth mPaint. setAntiAlias (true); // sets the color mPaint. setColor (Color. BLUE); // set hollow MPaint. setStyle (Style. STROKE); // set the width of the inner circle mPaint. setStrokeWidth (strokeWidth); // sets the transparency to 0-255 mPaint. setAlpha (255); // Initial Value strokeWidth = 0; radius = 0;}/*** draw */@ Override protected void onDraw (Canvas canvas) {super. onDraw (canvas); // draw a circle canvas. drawCircle (cx, cy, radius, mPaint);}/*** touch event */@ Override public boolean onTouchEvent (MotionEvent event) {// determine the gesture to press and lift the switch (event. getAction ()) {Case MotionEvent. ACTION_DOWN:/*** start to draw circles, that is, ripple, so we first need to get the coordinates of the press. In fact, we need to develop such operations, you must first obtain the coordinates */cx = (int) event in advance. getX (); cy = (int) event. getY (); Log. I ("coordinates", "center x:" + cx + "center y:" + cy); break;} // initialize init (); // send handler. sendEmptyMessage (FLUSH); return true;}/*** refresh status */private void flush () {// radius + 10 radius + = 10 each time; // The line width is 1/4 strokeWidth = radius/4 of the radius each time; // reset it to the paint brush mPaint. se TStrokeWidth (strokeWidth); // color gradient. Reduce the color value by 20 at a time. int nextAlpha = mPaint. getAlpha ()-20; // avoid equals to negative number if (nextAlpha <20) {// directly set to transparent nextAlpha = 0;} // continue to reset to the paint brush mPaint. setAlpha (nextAlpha);} private Handler handler = new Handler () {public void handleMessage (android. OS. message msg) {switch (msg. what) {case FLUSH: // change the parameter status flush (); // refresh and execute our painting method invalidate (); // continue to verify transparency, send as long as it is not 0 until transparent if (mPaint. GetAlpha ()! = 0) {handler. sendEmptyMessageDelayed (FLUSH, 100);} break ;}}};}
Layout_main.xml
<code class=" hljs avrasm"><relativelayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent">    <com.lgl.onclickruning.runing android:layout_width="match_parent" android:layout_height="match_parent"></com.lgl.onclickruning.runing></relativelayout></code>

The code is actually very simple. We can implement the simple logic, but don't underestimate this thinking. You can use this logic to implement more interesting special effects, we are looking forward to your exploration.

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.