打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
水下气泡上升界面效果
首先声明,该效果并不是用动画实现的,而是用自定义View实现的,在onDraw方法里面调用invalidate()函数,实现气泡上升效果
    气泡类:
  1. private class Bubble {
  2.                 /** 气泡半径 */
  3.                 private int radius;
  4.                 /** 上升速度 */
  5.                 private float speedY;
  6.                 /** 平移速度 */
  7.                 private float speedX;
  8.                 /** 气泡x坐标 */
  9.                 private float x;
  10.                 /** 气泡y坐标 */
  11.                 private float y;
  12. }
复制代码
    另起线程生成气泡:
  1. if (!starting) {
  2.                         starting = true;
  3.                         new Thread() {
  4.                                 public void run() {
  5.                                         while (true) {
  6.                                                 try {
  7.                                                         Thread.sleep(random.nextInt(3) * 300);
  8.                                                 } catch (InterruptedException e) {
  9.                                                         // TODO Auto-generated catch block
  10.                                                         e.printStackTrace();
  11.                                                 }
  12.                                                 Bubble bubble = new Bubble();
  13.                                                 int radius = random.nextInt(30);
  14.                                                 while (radius == 0) {
  15.                                                         radius = random.nextInt(30);
  16.                                                 }
  17.                                                 float speedY = random.nextFloat()*5;
  18.                                                 while (speedY < 1) {
  19.                                                         speedY = random.nextFloat()*5;
  20.                                                 }
  21.                                                 bubble.setRadius(radius);
  22.                                                 bubble.setSpeedY(speedY);
  23.                                                 bubble.setX(width / 2);
  24.                                                 bubble.setY(height);
  25.                                                 float speedX = random.nextFloat()-0.5f;
  26.                                                 while (speedX == 0) {
  27.                                                         speedX = random.nextFloat()-0.5f;
  28.                                                 }
  29.                                                 bubble.setSpeedX(speedX*2);
  30.                                                 bubbles.add(bubble);
  31.                                         }
  32.                                 };
  33.                         }.start();
  34.                 }
复制代码
   绘制渐变背景和气泡:
  1. Paint paint = new Paint();
  2.                 // 绘制渐变正方形
  3.                 Shader shader = new LinearGradient(0, 0, 0, height, new int[] {
  4.                                 getResources().getColor(R.color.blue_bright),
  5.                                 getResources().getColor(R.color.blue_light),
  6.                                 getResources().getColor(R.color.blue_dark) },
  7.                                 null, Shader.TileMode.REPEAT);
  8.                 paint.setShader(shader);
  9.                 canvas.drawRect(0, 0, width, height, paint);
  10.                 paint.reset();
  11.                 paint.setColor(Color.WHITE);
  12.                 paint.setAlpha(200);
  13.                 List<Bubble> list = new ArrayList<Bubble>(bubbles);
  14.                 for (Bubble bubble : list) {
  15.                         if (bubble.getY() - bubble.getSpeedY() <= 0) {
  16.                                 bubbles.remove(bubble);
  17.                         } else {
  18.                                 int i = bubbles.indexOf(bubble);
  19.                                 if (bubble.getX() + bubble.getSpeedX() <= bubble.getRadius()) {
  20.                                         bubble.setX(bubble.getRadius());
  21.                                 } else if (bubble.getX() + bubble.getSpeedX() >= width
  22.                                                 - bubble.getRadius()) {
  23.                                         bubble.setX(width - bubble.getRadius());
  24.                                 } else {
  25.                                         bubble.setX(bubble.getX() + bubble.getSpeedX());
  26.                                 }
  27.                                 bubble.setY(bubble.getY() - bubble.getSpeedY());
  28.                                 bubbles.set(i, bubble);
  29.                                 canvas.drawCircle(bubble.getX(), bubble.getY(),
  30.                                                 bubble.getRadius(), paint);
  31.                         }
  32.                 }
复制代码


解决了36楼问题:楼主 ,跑了下DEMO,  屏幕关闭后线程还在跑,但是CANVAS不刷新了,重新亮屏后所有的气泡挤在一起,时间一长会OOM吧
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
JavaScript小游戏实例:简单的键盘练习
[canvas]通过动态生成像素点做绚丽效果
JavaScript动画实例:炸开的小球
Ae 效果:CC Bubbles
泡泡内用餐更安全?
Bubble Rooms(气泡酒店)
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服