天天看點

自定義水波紋View

Circle.java

public class Circle {
    private float cx;
    private float cy;
    private float radius;
    private Paint paint;
    private int alpha = ;


    public Circle() {
    }

    public Circle(float cx, float cy, float radius, Paint paint) {
        this.cx = cx;
        this.cy = cy;
        this.radius = radius;
        this.paint = paint;
    }

    public float getCx() {
        return cx;
    }

    public void setCx(float cx) {
        this.cx = cx;
    }

    public float getCy() {
        return cy;
    }

    public void setCy(float cy) {
        this.cy = cy;
    }

    public float getRadius() {
        return radius;
    }

    public void setRadius(float radius) {
        this.radius = radius;
    }

    public Paint getPaint() {
        return paint;
    }

    public void setPaint(Paint paint) {
        this.paint = paint;
    }

    public int getAlpha() {
        return alpha;
    }

    public void setAlpha(int alpha) {
        this.alpha = alpha;
    }

    @Override
    public String toString() {
        return "Circle{" +
                "cx=" + cx +
                ", cy=" + cy +
                ", radius=" + radius +
                ", paint=" + paint +
                '}';
    }
}
           
public class WaveView extends View {

    private Random random = new Random();
    private List<Circle> list;
    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            invalidate();
            if (list != null && list.size() != ) {
                mHandler.sendEmptyMessageDelayed(, );
            }else{
                mHandler.removeCallbacksAndMessages(null);
            }
        }
    };

    public WaveView1(Context context) {
        super(context);
    }

    public WaveView1(Context context, AttributeSet attrs) {
        super(context, attrs);

        list = new ArrayList<>();
    }

    public WaveView1(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onDraw(Canvas canvas) {

        if(list!=null&&list.size()!=){
            for (int i = ; i < list.size(); i++) {
                Circle circle = list.get(i);
                if(circle.getAlpha()==){
                    list.remove(i);
                    i--;
                }else{
                    canvas.drawCircle(circle.getCx(),circle.getCy(),circle.getRadius(),circle.getPaint());
                    circle.getPaint().setAlpha(circle.getAlpha() - );
                    circle.setAlpha(circle.getAlpha() - );
                    circle.setRadius(circle.getRadius()+);
                }
            }
        }
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        float x = event.getX();
        float y = event.getY();
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                list.add(new Circle(x, y, , getPaint()));
                invalidate();
                mHandler.sendEmptyMessageDelayed(, );
                return true;

            case MotionEvent.ACTION_MOVE:
                Circle circle = new Circle(x, y, , getPaint());
                list.add(circle);
                invalidate();
                mHandler.sendEmptyMessageDelayed(, );
                break;
            case MotionEvent.ACTION_UP:

                break;
        }
        return true;
    }

    private Paint getPaint() {
        Paint paint = new Paint();
        paint = new Paint();
        paint.setStrokeWidth();
        paint.setStyle(Paint.Style.STROKE);
        paint.setAntiAlias(true);
        paint.setColor(random.nextInt(Integer.MAX_VALUE&) | );
        return paint;
    }
}