pinguinson

Untitled

Sep 16th, 2014
235
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.29 KB | None | 0 0
  1. package ru.ifmo.md.lesson1;
  2.  
  3. import android.content.Context;
  4. import android.graphics.Canvas;
  5. import android.util.Log;
  6. import android.view.SurfaceHolder;
  7. import android.view.SurfaceView;
  8.  
  9. import java.util.Random;
  10.  
  11. /**
  12. * Created by thevery on 11/09/14.
  13. */
  14. class WhirlView extends SurfaceView implements Runnable {
  15. int [][] field = null;
  16. int [][] bufferField = null;
  17. int [] cellColor = null;
  18. int width = 240;
  19. int height = 320;
  20. float scaleWidth = 1;
  21. float scaleHeight = 1;
  22. final int MAX_COLOR = 10;
  23. int[] palette = {0xFFFF0000, 0xFF800000, 0xFF808000, 0xFF008000, 0xFF00FF00, 0xFF008080, 0xFF0000FF, 0xFF000080, 0xFF800080, 0xFFFFFFFF};
  24. SurfaceHolder holder;
  25. Thread thread = null;
  26. volatile boolean running = false;
  27.  
  28. public WhirlView(Context context) {
  29. super(context);
  30. holder = getHolder();
  31. }
  32.  
  33. public void resume() {
  34. running = true;
  35. thread = new Thread(this);
  36. thread.start();
  37. }
  38.  
  39. public void pause() {
  40. running = false;
  41. try {
  42. thread.join();
  43. } catch (InterruptedException ignore) {}
  44. }
  45.  
  46. public void run() {
  47. while (running) {
  48. if (holder.getSurface().isValid()) {
  49. long startTime = System.nanoTime();
  50. Canvas canvas = holder.lockCanvas();
  51. updateField();
  52. drawIt(canvas);
  53. holder.unlockCanvasAndPost(canvas);
  54. long finishTime = System.nanoTime();
  55. Log.i("TIME", "Circle: " + (1000.0) / ((float) (finishTime - startTime) / 1000000));
  56. try {
  57. Thread.sleep(16);
  58. } catch (InterruptedException ignore) {}
  59. }
  60. }
  61. }
  62.  
  63. public void onSizeChanged(int w, int h, int oldW, int oldH) {
  64. scaleWidth = (float) w / width;
  65. scaleHeight = (float) h / height;
  66. initField();
  67. }
  68.  
  69. void initField() {
  70. field = new int[width][height];
  71. bufferField = new int[width][height];
  72. cellColor = new int[width * height];
  73. Random rand = new Random();
  74. for (int x=0; x<width; x++) {
  75. for (int y=0; y<height; y++) {
  76. field[x][y] = rand.nextInt(MAX_COLOR);
  77. }
  78. }
  79. }
  80.  
  81. void updateField() {
  82. for (int x=0; x<width; x++) {
  83. for (int y=0; y<height; y++) {
  84. bufferField[x][y] = field[x][y];
  85. for (int dx=-1; dx<=1; dx++) {
  86. int x2 = (x + dx) % width;
  87. for (int dy=-1; dy<=1; dy++) {
  88. int y2 = (y + dy) & height;
  89. if ( (field[x][y]+1) % MAX_COLOR == field[x2][y2]) {
  90. bufferField[x][y] = field[x2][y2];
  91. }
  92. }
  93. }
  94. }
  95. }
  96. field = bufferField;
  97. }
  98.  
  99.  
  100.  
  101.  
  102. public void drawIt(Canvas canvas) {
  103. for (int x=0; x<width; x++) {
  104. for (int y = 0; y < height; y++) {
  105. cellColor[y * width + x] = palette[field[x][y]];
  106. }
  107. }
  108. canvas.scale(scaleWidth, scaleHeight);
  109. canvas.drawBitmap(cellColor, 0, width, 0, 0, width, height, false, null);
  110. }
  111. }
Advertisement
Add Comment
Please, Sign In to add comment