Created
May 10, 2011 10:57
-
-
Save undetected1/964271 to your computer and use it in GitHub Desktop.
Multi-Threaded Canvas Draws
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
private ImageView imageView; | |
// ... | |
public void onDoingSomethingGood() { | |
new Thread(new Runnable() { | |
public void run() { | |
final Bitmap bitmap = loadImageFromOutside("http://mycloud.com/image.png"); | |
imageView.post(new Runnable() { | |
public void run() { | |
imageView.setImageBitmap(bitmap); | |
} | |
}); | |
} | |
}).start(); | |
} |
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
private ImageView imageView; | |
// ... | |
public void onDoingSomething() { | |
new Thread(new Runnable() { | |
public void run() { | |
final Bitmap b = loadImageFromOutside("http://mycloud.com/image.png"); | |
imageView.setImageBitmap(b); | |
} | |
}).start(); | |
} |
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 com.apleben.samples; | |
import android.content.Context; | |
import android.graphics.Canvas; | |
import android.view.SurfaceHolder; | |
import android.view.SurfaceView; | |
public class SurfaceViewCanvas extends SurfaceView implements SurfaceHolder.Callback { | |
private SurfaceHolder holder; | |
private SurfaceViewThread surfaceViewThread; | |
private boolean hasSurface; | |
SurfaceViewCanvas(Context context) { | |
super(context); | |
init(); | |
} | |
// Implementing all callback methods below | |
public void resume() { | |
// Create and start the graphics update thread | |
if (surfaceViewThread == null) { | |
surfaceViewThread = new SurfaceViewThread(); | |
if (hasSurface) | |
surfaceViewThread.start(); | |
} | |
} | |
public void pause() { | |
// Stop the graphics update thread | |
if (surfaceViewThread != null) { | |
surfaceViewThread.requestExitAndWait(); | |
surfaceViewThread = null; | |
} | |
} | |
public void surfaceCreated(SurfaceHolder holder) { | |
hasSurface = true; | |
if (surfaceViewThread != null) | |
surfaceViewThread.start(); | |
} | |
public void surfaceDestroyed(SurfaceHolder holder) { | |
hasSurface = false; | |
pause(); | |
} | |
public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) { | |
if (surfaceViewThread != null) | |
surfaceViewThread.onWindowResize(w, h); | |
} | |
private void init() { | |
// Create a new SurfaceHolder and assign this class as its callback | |
holder = getHolder(); | |
holder.addCallback(this); | |
hasSurface = false; | |
} | |
private final class SurfaceViewThread extends Thread { | |
private boolean done; | |
SurfaceViewThread() { | |
super(); | |
done = false; | |
} | |
@Override | |
public void run() { | |
SurfaceHolder surfaceHolder = holder; | |
// Repeat the drawing loop until the thread is stopped | |
while (!done) { | |
// Lock the surface and return the canvas to draw onto | |
Canvas canvas = surfaceHolder.lockCanvas(); | |
// TODO: Perform some Draws on the Canvas. Whatever you want! | |
// Unlock the canvas and render the result | |
surfaceHolder.unlockCanvasAndPost(canvas); | |
} | |
} | |
public void requestExitAndWait() { | |
// Mark this thread as complete and wait it to finish himself | |
done = true; | |
try { | |
join(); | |
} catch (InterruptedException ignored) { | |
} | |
} | |
public void onWindowResize(int w, int h) { | |
// Deal with a change in the new surface size | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment