package com.bwie.guilin.demolian;
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.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;
public class WaterView extends View {
private Paint mPaintTop;
private Paint mPaintBottom;
private Path mPathTop;
private Path mPathBottom;
private float φ;
private int topcolor,bottomcolor;
public WaterView(Context context) {
super(context);
init(context);
}
public WaterView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
TypedArray ty = context.obtainStyledAttributes(attrs, R.styleable.WaterView);
topcolor = ty.getColor(R.styleable.WaterView_topcolor, Color.WHITE);
bottomcolor = ty.getColor(R.styleable.WaterView_bottom, Color.WHITE);
ty.recycle();
init(context);
}
public WaterView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(context);
}
private void init(Context context) {
mPaintTop = new Paint();
mPaintTop.setColor(topcolor);
mPaintTop.setAntiAlias(true);
mPaintBottom = new Paint();
mPaintBottom.setColor(bottomcolor);
mPaintBottom.setAntiAlias(true);
mPaintBottom.setAlpha(60);
mPathTop = new Path();
mPathBottom = new Path();
}
//绘制
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
mPathTop.reset();
mPathBottom.reset();
//路径起始的位置
mPathTop.moveTo(getLeft(), getBottom());
mPathBottom.moveTo(getLeft(), getBottom());
//获取每个宽度所占的度数
double mY = Math.PI * 2 / getWidth();
//从左往右
φ -= 0.1f;
//路径移动的坐标
for (float x = 0; x <= getWidth(); x += 20) {
float y = (float) (10 * Math.cos(mY * x + φ)+10);
mPathTop.lineTo(x, y);
mPathBottom.lineTo(x, (float) (10 * Math.sin(mY * x + φ)));
listener.ann(y);
}
//路径终止的位置
mPathTop.lineTo(getRight(), getBottom());
mPathBottom.lineTo(getRight(), getBottom());
canvas.drawPath(mPathTop, mPaintTop);
canvas.drawPath(mPathBottom, mPaintBottom);
//更新ui
postInvalidateDelayed(20);
}
private AListener listener;
public void getListener(AListener listener) {
this.listener = listener;
}
//传递y值
public interface AListener {
void ann(float y);
}
}
在 Activitivity里面获取
package com.bwie.guilin.demolian;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ImageView logo = (ImageView) findViewById(R.id.logo);
WaterView waterView = (WaterView) findViewById(R.id.wave_view);
final RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) logo.getLayoutParams();
waterView.getListener(new WaterView.AListener() {
@Override
public void ann(float y) {
params.setMargins(0, 0, 0, (int) y);
logo.setLayoutParams(params);
}
});
}
}