文章参考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积分下载,没积分的同学可以留邮箱。