Android: Refresh bitmap with new data
I am trying to create a 15fps "bitmap video" of random pixel colors. The following code displays one bitmap correctly, however does not update/refresh the image with a new random one. Can anyone please help me figure out why?
public class ViewThread extends Thread {
private SurfaceHolder mHolder;
private Panel mPanel;
private boolean mRun = false;
public View开发者_运维技巧Thread( Panel panel) {
mPanel = panel;
mHolder = mPanel.getHolder();
}
public void setRunning(boolean run) {
mRun = run;
}
@Override
public void run() {
Canvas canvas = null;
while (mRun) {
canvas = mHolder.lockCanvas();
if (canvas != null) {
mPanel.doDraw(canvas);
mHolder.unlockCanvasAndPost(canvas);
}
}
}
}
public class Panel extends SurfaceView implements SurfaceHolder.Callback {
private ViewThread mThread;
private Bitmap mBitmap;
private int[] mcolors;
public Panel(Context context) {
super(context);
init();
}
private void init() {
getHolder().addCallback(this);
mThread = new ViewThread(this);
// create a graphic
mcolors = colorMap();
int[] colors = mcolors;
mBitmap = Bitmap.createBitmap(colors, 64, 64, Bitmap.Config.ARGB_8888);
mBitmap = Bitmap.createScaledBitmap(mBitmap, 256, 256, false);
}
protected void doDraw(Canvas canvas) {
canvas.drawColor(Color.BLACK);
canvas.drawBitmap(mBitmap, 8, 8, null);
}
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
}
//@Override
public void surfaceCreated(SurfaceHolder holder) {
if (!mThread.isAlive()) {
mThread = new ViewThread(this);
mThread.setRunning(true);
mThread.start();
}
}
public void surfaceDestroyed(SurfaceHolder holder) {
if (mThread.isAlive()) {
mThread.setRunning(false);
}
}
}
private static int[] colorMap() {
int[] ltable = { 0xff000000, 0xff00000f, 0xff00001e, 0xff00002d, 0xff00003c, 0xff00004b,
0xff00005a, 0xff000069, 0xff000078, 0xff000087, 0xff000096, 0xff0000a5,
0xff0000b4, 0xff0000c3, 0xff0000d2, 0xff0000e1, 0xff0000f0, 0xff0000ff,
0xff0f00ff, 0xff1e00ff, 0xff2d00ff, 0xff3c00ff, 0xff4b00ff, 0xff5a00ff,
0xff6900ff, 0xff7800ff, 0xff8700ff, 0xff9600ff, 0xffa500ff, 0xffb400ff,
0xffc300ff, 0xffd200ff, 0xffe100ff, 0xfff000ff, 0xffff00ff, 0xffff00f0,
0xffff00e1, 0xffff00d2, 0xffff00c3, 0xffff00b4, 0xffff00a5, 0xffff0096,
0xffff0087, 0xffff0078, 0xffff0069, 0xffff005a, 0xffff004b, 0xffff003c,
0xffff002d, 0xffff001e, 0xffff000f, 0xffff0000 };
int[] mcolors = new int[4096];
Random rand = new Random();
for(int i=0; i < 4096; i++)
{
int num = rand.nextInt(52);
mcolors[i] = ltable[num];
}
return mcolors;
}
}
It appears to me that you are calling your thread only once when surfaceCreated()
is called. After that, your thread is never called again. I believe you need something like timer and reinitiate thread or call thread run() method to display another image
.
[EDIT]
Few other things
1) Recreate your canvas holder in every while loop.
while (run) {
c = null;
try {
c = panel.getHolder().lockCanvas();
2) Also, after android 1.5 a surfaceview can't be refresh more 40-45 time per second so try reducing fps to 30 and see if it makes any difference.
Example: http://www.anddev.org/android-2d-3d-graphics-opengl-problems-f55/surfaceview-portage-1-5-other-version-of-android-t15675.html
精彩评论