我正在尝试创建随机像素颜色的15fps“位图视频”。以下代码正确显示一个位图,但不会使用新的随机图更新/刷新图像。任何人都可以帮我找出原因吗?
public class ViewThread extends Thread {
private SurfaceHolder mHolder;
private Panel mPanel;
private boolean mRun = false;
public ViewThread( 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;
}
}
答案 0 :(得分:0)
在我看来,你正在调用你的线程only once when surfaceCreated()
。之后,永远不再调用您的线程。我相信你需要像timer and reinitiate thread or call thread run() method to display another image
这样的东西。
<强> [编辑] 强>
很少有其他事情
1)在每个while循环中重新创建画布支架。
while (run) {
c = null;
try {
c = panel.getHolder().lockCanvas();
2)此外,在android 1.5之后,一个surfaceview无法每秒刷新40-45次,所以尝试将fps减少到30,看看它是否有任何区别。