手指點擊的地方隨機生成帶運動的不同顏色不同大小的圓

文章參考http://www.eoeandroid.com/thread-567599-1-1.html?_dsign=4765875a
我只是對他的代碼進行了效果改進,原效果是水底一直上升大小不一的氣泡,我這個效果是手點擊的地方生成帶運動的不同顏色不同大小的氣泡。
package com.android.systemui.statusbar.phone;


import java.util.ArrayList;
import java.util.List;
import java.util.Random;


import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;




public class BubbleLayout extends View implements OnClickListener{


	private List<Bubble> bubbles = new ArrayList<Bubble>();
	private Random random = new Random();// 生成隨機數
	private int width, height;
	Bubble bubble = new Bubble();
	private int count = 0;
	float x = 0, y = 0, x1 = 0, y1 = 0, x2 = 0, y2 = 0;
	public static boolean isLock = false;


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


	public BubbleLayout(Context context, AttributeSet attrs) {
		super(context, attrs);
	}


	public BubbleLayout(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
	}


	@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);
		width = getWidth();
		height = getHeight();


		Paint paint = new Paint();
		paint.reset();


		List<Bubble> list = new ArrayList<Bubble>(bubbles);
		// 依次繪製氣泡
		for (Bubble bubble : list) {
			// 碰到上邊界從數組中移除
			if (isLock) {
				bubbles.remove(bubble);
			}
			if (bubble.getY() - bubble.getSpeedY() <= 0) {
				bubbles.remove(bubble);
			}
			// 碰到左邊界從數組中移除
			else if (bubble.getX() - bubble.getRadius() <= 0) {
				bubbles.remove(bubble);
			}
			// 碰到右邊界從數組中移除
			else if (bubble.getX() + bubble.getRadius() >= width) {
				bubbles.remove(bubble);
			} else {
				int i = bubbles.indexOf(bubble);
				if (bubble.getX() + bubble.getSpeedX() <= bubble.getRadius()) {
					bubble.setX(bubble.getRadius());
				} else if (bubble.getX() + bubble.getSpeedX() >= width
						- bubble.getRadius()) {
					bubble.setX(width - bubble.getRadius());
				} else {
					bubble.setX(bubble.getX() + bubble.getSpeedX());
				}
				bubble.setY(bubble.getY() - bubble.getSpeedY());
				
				// 設置透明度 透明時移除,減少屏幕壓力
				if(bubble.getTransparence()>0){
				bubble.setTransparence(bubble.getTransparence()-2);
				}else{
					bubbles.remove(bubble);
				}
				paint.setColor(bubble.getColor());
				paint.setAlpha(bubble.getTransparence());// 設置不透明度:透明爲0,完全不透明爲255
				if (i > 0) {
					bubbles.set(i, bubble);


					canvas.drawCircle(bubble.getX(), bubble.getY(),
							bubble.getRadius(), paint);
				}
			}
		}
		invalidate();
	}


	private class Bubble {
		// 氣泡半徑
		private float radius;
		// 上升速度
		private float speedY;
		// 平移速度
		private float speedX;
		// 氣泡x座標
		private float x;
		// 氣泡y座標
		private float y;
		// 顏色
		private int color;
		// 透明度		
		private int transparence;
		
		public float getRadius() {
			return radius;
		}


		public void setRadius(float radius) {
			this.radius = radius;
		}
		
		public float getX() {
			return x;
		}
		
		public void setX(float x) {
			this.x = x;
		}
		
		public float getY() {
			return y;
		}


		public void setY(float y) {
			this.y = y;
		}


		public float getSpeedY() {
			return speedY;
		}


		public void setSpeedY(float speedY) {
			this.speedY = speedY;
		}


		public float getSpeedX() {
			return speedX;
		}


		public void setSpeedX(float speedX) {
			this.speedX = speedX;
		}


		public void setColor(int color) {
			this.color = color;
		}


		public int getColor() {
			return color;
		}


		public void setTransparence(int transparence) {
			this.transparence = transparence;
		}
		
		public int getTransparence() {
			return transparence;
		}


	}


	@Override
	public boolean onTouchEvent(MotionEvent event) {
		switch (event.getAction()) {
		case MotionEvent.ACTION_DOWN:
			
			count = 0;
			bubbles(event.getX(), event.getY());
			count = 0;
			x = event.getX();
			y = event.getY();
			break;
		case MotionEvent.ACTION_MOVE:
	
			x1 = event.getX();
			y1 = event.getY();
			count = 0;
			if (distance(x1, x2, y1, y2) > 15) {
				bubbles(event.getX(), event.getY());
				x2 = x1;
				y2 = y1;
			}
			if (distance(x, x1, y, y1) > 300 && !isLock) {
//				isLock = true;
//				bubbles.remove(bubble);
//				Intent intent = new Intent("com.bubbles.unlock");
//				getContext().sendBroadcast(intent);
			}
			count = 0;
			break;
		case MotionEvent.ACTION_UP:
			
			break;
		default:
			break;
		}
		return true;
	}


	void bubbles(final float x, final float y) {
		new Thread() {
			public void run() {
				while (count < 10) {
					Bubble bubble = new Bubble();
					int radius = random.nextInt(30);
					while (radius == 0) {
						radius = random.nextInt(30);
					}
					float speedY = random.nextFloat() * 5;
					while (speedY < 1) {
						speedY = random.nextFloat() * 5;
					}
					int color = random.nextInt(30);
					if (color < 5) {
						bubble.setColor(0X669999);
					} else if (color < 10) {
						bubble.setColor(0Xffffff);
					} else if (color < 15) {
						bubble.setColor(0XFFF8DC);
					} else if (color < 20) {
						bubble.setColor(0XF5F5DC);
					} else if (color < 25){
						bubble.setColor(0Xccccff);
					} else{
						bubble.setColor(0X808080);
					}
					bubble.setRadius(radius);
					bubble.setSpeedY(speedY * 2);
					bubble.setX(x);
					bubble.setY(y);
					bubble.setTransparence(100);
					
					float speedX = random.nextFloat() - 0.5f;
					while (speedX == 0) {
						speedX = random.nextFloat() - 0.5f;
					}
					bubble.setSpeedX(speedX * 4);
					bubbles.add(bubble);
					count++;
				}
			};
		}.start();
	}


	private int distance(float x1, float x2, float y1, float y2) {
		int distance = (int) Math.sqrt(Math.abs((x1 - x2) * (x1 - x2))
				+ Math.abs((y1 - y2) * (y1 - y2)));
		return distance;
	}


	@Override
	public void onClick(View arg0) {
		// TODO Auto-generated method stub
		
	}
}

下載地址:http://download.csdn.net/detail/cuckoochun/9522470 

因爲需要積分下載別的資源,所以要5積分下載,沒積分的同學可以留郵箱。


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章