天天看点

android的多点触摸(制作水波涟漪效果)

代码是本人自己编写的,本人还只是个菜鸟,只能说不怎么规范:

效果图:(我的手机只支持5点触控)

android的多点触摸(制作水波涟漪效果)
android的多点触摸(制作水波涟漪效果)

上面的坦克和“欢迎来到我的android界面!”是之前写的,坦克和字幕都是线程控制。

src包:

android的多点触摸(制作水波涟漪效果)

思路:

通过获得手指在屏幕上的点再根据点去画圆,然后将园交给线程去扩大其半径和透明度,当半径和透明度达到一定值时园就会消失,然后从list中移除。(其中要注意的是手指和园不能跟错对象,不能画错地方)

首先必须建立point点类和Cricle类

public class Point {
	float pointx;
	float pointy;
	int pointIndex;
	public Point(float pointx, float pointy, int pointIndex) {
		super();
		this.pointx = pointx;
		this.pointy = pointy;
		this.pointIndex = pointIndex;
	}
	
}
           
public class Cricle {
	float x;
	float y;
	int pointIndex;
	float r=0;//初始半径
	int alpha=255;//初始透明度
	public Cricle(float x,float y,int pointIndex){
		this.x=x;
		this.y=y;
		this.pointIndex=pointIndex;
	}
	
}
           

Myview类:(重点)

线程建议不要写在内部,另外写一个线程类
           
public class MyView extends View {
	public List<Cricle> cs=new ArrayList<Cricle>();
	public List<Point> ps=new ArrayList<Point>();
	Context context;
	ViewThread thread=new ViewThread();
	ViewThread2 thread2=new ViewThread2();
	ViewThread3 thread3=new ViewThread3();
	createCircle thread4=new createCircle();
	float x=100;
	float y=100;
	float x2=80;
	float y2=80;
	int i=0;
	
	public MyView(Context context) {
		super(context);
		this.context=context;
		thread.start();
		thread2.start();
		thread4.start();
		thread3.start();
		

	}
	
	@Override
	public void draw(Canvas canvas) {
		// TODO Auto-generated method stub
		super.draw(canvas);
		Paint paint=new Paint();
		paint.setColor(Color.GREEN);
		paint.setStyle(Style.STROKE);
		//画坦克
		if(i==0){
			canvas.drawRect(x, y, x+20, y+100, paint);
			canvas.drawRect(x+20, y+20, x+70, y+70, paint);
			canvas.drawRect(x+40, y-30, x+50, y+25, paint);
			canvas.drawRect(x+70, y, x+90, y+100, paint);		
			paint.setStyle(Style.FILL);
			canvas.drawRect(x+25, y+25, x+65, y+65, paint);
		}else{
			canvas.drawRect(x, y, x+20, y+100, paint);
			canvas.drawRect(x+20, y+20, x+70, y+70, paint);
			canvas.drawRect(x+40, y+65, x+50, y+120, paint);
			canvas.drawRect(x+70, y, x+90, y+100, paint);		
			paint.setStyle(Style.FILL);
			canvas.drawRect(x+25, y+25, x+65, y+65, paint);
			
		}
		//画字幕
		paint.setColor(Color.RED);
		paint.setTextSize(20);
		canvas.drawText("欢迎来到我的ANDRIOD界面!", x2, y2, paint);
		
		
		//从list中拿到圆并画出来
		for (int i = 0; i < cs.size(); i++) {
			paint.setStyle(Style.STROKE);
			paint.setAntiAlias(true);
			paint.setStrokeWidth(3);
			paint.setColor(Color.argb(cs.get(i).alpha, 0, 0, 255));
			canvas.drawCircle(cs.get(i).x, cs.get(i).y, cs.get(i).r, paint);
			
		}
		
	}
		//控制坦克和字幕X方向的移动
	public class ViewThread extends Thread {
		@Override
		public void run() {
			// TODO Auto-generated method stub
			super.run();
			while(true){
					while(x2>0){
								
					try {
							Thread.sleep(50);
							x2-=5;
							postInvalidate();
						} catch (InterruptedException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						
						}
					}
					
					while(x2<200){		
						try {
							Thread.sleep(50);
							x2+=5;
							postInvalidate();
								
						} catch (InterruptedException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					}
				}
			}
			
		}
	//控制坦克和字幕Y方向的移动
	public class ViewThread2 extends Thread {
		@Override
		public void run() {
			// TODO Auto-generated method stub
			super.run();
			while(true){
					while(y>30){
					i=0;			
					try {
							Thread.sleep(50);
							y-=5;
							y2-=5;
							postInvalidate();
						} catch (InterruptedException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						
						}
					}
					
					while(y<620){	
						i=1;
						try {
							Thread.sleep(50);
							y+=5;
							y2+=5;
								
						} catch (InterruptedException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					}
				}
			}
			
		}
	//线程改变圆的半径和透明度和移除圆
	public class ViewThread3 extends Thread {
		@Override
		public void run() {
			// TODO Auto-generated method stub
			super.run();
			while(true){
					try {
						thread.sleep(300);
						for(int i = 0; i < cs.size(); i++){
						cs.get(i).r+=3;
						cs.get(i).alpha-=8;
						if(cs.get(i).alpha<0){
							cs.remove(cs.get(i));
						}
						postInvalidate();
						}
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					
				}
			}
		}
		
	}
	//线程创建圆
	public class createCircle extends Thread {
		@SuppressWarnings("static-access")
		@Override
		public void run() {
			// TODO Auto-generated method stub
			super.run();
			while(true){
					try {
						thread.sleep(200);
						for(int i = 0; i < ps.size(); i++){
							Cricle c=new Cricle(ps.get(i).pointx, ps.get(i).pointy, ps.get(i).pointIndex); 
							cs.add(c);
						}
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					
				}
			}
		}
		
	}
}
           

MainActivity类:

public class MainActivity extends Activity {
	MyView view;
	GestureDetector gs=null;
	//List<Cricle> cs=null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        view=new MyView(MainActivity.this);
       
        setContentView(view);
      
      
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
    	// TODO Auto-generated method stub
    	/*if(event.getAction()==2){
	    	Cricle c=new Cricle(event.getX(), event.getY());
	    	view.cs.add(c);
    	}*/
    	int action=event.getAction();
    	int actionCode=(action&0x000000ff)>=5?(action&0x000000ff)-5:(action&0x000000ff);
    	int pointIndex=action>>8;
    	int pointId=event.getPointerId(pointIndex);
    	switch(actionCode){
    		case MotionEvent.ACTION_DOWN:
    			Point cp=new Point(event.getX(pointIndex), event.getY(pointIndex), pointIndex);
    			addPoint(cp);
    			break;
    		case MotionEvent.ACTION_CANCEL:
    		case MotionEvent.ACTION_OUTSIDE:
    		case MotionEvent.ACTION_UP:
    			removePoint(pointId);
    			break;
    		case MotionEvent.ACTION_MOVE:
    			for(int i=0;i<event.getPointerCount();i++){
    				int id=event.getPointerId(i);
    				Point p=getPoint(id);
    				if(null!=p){
    					p.pointx=event.getX(i);
    					p.pointy=event.getY(i);
    				}
    			}
    			break;
    	}
    	
    	return true;
    }
    public void addCircle(Cricle circle){
    	view.cs.add(circle);
    }
    public void addPoint(Point p){
    	view.ps.add(p);
    }
    public void removePoint(int id){
    	Iterator<Point> it= view.ps.iterator();
    	while(it.hasNext()){
    		Point p=it.next();
    		if(p.pointIndex==id){
    			it.remove();
    		}
    	}
    }
    public void removeCircle(int id){
    	Iterator<Cricle> it= view.cs.iterator();
    	while(it.hasNext()){
    		Cricle c=it.next();
    		if(c.pointIndex==id){
    			it.remove();
    		}
    	}
    }
    public Cricle getCircle(int id){
    	for(int i=0;i<view.cs.size();i++){
    		if(view.cs.get(i).pointIndex==id){
    			return view.cs.get(i);
    		}
    	}
    	return null;
    }
    public Point getPoint(int id){
    	for(int i=0;i<view.ps.size();i++){
    		if(view.ps.get(i).pointIndex==id){
    			return view.ps.get(i);
    		}   		
    	}
    	return null;
    }
}
           

看完是不是觉得很简单,希望能帮助到需要帮助的人!!!