Advertisement
Guest User

Untitled

a guest
Dec 17th, 2012
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.91 KB | None | 0 0
  1. public class SnowService extends WallpaperService {
  2.    
  3.         private final static int[] windCurve = {1,3,5,6,6,6,7,7,6,5,4,2,1,1};
  4.    
  5.         public Engine onCreateEngine() {
  6.             return new SnowEngine();
  7.         }
  8.    
  9.         public class SnowEngine extends WallpaperService.Engine {
  10.    
  11.             private SnowThread thread;
  12.             private Random random = new Random();
  13.             private SurfaceHolder holder;
  14.    
  15.             private Bitmap flakeImage;
  16.             private Set<Snowflake> flakes = new HashSet<Snowflake>();
  17.             private Set<Snowflake> oobFlakes = new HashSet<Snowflake>();
  18.             private int wind = 0;
  19.    
  20.             public SnowEngine() {
  21.                 flakeImage = BitmapFactory.decodeResource(getResources(), R.drawable.square3x3);
  22.             }
  23.    
  24.             public class SnowThread extends Thread {
  25.    
  26.                 private boolean isSnowing = true;
  27.                 private long lastWindUpdate = System.currentTimeMillis();
  28.                 private int windCurveIndex = 0;
  29.    
  30.                 private void update(Canvas c) {
  31.    
  32.                     c.drawRGB(0, 0, 0);
  33.                     flakes.add(new Snowflake(random.nextFloat() * c.getWidth(), -flakeImage.getWidth(), flakeImage));
  34.                     long nowTime = System.currentTimeMillis();
  35.    
  36.                     if (wind == 0) {
  37.                        if (nowTime - lastWindUpdate > 20000) {
  38.                           wind = windCurve[++windCurveIndex];
  39.                           lastWindUpdate = nowTime;
  40.                        }
  41.                     } else if (nowTime - lastWindUpdate > 1000) {
  42.                         if (windCurveIndex < windCurve.length -1) {
  43.                             wind = windCurve[++windCurveIndex];
  44.                             lastWindUpdate = nowTime;
  45.                         } else {
  46.                             windCurveIndex = 0;
  47.                             wind = 0;
  48.                             lastWindUpdate = nowTime;
  49.                         }
  50.                     }
  51.    
  52.    
  53.                     for (Snowflake flake : flakes) {
  54.                         flake.update(wind);
  55.                         c.drawBitmap(flake.bitmap, flake.x, flake.y, null);
  56.    
  57.                         if (flake.x > c.getWidth() || flake.x < -flake.bitmap.getWidth() ||
  58.                                 flake.y > c.getHeight()) {
  59.                             oobFlakes.add(flake);
  60.                         }
  61.    
  62.                     }
  63.    
  64.                     flakes.removeAll(oobFlakes);
  65.                     oobFlakes.clear();
  66.    
  67.                 }
  68.    
  69.                 @Override
  70.                 public void run() {
  71.                     Log.d("snow", "SnowThread started");
  72.                     while (isSnowing) {
  73.                         Canvas canvas = null;
  74.                         try {
  75.                             Thread.sleep(30);
  76.                             canvas = holder.lockCanvas();
  77.                             update(canvas);
  78.    
  79.                         } catch (InterruptedException e) {
  80.    
  81.                         } finally {
  82.                             if (canvas != null) {
  83.                                 holder.unlockCanvasAndPost(canvas);
  84.                             }
  85.                         }
  86.                     }
  87.                 }
  88.    
  89.                 public boolean isSnowing() {
  90.                     return isSnowing;
  91.                 }
  92.    
  93.                 public void stopSnowing() {
  94.                     isSnowing = false;
  95.                 }
  96.             }
  97.    
  98.             @Override
  99.             public void onVisibilityChanged(boolean visible) {
  100.                 super.onVisibilityChanged(visible);
  101.                 Log.d("snow", "visibilityChanged to " + visible);
  102.                 if (visible) {
  103.                     thread = new SnowThread();
  104.                     thread.start();
  105.                 } else {
  106.                     thread.stopSnowing();
  107.                 }
  108.             }
  109.    
  110.             @Override
  111.             public void onSurfaceCreated(SurfaceHolder holder) {
  112.                 Log.d("snow", "surfaceCreated");
  113.                 this.holder = holder;
  114.             }
  115.    
  116.             @Override
  117.             public void onSurfaceDestroyed(SurfaceHolder holder) {
  118.                 // usual case is onVisibilityChanged(false) has been called
  119.                 // and  the thread is shutting down
  120.                 Log.d("snow", "surfaceDestroyed");
  121.                 boolean retry = true;
  122.                 while (retry) {
  123.                     try {
  124.                         thread.stopSnowing(); // just in case
  125.                         thread.join();
  126.                         Log.d("snow", "snowThread died");
  127.                         retry = false;
  128.                     } catch (InterruptedException e) {
  129.                     }
  130.                 }
  131.             }
  132.    
  133.    
  134.             class Snowflake {
  135.    
  136.                 public Bitmap bitmap;
  137.                 float x;
  138.                 float y;
  139.                 int bias = 0;
  140.                 int biasTime = 0;
  141.                 boolean interBias = false;
  142.    
  143.                 Snowflake(float x, float y, Bitmap bitmap) {
  144.                     this.x = x;
  145.                     this.y = y;
  146.                     this.bitmap = bitmap;
  147.                 }
  148.    
  149.                 void update(int wind) {
  150.    
  151.                     if (biasTime < 0) {
  152.                         if (interBias) {
  153.                             bias = ((Integer) random.nextInt(3)).compareTo(1);
  154.                             interBias = false;
  155.                         } else {
  156.                             bias = 0;
  157.                             interBias = true;
  158.                         }
  159.    
  160.                         biasTime = random.nextInt(30);
  161.                     }
  162.    
  163.                     biasTime--;
  164.                     x += bias + wind;
  165.                     y += 4;
  166.                 }
  167.             }
  168.         }
  169.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement