码迷,mamicode.com
首页 > 移动开发 > 详细

android 水波纹

时间:2015-10-17 23:44:57      阅读:297      评论:0      收藏:0      [点我收藏+]

标签:

public class MyWaveView extends View{
    
    private ArrayList<Wave> list;
    private int[] colors = {Color.RED,Color.BLUE,
            Color.DKGRAY,Color.GREEN,Color.YELLOW};
    public boolean isRunning = false;

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


    public MyWaveView(Context context, AttributeSet attrs) {
        this(context, attrs,0);
    }
    

    public MyWaveView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        
        list = new ArrayList<MyWaveView.Wave>();
        
    }
    
    
    //用户用手触摸时触发
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN:
        case MotionEvent.ACTION_MOVE:
            int x = (int) event.getX();
            int y = (int) event.getY();
        
            //创建水波纹,并放到集合中;
            addWave(x, y);
            break;
        default:
            break;
        }
        return true;
    }
    
    
    public void addWave(int x,int y ){
        Wave wave = new Wave();
        //确定圆心
        wave.rX = x;
        wave.rY = y;
        //确定半径 radius: 0--->
        
        Paint paint = new Paint();
        paint.setColor(colors[(int)(Math.random()*5)]);
        paint.setAntiAlias(true);
        paint.setStyle(Paint.Style.STROKE);
        wave.paint = paint;
        addWaveToList(wave,x,y);
    }
    
    private void addWaveToList(Wave wave,int x,int y) {
        if(list.size() == 0){
            list.add(wave);
            isRunning = true;
            handler.sendEmptyMessage(0);
        }else{
            if(Math.abs(x - (list.get(list.size() -1)).rX) > 50
                    || Math.abs(y - (list.get(list.size() -1)).rY) > 50){
                list.add(wave);
            }
        }
        
    }
    
    Handler handler = new Handler(){
        public void handleMessage(android.os.Message msg) {
            refreshPaint();
            invalidate();
            
            if(isRunning){
                handler.sendEmptyMessageDelayed(0, 18);
            }
        };
    };
    
    
    private void refreshPaint(){
        for(int i = 0;i< list.size();i++){
            Wave wave = list.get(i);
            wave.radius += 4;
            
            int alpha = wave.paint.getAlpha();
            
            alpha -= 5;
            if(alpha < 5 ){
                alpha = 0;
            }
            wave.paint.setAlpha(alpha);
            if(alpha == 0){
                list.remove(i);
                
            }
            wave.radius = wave.radius + 3;
            wave.paint.setStrokeWidth(wave.radius/3);
            
            if(list.size() == 0){
                isRunning = false;
            }
        }
    }

    //水波纹对象
    public class Wave{
        public int radius;
        public int rX;
        public int rY;
        public Paint paint;
        
    }
    
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        
        for(int i = 0; i < list.size();i++ ){
            Wave wave = list.get(i);
            canvas.drawCircle(wave.rX, wave.rY, wave.radius, wave.paint);
        }
    }
}

 

android 水波纹

标签:

原文地址:http://www.cnblogs.com/wei1228565493/p/4888536.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!