Reputation: 5236
I have a problem with simple drawing application on Android.
Here's my class which is extending SurfaceView:
public class SomeView extends SurfaceView implements SurfaceHolder.Callback {
private static SurfaceHolder surfaceHolder;
static Canvas canvas=null;
static Paint paint=new Paint();
float X,Y,X1,Y1;
static int mode=1;
public static void ClearAll(){
canvas=surfaceHolder.lockCanvas(null);
canvas.drawColor(Color.BLACK);
surfaceHolder.unlockCanvasAndPost(canvas);
}
public SomeView(Context context,AttributeSet attrs) {
super(context);
getHolder().addCallback(this);
paint.setColor(Color.WHITE);
paint.setStyle(Style.FILL);
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
this.surfaceHolder=holder;
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
}
public boolean onTouchEvent(MotionEvent event)
{
canvas=surfaceHolder.lockCanvas(null);
if (mode==1){
canvas.drawCircle(event.getX(), event.getY(),10, paint);
}
if (mode==2){
paint.setStrokeWidth(10);
if (event.getAction()==MotionEvent.ACTION_DOWN){
X=event.getX();
Y=event.getY();
}
if (event.getAction()==MotionEvent.ACTION_UP){
X1=event.getX();
Y1=event.getY();
canvas.drawLine(X, Y, X1, Y1, paint);
}
}
surfaceHolder.unlockCanvasAndPost(canvas);
return true;
}
}
mode=1 - is simple user's touches tracing,
mode=2 - is straight-lines drawing
However, when I'm drawing in mode=2 the picture becomes weird:
Some lines disappear and after few more lines were drawn appears again. When I'm still touching the screen the canvas is blinking and showing all the lines were drawn since last ClearAll()
call. If I stop touching only few lines are still visible.
What is the problem?
Upvotes: 0
Views: 2334
Reputation: 348
SurfaceView used double buffering. Generally - on each cycle/action you have to draw every pixel you want to be visible on the canvas. Hope that helps.
Upvotes: 1