水泼纹

==============================================MainActicty

public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        final ImageView imageView = (ImageView) findViewById(R.id.logo);
        Linshi mWaterView = (Linshi) findViewById(R.id.wave_view);

        final RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) imageView.getLayoutParams();
        mWaterView.animation(new Linshi.AnimationListener() {
            @Override
            public void animation(float y) {
                params.setMargins(0, 0, 0, (int) y);
                imageView.setLayoutParams(params);
            }
        });

    }
}

=======================================Linshi

public class Linshi extends View {

    private Paint paint;
    private Paint paint1;
    private Path path;
    private Path path1;

    private float φ;


    public Linshi(Context context) {
        super(context);
        init(context);
    }


    public Linshi(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }


    public Linshi(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    private void init(Context context) {
        paint = new Paint();
        paint.setColor(Color.WHITE);
        paint.setAntiAlias(true);

        paint1 = new Paint();
        paint1.setColor(Color.WHITE);
        paint1.setAntiAlias(true);
        paint1.setAlpha(60);

        path = new Path();
        path1 = new Path();

    }

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

        //路径起始的位置
        path.moveTo(getLeft(), getBottom());
        path1.moveTo(getLeft(), getBottom());

        //路径终止的位置
        path.lineTo(getRight(), getBottom());
        path1.lineTo(getRight(), getBottom());

        double m = Math.PI * 2 / getWidth();

        float y, y2;
        φ -= 0.1f;

        for (float x = 0; x <= getWidth(); x += 20) {
            y = (float) (10 * Math.cos(m * x + φ)) + 10;
            y2 = (float) (10 * Math.sin(m * x + φ));
            path.lineTo(x, y);
            path1.lineTo(x, y2);
            listener.animation(y);
        }


        canvas.drawPath(path, paint);
        canvas.drawPath(path1, paint1);

        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=".MainActivity">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:background="#d43c3c">

        <ImageView
            android:id="@+id/logo"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:layout_above="@+id/wave_view"
            android:layout_centerHorizontal="true"
           android:src="@drawable/aaaaaaa"
            />

        <day03.bwie.com.day03.Linshi
            android:id="@+id/wave_view"
            android:layout_width="match_parent"
            android:layout_height="20dp"
            android:layout_alignParentBottom="true" />
    </RelativeLayout>

</RelativeLayout>

猜你喜欢

转载自blog.csdn.net/qq_43131935/article/details/82960033