自定义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);
       }
   });
}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值