Android Custom Control to Realize Water Ripple Effect
- 2021-11-01 04:34:27
- OfStack
In this paper, we share the specific code of Android custom control to realize water ripple for your reference, the specific contents are as follows
Sample code:
MainActivity.java
package com.example.mhy.shuibowen;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<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"
tools:context="com.example.mhy.shuibowen.MainActivity">
<com.example.mhy.shuibowen.MyRingWave
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</RelativeLayout>
MyRingWave.java
package com.example.mhy.shuibowen;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Handler;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import java.util.ArrayList;
/**
* Water ripple effect
* Created by mhy on 2016/6/16.
*/
public class MyRingWave extends View {
/**
* 2 Minimum distance between the center points of adjacent waves
*/
private static final int DIS_SOLP = 13;
protected boolean isRunning = false;
private ArrayList<Wave> wList;
public MyRingWave(Context context,AttributeSet attrs) {
super(context, attrs);
wList = new ArrayList<MyRingWave.Wave>();
}
@Override
protected void onDraw(Canvas canvas) {
for(int i=0; i<wList.size(); i++) {
Wave wave = wList.get(i);
canvas.drawCircle(wave.cx, wave.cy, wave.r, wave.p);
}
}
@Override
public boolean onTouchEvent(MotionEvent event) {
super.onTouchEvent(event);
switch(event.getAction()) {
case MotionEvent.ACTION_DOWN:
case MotionEvent.ACTION_MOVE:
int x = (int) event.getX();
int y = (int) event.getY();
addPoint(x, y);
break;
default:
break;
}
return true;
}
private Handler handler = new Handler(){
public void handleMessage(android.os.Message msg) {
// Refresh data
flushData();
// Refresh page
invalidate();
// Loop animation
if (isRunning) {
handler.sendEmptyMessageDelayed(0, 50);
}
}
};
/**
* Refresh data
*/
private void flushData() {
for (int i = 0; i < wList.size(); i++) {
Wave w = wList.get(i);
// If the transparency is 0 Delete from the collection
int alpha = w.p.getAlpha();
if(alpha == 0){
wList.remove(i); // Delete i In the future, i The value of should be subtracted again 1 Otherwise it will be missed 1 Objects, however, there is little influence here, and the effect can't be seen.
continue;
}
alpha-=5;
if(alpha<5){
alpha =0;
}
// Reduce transparency
w.p.setAlpha(alpha);
// Expanded radius
w.r = w.r+3;
// Set radius thickness
w.p.setStrokeWidth(w.r/3);
}
/*
* If the collection is emptied, stop refreshing the animation
*/
if(wList.size() == 0){
isRunning = false;
}
}
/**
* Add a new wave center point
* @param x
* @param y
*/
private void addPoint(int x, int y) {
if(wList.size() == 0) {
addPoint2List(x, y);
isRunning = true;
handler.sendEmptyMessage(0);
}else{
Wave w = wList.get(wList.size()-1);
if(Math.abs(w.cx - x)>DIS_SOLP || Math.abs(w.cy-y)>DIS_SOLP){
addPoint2List(x,y);
}
};
}
/**
* Add new waves
* @param x
* @param y
*/
private void addPoint2List(int x, int y) {
Wave w = new Wave();
w.cx = x;
w.cy=y;
Paint pa=new Paint();
pa.setColor(colors[(int)(Math.random()*4)]);
pa.setAntiAlias(true);
pa.setStyle(Paint.Style.STROKE);
w.p = pa;
wList.add(w);
}
private int [] colors = new int[]{Color.BLUE,Color.RED,Color.YELLOW,Color.GREEN};
private class Wave {
// Center of Circle
int cx;
int cy;
// Paintbrush
Paint p;
// Radius
int r;
}
}
MyRing.java
package com.example.mhy.shuibowen;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
/**
* Created by mhy on 2016/6/16.
*/
public class MyRing extends View {
/**
* Center of a circle X Coordinates
*/
private float cx;
/**
* Center of a circle Y Coordinates
*/
private float cy;
/**
* Ring radius
*/
private float radius = 0;
/**
* Default brush
*/
private Paint paint;
private boolean isRuning = false;
public MyRing(Context context, AttributeSet attrs) {
super(context, attrs);
initView();
}
private void initView() {
radius = 0;
paint = new Paint();
paint.setAntiAlias(true);
paint.setStyle(Paint.Style.STROKE); // Hollow circle
paint.setStrokeWidth(radius / 4); // Brush width Radius 4 Divide 1
paint.setColor(Color.GREEN); // Brush color
paint.setAlpha(255); // Opaque
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
if(changed) {
cx = getWidth() / 2;
cy = getHeight() / 2;
}
}
@Override
protected void onDraw(Canvas canvas) {
canvas.drawCircle(cx, cy, radius, paint);
}
@Override
protected void onAttachedToWindow() {
super.onAttachedToWindow();
}
@Override
protected void onDetachedFromWindow() {
super.onDetachedFromWindow();
isRuning = false;
}
@Override
public boolean onTouchEvent(MotionEvent event) {
super.onTouchEvent(event);
if(event.getAction() == MotionEvent.ACTION_DOWN) {
cx = event.getX();
cy = event.getY();
initView();
startAnim();
}
return true;
}
private Handler handler = new Handler(){
@Override
public void handleMessage(Message msg) {
// Setting Transparency
int alpha = paint.getAlpha();
if(alpha == 0) {
isRuning = false;
}
// Transparency Slowly become transparent
alpha = Math.max(0, alpha-10);
paint.setAlpha(alpha);
System.out.println(alpha);
// Set radius
radius += 5;
paint.setStrokeWidth(radius / 3);
invalidate();
if(isRuning) {
handler.sendEmptyMessageDelayed(0, 50);
}
}
};
private void startAnim() {
isRuning = true;
handler.sendEmptyMessageDelayed(0, 50);
}
}