原理:
最低一条线(水深):y在某一点的高度=sin(x+angle)*pi/180
上面一条波浪:y在某一点的高度=sin(x+angle)*pi/180+20,与最下面的先相差20像素高度
最上面一条波浪:y在某一点的高度=sin(x+angle)*pi/180+50,上面两条线形成一个新的波浪,颜色浅一些。
int height = getHeight();
int width = getWidth();paint1.setColor(Color.rgb(205, 243, 246));
paint2.setAlpha(200);
paint2.setColor(Color.rgb(150, 206, 231));
paint3.setAlpha(150);
paint3.setColor(Color.rgb(89, 186, 231));
double lineX = 0;
double lineY1 = 0;
double lineY2 = 0;
double lineY3 = 0;
for (int i = 0; i < width; i++) {
lineX = i;
if (isRun) {
lineY1 = Math.sin((i + angle) * Math.PI / 180);
lineY2 = 10 * Math.sin((i + angle) * Math.PI / 180) + 20;
lineY3 = 20 * Math.sin((i + angle) * Math.PI / 180) + 50;
} else {
lineY1 = 0;
lineY2 = 20;
lineY3 = 50;
}
canvas.drawLine((int) lineX, (int) (lineY1 + height / 1.5),
(int) lineX + 1, (int) (lineY2 + height / 1.5), paint1);
canvas.drawLine((int) lineX, (int) (lineY2 + height / 1.5),
(int) lineX + 1, (int) (lineY3 + height / 1.5), paint2);
canvas.drawLine((int) lineX, (int) (lineY3 + height / 1.5),
(int) lineX + 1, height, paint3);
}
在线程中更新角度
@Override
public void run() {
// TODO Auto-generated method stub
while (isRun) {
angle++;
if (angle == 360) {
angle = 0;
}
try {
Thread.sleep(1);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
最终效果图