水波纹的例子实现

效果图:

https://img3.mukewang.com/5c428f890001567e02730342.jpg

我们看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"
android:background="#FDE7F1">
<RelativeLayout
android:layout_width="284dp"
android:layout_height="284dp"
android:layout_centerHorizontal="true"
android:layout_marginTop="45dp"
android:background="@drawable/background">

    <nickgao.com.viewpagerswitchexample.view.WaveView
        android:id="@+id/wave_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_margin="36dp" />

    <ProgressBar
        android:id="@+id/temperature_device_loading_progress"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_margin="36dp"
        android:indeterminateDrawable="@drawable/device_loading_bg" />

</RelativeLayout>

</RelativeLayout>

activity中调用:

WaveView mRateWave = (WaveView)findViewById(R.id.wave_view);
mRateWave.setWaveColor(getResources().getColor(R.color.color_9ff0f0), getResources().getColor(R.color.color_5ad3d3));
mRateWave.setBorder(DensityUtil.dp2px(this, 2), getResources().getColor(R.color.color_9ff0f0));
mWaveHelper = new WaveHelper(mRateWave);
mRateWave.setShowWave(true);
mRateWave.setWaveLengthRatio(1f);
mWaveHelper.start(0.7f);

WaveView的代码是:
public class WaveView extends View {
private static final float DEFAULT_AMPLITUDE_RATIO = 0.05f;
private static final float DEFAULT_WATER_LEVEL_RATIO = 0.5f;
private static final float DEFAULT_WAVE_LENGTH_RATIO = 1.0f;
private static final float DEFAULT_WAVE_SHIFT_RATIO = 0.0f;
public static final int DEFAULT_BEHIND_WAVE_COLOR = Color.parseColor("#28FFFFFF");
public static final int DEFAULT_FRONT_WAVE_COLOR = Color.parseColor("#3CFFFFFF");
public static final ShapeType DEFAULT_WAVE_SHAPE = ShapeType.CIRCLE;

public enum ShapeType {
    CIRCLE,
    SQUARE
}

// if true, the shader will display the wave
private boolean mShowWave;

// shader containing repeated waves
private BitmapShader mWaveShader;
// shader matrix
private Matrix mShaderMatrix;
// paint to draw wave
private Paint mViewPaint;
// paint to draw border
private Paint mBorderPaint;

private float mDefaultAmplitude;
private float mDefaultWaterLevel;
private float mDefaultWaveLength;
private double mDefaultAngularFrequency;

private float mAmplitudeRatio = DEFAULT_AMPLITUDE_RATIO;
private float mWaveLengthRatio = DEFAULT_WAVE_LENGTH_RATIO;
private float mWaterLevelRatio = DEFAULT_WATER_LEVEL_RATIO;
private float mWaveShiftRatio = DEFAULT_WAVE_SHIFT_RATIO;

private int mBehindWaveColor = DEFAULT_BEHIND_WAVE_COLOR;
private int mFrontWaveColor = DEFAULT_FRONT_WAVE_COLOR;
private ShapeType mShapeType = DEFAULT_WAVE_SHAPE;

public WaveView(Context context) {
    super(context);
    init();
}

public WaveView(Context context, AttributeSet attrs) {
    super(context, attrs);
    init();
}

public WaveView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    init();
}

private void init() {
    mShaderMatrix = new Matrix();
    mViewPaint = new Paint();
    mViewPaint.setAntiAlias(true);
}

public float getWaveShiftRatio() {
    return mWaveShiftRatio;
}

/**
 * Shift the wave horizontally according to <code>waveShiftRatio</code>.
 *
 * @param waveShiftRatio Should be 0 ~ 1. Default to be 0.
 *                       Result of waveShiftRatio multiples width of WaveView is the length to shift.
 */
public void setWaveShiftRatio(float waveShiftRatio) {
    if (mWaveShiftRatio != waveShiftRatio) {
        mWaveShiftRatio = waveShiftRatio;
        invalidate();
    }
}

public float getWaterLevelRatio() {
    return mWaterLevelRatio;
}

/**
 * Set water level according to <code>waterLevelRatio</code>.
 *
 * @param waterLevelRatio Should be 0 ~ 1. Default to be 0.5.
 *                        Ratio of water level to WaveView height.
 */
public void setWaterLevelRatio(float waterLevelRatio) {
    if (mWaterLevelRatio != waterLevelRatio) {
        mWaterLevelRatio = waterLevelRatio;
        invalidate();
    }
}

public float getAmplitudeRatio() {
    return mAmplitudeRatio;
}

/**
 * Set vertical size of wave according to <code>amplitudeRatio</code>
 *
 * @param amplitudeRatio Default to be 0.05. Result of amplitudeRatio + waterLevelRatio should be less than 1.
 *                       Ratio of amplitude to height of WaveView.
 */
public void setAmplitudeRatio(float amplitudeRatio) {
    if (mAmplitudeRatio != amplitudeRatio) {
        mAmplitudeRatio = amplitudeRatio;
        invalidate();
    }
}

public float getWaveLengthRatio() {
    return mWaveLengthRatio;
}

/**
 * Set horizontal size of wave according to <code>waveLengthRatio</code>
 *
 * @param waveLengthRatio Default to be 1.
 *                        Ratio of wave length to width of WaveView.
 */
public void setWaveLengthRatio(float waveLengthRatio) {
    mWaveLengthRatio = waveLengthRatio;
}

public boolean isShowWave() {
    return mShowWave;
}

public void setShowWave(boolean showWave) {
    mShowWave = showWave;
}

public void setBorder(int width, int color) {
    if (mBorderPaint == null) {
        mBorderPaint = new Paint();
        mBorderPaint.setAntiAlias(true);
        mBorderPaint.setStyle(Style.STROKE);
    }
    mBorderPaint.setColor(color);
    mBorderPaint.setStrokeWidth(width);

    invalidate();
}

public void setWaveColor(int behindWaveColor, int frontWaveColor) {
    mBehindWaveColor = behindWaveColor;
    mFrontWaveColor = frontWaveColor;

    if (getWidth() > 0 && getHeight() > 0) {
        // need to recreate shader when color changed
        mWaveShader = null;
        createShader();
        invalidate();
    }
}

public void setShapeType(ShapeType shapeType) {
    mShapeType = shapeType;
    invalidate();
}

@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
    super.onSizeChanged(w, h, oldw, oldh);

    createShader();
}

/**
 * Create the shader with default waves which repeat horizontally, and clamp vertically
 */
private void createShader() {
    mDefaultAngularFrequency = 2.0f * Math.PI / DEFAULT_WAVE_LENGTH_RATIO / getWidth();
    mDefaultAmplitude = getHeight() * DEFAULT_AMPLITUDE_RATIO;
    mDefaultWaterLevel = getHeight() * DEFAULT_WATER_LEVEL_RATIO;
    mDefaultWaveLength = getWidth();

    Bitmap bitmap = Bitmap.createBitmap(getWidth(), getHeight(), Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(bitmap);

    Paint wavePaint = new Paint();
    wavePaint.setStrokeWidth(2);
    wavePaint.setAntiAlias(true);

    // Draw default waves into the bitmap
    // y=Asin(ωx+φ)+h
    final int endX = getWidth() + 1;
    final int endY = getHeight() + 1;

    float[] waveY = new float[endX];

    wavePaint.setColor(mBehindWaveColor);
    for (int beginX = 0; beginX < endX; beginX++) {
        double wx = beginX * mDefaultAngularFrequency;
        float beginY = (float) (mDefaultWaterLevel + mDefaultAmplitude * Math.sin(wx));
        canvas.drawLine(beginX, beginY, beginX, endY, wavePaint);

        waveY[beginX] = beginY;
    }

    wavePaint.setColor(mFrontWaveColor);
    final int wave2Shift = (int) (mDefaultWaveLength / 4);
    for (int beginX = 0; beginX < endX; beginX++) {
        canvas.drawLine(beginX, waveY[(beginX + wave2Shift) % endX], beginX, endY, wavePaint);
    }

    // use the bitamp to create the shader
    mWaveShader = new BitmapShader(bitmap, Shader.TileMode.REPEAT, Shader.TileMode.CLAMP);
    mViewPaint.setShader(mWaveShader);
}

@Override
protected void onDraw(Canvas canvas) {
    // modify paint shader according to mShowWave state
    if (mShowWave && mWaveShader != null) {
        // first call after mShowWave, assign it to our paint
        if (mViewPaint.getShader() == null) {
            mViewPaint.setShader(mWaveShader);
        }

        // sacle shader according to mWaveLengthRatio and mAmplitudeRatio
        // this decides the size(mWaveLengthRatio for width, mAmplitudeRatio for height) of waves
        mShaderMatrix.setScale(
                mWaveLengthRatio / DEFAULT_WAVE_LENGTH_RATIO,
                mAmplitudeRatio / DEFAULT_AMPLITUDE_RATIO,
                0,
                mDefaultWaterLevel);
        // translate shader according to mWaveShiftRatio and mWaterLevelRatio
        // this decides the start position(mWaveShiftRatio for x, mWaterLevelRatio for y) of waves
        mShaderMatrix.postTranslate(
                mWaveShiftRatio * getWidth(),
                (DEFAULT_WATER_LEVEL_RATIO - mWaterLevelRatio) * getHeight());

        // assign matrix to invalidate the shader
        mWaveShader.setLocalMatrix(mShaderMatrix);

        float borderWidth = mBorderPaint == null ? 0f : mBorderPaint.getStrokeWidth();
        switch (mShapeType) {
            case CIRCLE:
                if (borderWidth > 0) {
                    canvas.drawCircle(getWidth() / 2f, getHeight() / 2f,
                            (getWidth() - borderWidth) / 2f - 1f, mBorderPaint);
                }
                float radius = getWidth() / 2f - borderWidth;
                canvas.drawCircle(getWidth() / 2f, getHeight() / 2f, radius, mViewPaint);
                break;
            case SQUARE:
                if (borderWidth > 0) {
                    canvas.drawRect(
                            borderWidth / 2f,
                            borderWidth / 2f,
                            getWidth() - borderWidth / 2f - 0.5f,
                            getHeight() - borderWidth / 2f - 0.5f,
                            mBorderPaint);
                }
                canvas.drawRect(borderWidth, borderWidth, getWidth() - borderWidth,
                        getHeight() - borderWidth, mViewPaint);
                break;
        }
    } else {
        mViewPaint.setShader(null);
    }
}

}

github:https://github.com/nickgao1986/WaveHeader

如果你觉得效果好,请帮忙点个赞,谢谢

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 201,784评论 5 474
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 84,745评论 2 378
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 148,702评论 0 335
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,229评论 1 272
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,245评论 5 363
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,376评论 1 281
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,798评论 3 393
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,471评论 0 256
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,655评论 1 295
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,485评论 2 318
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,535评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,235评论 3 318
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,793评论 3 304
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,863评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,096评论 1 258
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 42,654评论 2 348
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,233评论 2 341

推荐阅读更多精彩内容