Hello,

I have this View that draws random dots on the screen using
drawCircle.

It prints the first dot, then the first dot goes away and it prints
the second dot. I expected both dots to be there, but what ever.

Weird thing is, then the second dot goes away, and the first dot come
back along with a third dot. Now they go away and the second comes
back with a fourth, and so on.

So it looks like the program is alternating between two different
canvases, or views, (not sure what the right terminology is).

Any idea why?

Here's the class:


package mike.test;

import android.content.Context;

public class TestView extends SurfaceView implements
SurfaceHolder.Callback {

public void onDraw(Canvas canvas){
canvas.drawCircle((float) Math.random() * canvas.getWidth(),
(float) Math.random() * canvas.getHeight(), (float) Math.random() *
20, mPaint);
}


//change the color of the dot if the user clicks
private OnClickListener mClickListener = new OnClickListener() {
public void onClick(View v) {
TestView tv = (TestView) v;
tv.currentColor++;
if (tv.currentColor > tv.colors.length - 1) tv.currentColor = 0;
Log.d("test", String.valueOf(tv.colors.length) +
String.valueOf(tv.colors.length) );
tv.mPaint.setColor(tv.colors[tv.currentColor]);
}
};


private TestThread thread;
public int currentColor = 0;
public Paint mPaint = new Paint();
int[] colors = {Color.BLUE, Color.RED, Color.YELLOW, Color.GREEN};
public TestView(Context context, AttributeSet attrs) {
super(context, attrs);
getHolder().addCallback(this);
thread = new TestThread(getHolder(), this);
setFocusable(true);
setOnClickListener(mClickListener);

}



@Override
public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int
arg3) {

}

@Override
public void surfaceCreated(SurfaceHolder holder) {
thread.setRunning(true);
thread.start();

}

@Override
public void surfaceDestroyed(SurfaceHolder holder) {
boolean retry = true;
thread.setRunning(false);
while (retry) {
try {
thread.join();
retry = false;
} catch (InterruptedException e) {
// we will try it again and again...
}
}
}

class TestThread extends Thread {
private SurfaceHolder mHolder;
private TestView mTestView;
private boolean _run = false;
public TestThread(SurfaceHolder holder, TestView panel){
mHolder = holder;
mTestView = panel;
}

public void setRunning(boolean run) {
_run = run;
}
private long lastTime = 0;

@Override
public void run() {
Canvas c;
while (_run) {
//if (lastTime == 0) lastTime = System.currentTimeMillis();
if (System.currentTimeMillis() > lastTime + 2000) {
lastTime = System.currentTimeMillis();

c = null;
try {
c = mHolder.lockCanvas(null);
synchronized (mHolder) {
mTestView.onDraw(c);
}
} finally {
// do this in a finally so that if an exception is thrown
// during the above, we don't leave the Surface in an
// inconsistent state
if (c != null) {
mHolder.unlockCanvasAndPost(c);
}
}
}
}
}


}

}

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to