静态波形绘制
Posted lucky-zhu
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了静态波形绘制相关的知识,希望对你有一定的参考价值。
背景,需要弄个波形顶部的背景,所以就网上找了个demo来修改所得(技术有限,本人热衷修改(-_-!!))
效果图:
以下为自定义View类没什么好讲的:
import android.content.Context; import android.graphics.Canvas; import android.graphics.Paint; import android.graphics.Path; import android.util.AttributeSet; import android.view.View; import java.util.Random; public class WaveView extends View { private Paint mPaint; private Path mPath; private int mWaveLength = 780; private int dx=0; private int dy; private int width; public WaveView(Context context) { super(context); init(); } public WaveView(Context context, AttributeSet attrs) { super(context, attrs); init(); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); if (width == 0) { width = getMeasuredWidth(); } } private void init() { mPaint = new Paint(); mPaint.setColor(0x6fF2F2F2); mPaint.setStyle(Paint.Style.FILL); mPaint.setAntiAlias(true); mPath = new Path(); } int s = 0; @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); mPath.reset(); //一个周期宽度 mWaveLength = width - 300; int originY = 60; int mWaveHalfLength = mWaveLength / 2; mPath.moveTo(-(mWaveLength * 3 / 4) + 150, originY); //波的幅度 int max = 70; int min = 70; Random random = new Random(); s = random.nextInt(max) % (max - min + 1) + min; // mWaveLength = random.nextInt(maxW) % (maxW - minW + 1) + minW; mPaint.setColor(0xffF2F2F2);//从不同的起点绘制第一条波 draw(canvas, mWaveHalfLength); originY = 80; mPaint.setColor(0x4fF2F2F2); mPath.moveTo(-0, originY);//从不同的起点绘制第二条波 draw(canvas, mWaveHalfLength); mPath.moveTo(-200, originY);//从不同的起点绘制第三条波 draw(canvas, mWaveHalfLength); } private void draw(Canvas canvas, int mWaveHalfLength) { //屏幕的宽度里面放多少个波长 for (int i = -mWaveLength; i < getWidth() + mWaveLength; i += mWaveLength) { //相对绘制二阶贝塞尔曲线(相对于自己的起始点--也即是上一个曲线的终点 的距离dx1) mPath.rQuadTo(mWaveHalfLength / 2, -s, mWaveHalfLength, 0); mPath.rQuadTo(mWaveHalfLength / 2, s, mWaveHalfLength, 0); } //颜色填充 //画一个封闭的空间 mPath.lineTo(getWidth(), getHeight()); mPath.lineTo(0, getHeight()); mPath.close(); canvas.drawPath(mPath, mPaint); } }
以上是关于静态波形绘制的主要内容,如果未能解决你的问题,请参考以下文章