欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

波浪动态图

程序员文章站 2024-01-19 12:32:28
...

布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    android:orientation="vertical"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >

   <RelativeLayout
       android:layout_width="match_parent"
       android:layout_height="200px"
       android:background="#ff0000"
       >

       <com.bwei.www.waveview.WaveView
           android:layout_width="match_parent"
           android:layout_height="27px"
           android:layout_alignParentBottom="true"
           android:id="@+id/wv"
           />
       <ImageView
           android:id="@+id/img_cursor"
           android:layout_alignParentBottom="true"
           android:layout_centerHorizontal="true"
           android:src="@mipmap/ic_launcher"
           android:layout_width="75dp"
           android:layout_height="75dp" />

   </RelativeLayout>

</LinearLayout>

 

//页面另一个

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.DrawFilter;
import android.graphics.Paint;
import android.graphics.PaintFlagsDrawFilter;
import android.graphics.Path;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;



public class WaveView extends View {
    private float fai = 0;
    private Paint paint1;
    private Paint paint2;
    private Path path1;
    private Path path2;
    private DrawFilter drawFilter;

    public WaveView(Context context) {
        this(context,null);
    }

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

    public WaveView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }
//创建一个接口
    public interface OnWaveChangeListeren{
        void onChange(float y);
    }
    private OnWaveChangeListeren listeren;

    public void setOnWaveChangeListeren(OnWaveChangeListeren listeren){
        this.listeren = listeren;

    }

    private void init() {
        paint1 = new Paint();
        paint1.setColor(Color.WHITE);
        paint1.setAntiAlias(true);
        paint1.setStyle(Paint.Style.FILL);
        paint1.setStrokeWidth(5);

        paint2 = new Paint();
        paint2.setColor(Color.WHITE);
        paint2.setAntiAlias(true);
        paint2.setStrokeWidth(5);
        paint2.setAlpha(60);

        path1 = new Path();
        path2 = new Path();

        drawFilter = new PaintFlagsDrawFilter(0, Paint.FILTER_BITMAP_FLAG|Paint.ANTI_ALIAS_FLAG);

    }

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

        double Ω = 2*Math.PI/getMeasuredWidth();
        canvas.setDrawFilter(drawFilter);

        fai -= 0.1f;
        int A = getMeasuredHeight()/2;

        path1 .reset();
        path2.reset();

        path1.moveTo(getLeft(),getBottom());
        path2.moveTo(getLeft(),getBottom());



        for (int x =0;x<=getMeasuredWidth();x+=20){

            float y1 =  A *(float) Math.sin(Ω * x +fai)+A;
            float y2 = -A * (float)Math.sin(Ω * x + fai) + A;

            if (x>getMeasuredWidth()/2-10&&x<getMeasuredWidth()/2+10){
                listeren.onChange(y2);
            }
            path1.lineTo(x,y1);
            path2.lineTo(x,y2);
        }

        path1.lineTo(getWidth(),getBottom());
        path2.lineTo(getWidth(),getBottom());

        canvas.drawPath(path1,paint1);
        canvas.drawPath(path2,paint2);

        postInvalidateDelayed(30);
    }
}

//主页面

package com.bwei.www.waveview;

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

public class MainActivity extends AppCompatActivity {

    private WaveView wv;
    private ImageView imgCoursor;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        wv = findViewById(R.id.wv);
        imgCoursor = findViewById(R.id.img_cursor);

        WaveView.OnWaveChangeListeren listeren = new WaveView.OnWaveChangeListeren() {
            @Override
            public void onChange(float y) {
                RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) imgCoursor.getLayoutParams();
                layoutParams.setMargins(0,0,0,(int)y);
                imgCoursor.setLayoutParams(layoutParams);
            }
        };
wv.setOnWaveChangeListeren(listeren);
    }
}