自定义控件实现水波纹效果

水波纹效果

  • XML布局中
``
- <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.example.myandroid_test.WaterWave
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        </RelativeLayout>
  • 自定义Vi package com.example.myandroid_test;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;

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;

public class WaterWave extends View {

// 波形的List
private List<Wave> waveList;
// 最大的不透明,完全不透明
private static final int MAX_ALPHA = 255;
protected static final int FLUSH_ALL = -1;
private boolean isStart = true;

// 按下的时候x坐标
private int xDown;
// 按下的时候y坐标
private int yDown;
// 用来表示圆环的半径
private float radius;
private int alpha;

private int[] colors = { Color.BLUE, Color.RED, Color.YELLOW, Color.DKGRAY,
Color.GREEN, Color.LTGRAY, Color.MAGENTA };

// 构造参数
public WaterWave(Context context, AttributeSet attrs) {
super(context, attrs);
waveList = Collections.synchronizedList(new ArrayList<Wave>());
}

// 控件大小
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}

// 画图方法
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
// 重绘所有的圆环
for (int i = 0; i < waveList.size(); i++) {
Wave wave = waveList.get(i);
canvas.drawCircle(wave.xDown, wave.yDown, wave.radius, wave.paint);
}

}

// 初始化画笔方法
private Paint initPaint(int alpha, float width) {
Paint paint = new Paint();
// 抗锯齿
paint.setAntiAlias(true);
// 设置圆环厚度
paint.setStrokeWidth(width);

// 设置环形的方式绘制
paint.setStyle(Paint.Style.STROKE);
// 设置透明度默认
paint.setAlpha(alpha);

paint.setColor(colors[(int) (Math.random() * (colors.length - 1))]);
return paint;
}

// handler
private Handler hd = new Handler() {
public void handleMessage(android.os.Message msg) {
super.handleMessage(msg);
if (msg.what == 0) {
// 刷新状态
flushState();
// 执行刷新OnDraw方法
invalidate();
if (waveList != null && waveList.size() > 0) {
hd.sendEmptyMessageDelayed(0, 100);
}

}
};
};

// 刷新状态方法
private void flushState() {
for (int i = 0; i < waveList.size(); i++) {
Wave wave = waveList.get(i);
if (isStart == false && wave.alpha == 0) {
waveList.remove(i);
wave.paint = null;
wave = null;
continue;

} else if (isStart == true) {
isStart = false;
}
wave.radius += 10;
wave.alpha -= 10;        
if (wave.alpha < 0) {
wave.alpha = 0;
}
wave.width = wave.radius / 4;
wave.paint.setAlpha(wave.alpha);
wave.paint.setStrokeWidth(wave.width);
}
}

// 触摸时间的方法
@Override
public boolean onTouchEvent(MotionEvent event) {
super.onTouchEvent(event);

switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
Wave wave = new Wave();
wave.radius = 0;
wave.alpha = MAX_ALPHA;
wave.width = wave.radius / 4;
wave.xDown = (int) event.getX();
wave.yDown = (int) event.getY();
wave.paint = initPaint(wave.alpha, wave.width);
if (waveList.size() == 0) {
isStart = true;
}
waveList.add(wave);
// 点击之后刷新一次图形
invalidate();
if (isStart) {
hd.sendEmptyMessage(0);
}

case MotionEvent.ACTION_MOVE:
Wave wave1 = new Wave();
wave1.radius = 0;
wave1.alpha = MAX_ALPHA;
wave1.width = wave1.radius / 4;
wave1.xDown = (int) event.getX();
wave1.yDown = (int) event.getY();
wave1.paint = initPaint(wave1.alpha, wave1.width);
if (waveList.size() == 0) {
isStart = true;
}
waveList.add(wave1);
// 点击之后刷新一次图形
invalidate();
if (isStart) {
hd.sendEmptyMessage(0);
}
break;
default:
break;
}

return true;
}

// 波浪类
private class Wave {

int waveX;
int waveY;

float radius;
Paint paint;

int xDown;
int yDown;
float width;
int alpha;

}

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值