android 自定义波浪线,自定义波浪线

package com.bw.weeknine;

import android.animation.ValueAnimator;

import android.content.Context;

import android.content.res.TypedArray;

import android.graphics.Canvas;

import android.graphics.Color;

import android.graphics.Paint;

import android.graphics.Path;

import android.util.AttributeSet;

import android.view.View;

import android.view.animation.LinearInterpolator;

public class WaveView extends View {

private int width = 0;

private int height = 0;

private int baseHeight = 0;// 波浪高度

private int waveHeight = 100;// 波峰波、波谷高度

private int waveWidth;//波浪宽度

private float offset = 0;//偏移量

private Paint paint;

public WaveView(Context context, AttributeSet attrs) {

super(context, attrs);

initPaint();

}

/**

* 设置开启动画

*/

private void startAni() {

ValueAnimator animator = ValueAnimator.ofFloat(0, waveWidth);

animator.setInterpolator(new LinearInterpolator());

animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {

@Override

public void onAnimationUpdate(ValueAnimator animation) {

float animatorValue = (float) animation.getAnimatedValue();

offset = animatorValue;

postInvalidate();

}

});

animator.setDuration(1000);

animator.setRepeatCount(ValueAnimator.INFINITE);

animator.start();

}

@Override

protected void onDraw(Canvas canvas) {

super.onDraw(canvas);

canvas.drawPath(getPath(), paint);

}

//初始化paint

private void initPaint() {

paint = new Paint();

paint.setColor(Color.parseColor("#88FFFFFF"));

paint.setStyle(Paint.Style.FILL);

}

@Override

protected void onLayout(boolean changed, int left, int top, int right, int bottom) {

super.onLayout(changed, left, top, right, bottom);

width = getMeasuredWidth();

height = getMeasuredHeight();

waveWidth = width;

baseHeight = height / 2;

startAni();

}

/**

* 生成path

*

* @return

*/

private Path getPath() {

int itemWidth = waveWidth / 2;//波长

Path path = new Path();

path.moveTo(-itemWidth * 3 , baseHeight);//起始点坐标

for (int count = -3; count < 2; count++) {

int controlX = count * itemWidth;

path.quadTo(controlX + itemWidth / 2 + offset,//控制点的X

getWaveHeight(count),//控制点的Y

controlX + itemWidth + offset,//结束点的X

baseHeight//结束点的Y

);

}

//封闭区域

path.lineTo(width, height);

path.lineTo(0, height);

path.close();

return path;

}

//计算波峰、波谷

private int getWaveHeight(int num) {

if (num % 2 == 0) {

return baseHeight - waveHeight;

}

return baseHeight + waveHeight;

}

}

布局

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:background="#f000"

/>

value下的attrs

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值