WaterView//自定义View
package com.example.dell.ds_week01_lx01;
import android.content.Context;
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 paintTop,paintBottom;
private Path pathBottom,pathTop;
private float φ;
public WaterView(Context context) {
super( context );
init(context);
}
public WaterView(Context context, @Nullable AttributeSet attrs) {
super( context, attrs );
init(context);
}
public WaterView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super( context, attrs, defStyleAttr );
init(context);
}
private void init(Context context) {
paintTop = new Paint();
paintTop.setColor( Color.WHITE );
paintTop.setAntiAlias( true );
//创建画笔
paintBottom = new Paint();
//设置颜色为蓝色
paintBottom.setColor( Color.WHITE );
//设置抗锯齿为true
paintBottom.setAntiAlias( true );
paintBottom.setAlpha( 60 );
pathTop = new Path();
pathBottom = new Path();
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw( canvas );
pathTop.reset();
pathBottom.reset();
//设置路径其实位置
pathTop.moveTo( getLeft(),getBottom() );
pathBottom.moveTo( getLeft(),getBottom() );
φ-=0.1f;
double pi = Math.PI * 2 / getWidth();
//设置路径移动的坐标
for ( float x=0;x<getWidth();x+=20){
float y = (float) (10*Math.cos( pi*x+φ )+10 );
pathTop.lineTo( x,y );
pathBottom.lineTo(x, (float) (10*Math.sin(pi*x+φ)));
listener.animation(y);
}
//设置路径结束位置
pathTop.lineTo( getRight(),getBottom() );
pathBottom.lineTo( getRight(),getBottom() );
//设置绘制路径
canvas.drawPath( pathTop,paintTop );
canvas.drawPath( pathBottom,paintBottom );
postInvalidateDelayed( 20 );
}
private AnimationListener listener;
//传递接口
public void animation(AnimationListener listener){
this.listener=listener;
}
public interface AnimationListener{
void animation(float y);
}
}
//布局界面
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".WaterActivity">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="200dp"
android:background="#d43c3c">
<ImageView
android:id="@+id/login"
android:layout_centerHorizontal="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/water"
android:src="@mipmap/ic_launcher_round" />
<com.example.dell.ds_week01_lx01.WaterView
android:id="@+id/water"
android:layout_width="match_parent"
android:layout_height="20dp"
android:layout_alignParentBottom="true" />
</RelativeLayout>
</RelativeLayout>
//WaterActivity 页面
package com.example.dell.ds_week01_lx01;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
public class WaterActivity extends AppCompatActivity {
private WaterView water;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate( savedInstanceState );
setContentView( R.layout.activity_water );
final ImageView image = (ImageView) findViewById( R.id.login );
water = (WaterView) findViewById( R.id.water );
final RelativeLayout.LayoutParams params= (RelativeLayout.LayoutParams) image.getLayoutParams();
water.animation( new WaterView.AnimationListener() {
@Override
public void animation(float y) {
params.setMargins( 0,0,0, (int) y );
image.setLayoutParams( params );
}
} );
}
}
//activity_main布局页面
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Button
android:id="@+id/sbw"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="水波纹" />
</android.support.constraint.ConstraintLayout>
//MainActivity页面
//点击跳转 跳到水波纹页面 也就是WaterActivity
package com.example.dell.ds_week01_lx01;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate( savedInstanceState );
setContentView( R.layout.activity_main );
Button btn = findViewById( R.id.sbw );
btn.setOnClickListener( new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity( new Intent( MainActivity.this,WaterActivity.class ) );
}
} );
}
}