Created
September 15, 2014 12:57
-
-
Save heat-wave/2a0fc565d13d6fb496d9 to your computer and use it in GitHub Desktop.
Working bitmap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package ru.ifmo.md.lesson1; | |
import android.content.Context; | |
import android.graphics.Canvas; | |
import android.util.Log; | |
import android.view.SurfaceHolder; | |
import android.view.SurfaceView; | |
import android.graphics.Bitmap; | |
import java.util.Random; | |
/** | |
* Created by thevery on 11/09/14. | |
* Modified by heat-wave on 15/09/14 | |
*/ | |
class WhirlView extends SurfaceView implements Runnable { | |
int [][] field = null; | |
int width = 0; | |
int height = 0; | |
int scale = 4; | |
final int MAX_COLOR = 10; | |
int[] palette = {0xFFFF0000, 0xFF800000, 0xFF808000, 0xFF008000, 0xFF00FF00, 0xFF008080, 0xFF0000FF, 0xFF000080, 0xFF800080, 0xFFFFFFFF}; | |
Bitmap screen; | |
SurfaceHolder holder; | |
Thread thread = null; | |
volatile boolean running = false; | |
public WhirlView(Context context) { | |
super(context); | |
holder = getHolder(); | |
} | |
public void resume() { | |
running = true; | |
thread = new Thread(this); | |
thread.start(); | |
} | |
public void pause() { | |
running = false; | |
try { | |
thread.join(); | |
} catch (InterruptedException ignore) {} | |
} | |
public void run() { | |
while (running) { | |
if (holder.getSurface().isValid()) { | |
long startTime = System.nanoTime(); | |
Canvas canvas = holder.lockCanvas(); | |
updateField(); | |
onDraw(canvas); | |
holder.unlockCanvasAndPost(canvas); | |
long finishTime = System.nanoTime(); | |
Log.i("TIME", "FPS: " + (1000000000.0 / (finishTime - startTime))); | |
try { | |
Thread.sleep(16); | |
} catch (InterruptedException ignore) {} | |
} | |
} | |
} | |
@Override | |
public void onSizeChanged(int w, int h, int oldW, int oldH) { | |
width = w/scale; | |
height = h/scale; | |
screen = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565); | |
field = new int[width][height]; | |
initField(); | |
} | |
void initField() { | |
field = new int[width][height]; | |
Random rand = new Random(); | |
for (int x=0; x<width; x++) { | |
for (int y=0; y<height; y++) { | |
field[x][y] = rand.nextInt(MAX_COLOR); | |
screen.setPixel(x, y, palette[field[x][y]]); | |
} | |
} | |
} | |
void updateField() { | |
int[][] field2 = new int[width][height]; | |
for (int x=0; x<width; x++) { | |
for (int y=0; y<height; y++) { | |
field2[x][y] = field[x][y]; | |
for (int dx=-1; dx<=1; dx++) { | |
for (int dy=-1; dy<=1; dy++) { | |
int x2 = x + dx; | |
int y2 = y + dy; | |
if (x2<0) x2 += width; | |
if (y2<0) y2 += height; | |
if (x2>=width) x2 -= width; | |
if (y2>=height) y2 -= height; | |
if ( (field[x][y]+1) % MAX_COLOR == field[x2][y2]) { | |
field2[x][y] = field[x2][y2]; | |
screen.setPixel(x, y, palette[field[x][y]]); | |
} | |
} | |
} | |
} | |
} | |
field = field2; | |
} | |
@Override | |
public void onDraw(Canvas canvas) { | |
canvas.drawBitmap(Bitmap.createScaledBitmap(screen, canvas.getWidth(), canvas.getHeight(), true), 0, 0, null); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment