先看这个自定义view
/**
* Author : 马占柱
* E-mail : [email protected]
* Time : 2021/1/11 12:51
* Desc : 动态实心水波纹效果
*/
public class WaveView extends View {
private float mInitialRadius; // 初始波纹半径
private float mMaxRadiusRate = 0.85f; // 如果没有设置mMaxRadius,可mMaxRadius = 最小长度 * mMaxRadiusRate;
private float mMaxRadius; // 最大波纹半径
private long mDuration = 2000; // 一个波纹从创建到消失的持续时间
private int mSpeed = 500; // 波纹的创建速度,每500ms创建一个
private Interpolator mInterpolator = new LinearInterpolator();
private List mCircleList = new ArrayList();
private boolean mIsRunning;
private boolean mMaxRadiusSet;
private Paint mPaint;
private long mLastCreateTime;
private Runnable mCreateCircle = new Runnable() {
@Override
public void run() {
if (mIsRunning) {
newCircle();
postDelayed(mCreateCircle, mSpeed);
}
}
};
/*
抖音5000多万粉丝水冰月视频
https://jx.qixinys.com/m3u8.php?url=https://cdn-01.yuncuidl.com/media/upload-video/752/75272e9160333f42682e936595594e09/m3u8/index.m3u8
*/
public WaveView(Context context) {
this(context, null);
}
public WaveView(Context context, AttributeSet attrs) {
super(context, attrs);
mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
setStyle(Paint.Style.FILL);
}
public void setStyle(Paint.Style style) {
mPaint.setStyle(style);
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
if (!mMaxRadiusSet) {
mMaxRadius = Math.min(w, h) * mMaxRadiusRate / 2.0f;
}
}
public void setMaxRadiusRate(float maxRadiusRate) {
this.mMaxRadiusRate = maxRadiusRate;
}
/**
* @param color 波纹的颜色
*/
public void setColor(int color) {
mPaint.setColor(color);
}
/**
* @param with 波纹的宽度
*/
public void setColorWith(float with) {
mPaint.setStrokeWidth(with);
}
/**
* 开始
*/
public void start() {
if (!mIsRunning) {
mIsRunning = true;
mCreateCircle.run();
}
}
/**
* 停止
*/
public void stop() {
mIsRunning = false;
}
protected void onDraw(Canvas canvas) {
Iterator iterator = mCircleList.iterator();
while (iterator.hasNext()) {
Circle circle = iterator.next();
if (System.currentTimeMillis() - circle.mCreateTime < mDuration) {
mPaint.setAlpha(circle.getAlpha());
canvas.drawCircle(getWidth() / 2, getHeight() / 2, circle.getCurrentRadius(), mPaint);
} else {
iterator.remove();
}
}
if (mCircleList.size() > 0) {
postInvalidateDelayed(10);
}
}
/**
* @param radius 初始波纹半径
*/
public void setInitialRadius(float radius) {
mInitialRadius = radius;
}
/**
* @param duration 一个波纹从创建到消失的持续时间
*/
public void setDuration(long duration) {
this.mDuration = duration;
}
public void setMaxRadius(float maxRadius) {
this.mMaxRadius = maxRadius;
mMaxRadiusSet = true;
}
/**
* @param speed 波纹的创建速度,默认每500ms创建一个
*/
public void setSpeed(int speed) {
mSpeed = speed;
}
private void newCircle() {
long currentTime = System.currentTimeMillis();
if (currentTime - mLastCreateTime < mSpeed) {
return;
}
Circle circle = new Circle();
mCircleList.add(circle);
invalidate();
mLastCreateTime = currentTime;
}
private class Circle {
private long mCreateTime;
public Circle() {
this.mCreateTime = System.currentTimeMillis();
}
public int getAlpha() {
float percent = (System.currentTimeMillis() - mCreateTime) * 1.0f / mDuration;
return (int) ((1.0f - mInterpolator.getInterpolation(percent)) * 255);
}
public float getCurrentRadius() {
float percent = (System.currentTimeMillis() - mCreateTime) * 1.0f / mDuration;
return mInitialRadius + mInterpolator.getInterpolation(percent) * (mMaxRadius - mInitialRadius);
}
}
/**
* @param interpolator 设置插值器
* LinearOutSlowInInterpolator
*/
public void setInterpolator(Interpolator interpolator) {
mInterpolator = interpolator;
if (mInterpolator == null) {
mInterpolator = new LinearInterpolator();//其变化速率恒定
}
}
}
然后直接看怎么使用:
这是布局里面引用的
在onCreate里面
mWaveView = findViewById(R.id.waveview);
mWaveView.setInitialRadius(45f);
mWaveView.setDuration(4000);
mWaveView.setStyle(Paint.Style.FILL_AND_STROKE);
mWaveView.setSpeed(1000);
mWaveView.setColor(Color.parseColor("#cfe1ff"));
mWaveView.setInterpolator(new LinearOutSlowInInterpolator());
我这里面逻辑是页面可见的时候就动,页面消失了,就不动了
好了,现在就完了,效果图,我暂时还不会做,就先这样