I'm trying to create a specific paint program using a transparent surefaceView, and a brush that was made from a custom bitmap. I looked on this website for answers but couldn't find what I need.
Because of the while loop of the run method, the bitmaps that I'm drawing on the screen are flickering non-stop. It appears as the program is drawing the same bitmaps infinitely which is causing this flicker.
I'm thinking about saving every brush stroke until event.getAction()==MotionEvent.ACTION_UP
as a separate bitmap. But I'm having trouble doing this.
This is my Code, I'd appreciate any help with fixing this flickering problem!
public class SurfaceMyPaint extends SurfaceView implements Runnable {
Thread t;
SurfaceHolder holder;
Bitmap brush;
boolean isItOk = false;
public SurfaceMyPaint(Context context)
{
super(context);
holder = getHolder();
initial();
}
public SurfaceMyPaint(Context context, AttributeSet attrs, int defStyle)
{
super(context, attrs, defStyle);
holder = getHolder();
initial();
// TODO Auto-generated constructor stub
}
public SurfaceMyPaint(Context context, AttributeSet attrs)
{
super(context, attrs);
holder = getHolder();
initial();
// TODO Auto-generated constructor stub
}
public void initial()
{
brush= BitmapFactory.decodeResource(getResources(), R.drawable.brush2);
}
public boolean onTouchEvent(MotionEvent event)
{
if(event.getAction()== MotionEvent.ACTION_DOWN)
{
x= event.getX();
y= event.getY();
}
if(event.getAction()== MotionEvent.ACTION_MOVE)
{
x = event.getX();
y= event.getY();
Canvas c= holder.lockCanvas();
c.drawBitmap(brush,x- (brush.getWidth() / 2),y- (brush.getWidth() / 2),null);
holder.unlockCanvasAndPost(c);
}
return true;
}
public void run()
{
while (isItOk == true)
{
if (!holder.getSurface().isValid())
continue;
}
}
public void pause(){
isItOk = false;
while (true){
try {
t.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
break;
}
t=null;
}
public void resume(){
isItOk = true;
t = new Thread(this);
t.start();
}
}