自定义View实现水波纹
View中
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
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 φ;
public WaterView(Context context) {
super(context);
init(context);
}
public WaterView(Context context, AttributeSet attrs) {
super(context, attrs);
init(context);
}
public WaterView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(context);
}
private void init(Context context) {
mpainttop = new Paint();
mpainttop.setColor(Color.WHITE);
mpainttop.setAntiAlias(true);
mpaintbottom = new Paint();
mpaintbottom.setColor(Color.WHITE);
mpaintbottom.setAntiAlias(true);
mpaintbottom.setAlpha(40);
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 i = 0; i <= getWidth(); i += 20) {
float y = (float) (10 * Math.cos(my * i + φ) + 10);
mpathtop.lineTo(i, y);
mpathbottom.lineTo(i, (float) (10 * Math.sin(my * i + φ)));
//调用接口 图片随着 mpathtop轴动
listener.animation(y);
}
//结束
mpathtop.lineTo(getRight(), getBottom());
mpathbottom.lineTo(getRight(), getBottom());
canvas.drawPath(mpathtop, mpainttop);
canvas.drawPath(mpathbottom, mpaintbottom);
//更新
postInvalidateDelayed(20);
}
/**
*作用:图片随着水波纹动
*/
private AnimationListener listener;
public void setAnimation(AnimationListener listener) {
this.listener = listener;
}
public interface AnimationListener {
void animation(float y);
}
}
Layout中
<?xml version="1.0" encoding="utf-8"?><RelativeLayout
android:layout_width="match_parent"
android:layout_height="200dp"
android:background="#d43c3c">
<ImageView
android:id="@+id/img"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="10dp"
android:src="@mipmap/ic_launcher_round" />
<com.bwie.activity.xujiahui20181007.WaterView
android:id="@+id/water"
android:layout_width="match_parent"
android:layout_height="10dp"
android:layout_alignParentBottom="true"></com.bwie.activity.xujiahui20181007.WaterView>
</RelativeLayout>
Activity中
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.ImageView;
import android.widget.RelativeLayout;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ImageView view = findViewById(R.id.img);
final WaterView water = findViewById(R.id.water);
final RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) view.getLayoutParams();
water.setAnimation(new WaterView.AnimationListener() {
@Override
public void animation(float y) {
layoutParams.setMargins(0,0,0, (int) y);
view.setLayoutParams(layoutParams);
}
});
}
}