自定义View 水波纹

public class WaterView extends View {

private Path path;
private Paint paint;
private float mX;

public WaterView(Context context) {
    this(context,null);
}

public WaterView(Context context, AttributeSet attrs) {
    this(context, attrs,-1);
}

public WaterView(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    init(context);
}

private void init(Context context) {

    path = new Path();
    paint = new Paint();

    paint.setAntiAlias(true);
    paint.setStrokeWidth(5);
    paint.setColor(Color.RED);

}

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);

    path.reset();

    path.moveTo(getLeft(),getBottom());

    float mPi = (float) (Math.PI * 2 / getRight());

    mX -= 0.1f;
    for (int x=0;x<=getRight();x+=20){
        path.lineTo(x, (float) (10*Math.sin(mPi*x+mX)+50));
    }

    path.lineTo(getRight(),getBottom());

    canvas.drawPath(path,paint);

    postInvalidateDelayed(20);
}

}

上一篇:第一阶段末尾


下一篇:drf 自带token学习记录