Android 之 水波纹 波动效果

package com.example.app20181007;

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 AppView extends View {
    private Context context;
    private Paint mPaintTop;
    private Paint mPaintBottom;
    private Path mPathTop;
    private Path mPathBottom;
    private float φ;
    public AppView(Context context) {
        this(context,null);
    }

    public AppView(Context context, @Nullable AttributeSet attrs) {
        this(context,attrs,0);
    }

    public AppView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.context = context;

        //创建画笔
        mPaintTop = new Paint();
        mPaintBottom = new Paint();
        //设置抗锯齿
        mPaintTop.setAntiAlias(true);
        //设置颜色
        mPaintTop.setColor(Color.BLUE);

        mPaintBottom.setAntiAlias(true);
        mPaintBottom.setColor(Color.WHITE);
        mPaintBottom.setAlpha(60);

        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());

        φ-=0.1f;


        double mY = Math.PI*2/getWidth();//3.6
        //路径移动的坐标
        for(float x = 0;x<=getWidth();x+=20){
            float y = (float) ((13*Math.cos(mY*x+φ))+10);
            mPathTop.lineTo(x, y);
            mPathBottom.lineTo(x, (float) (13*Math.sin(mY*x+φ)));
            listtener.animation(y);
        }

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

        //绘制路径
        canvas.drawPath(mPathTop,mPaintTop);
        canvas.drawPath(mPathBottom,mPaintBottom);

        postInvalidateDelayed(20);
    }

    //设置位置
    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);
    }

    //测量
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

    //接口回调传回y轴坐标,使头像与波浪共同运动

    private  AnimationListtener listtener;
    public void animation(AnimationListtener listener){
        this.listtener = listener;
    }

    public interface AnimationListtener{
        void animation(float y);
    }

}
package com.example.app20181007;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;

public class MainActivity extends AppCompatActivity {

    private ImageView imageView;
    private AppView appView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        imageView =findViewById(R.id.image_logo);
        appView =findViewById(R.id.app_view);
        
        final RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) imageView.getLayoutParams();
        appView.animation(new AppView.AnimationListtener() {
            @Override
            public void animation(float y) {
                layoutParams.setMargins(0,0,0, (int) y-40);
                imageView.setLayoutParams(layoutParams);
            }
        });
    }
}

猜你喜欢

转载自blog.csdn.net/qq_42234894/article/details/82960994