nav_jan nav_jan - 7 months ago 57
Java Question

onDraw() or dispatchDraw() not getting called on touch

Code:

public class MainActivity extends AppCompatActivity implements GestureDetector.OnGestureListener,
GestureDetector.OnDoubleTapListener {

DrawView drawView;
private GestureDetectorCompat g1;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
drawView = new DrawView(this);
drawView.setBackgroundColor(Color.TRANSPARENT);
setContentView(drawView);
g1 = new GestureDetectorCompat(this,this);
g1.setOnDoubleTapListener(this);
}

@Override
public boolean onTouchEvent(MotionEvent event) {
g1.onTouchEvent(event);
return super.onTouchEvent(event);
}

@Override
public boolean onSingleTapConfirmed(MotionEvent e) {
DrawView.x_touch = e.getX();
DrawView.y_touch = e.getY();
Log.v("id2","message2");
return false;
}


Code for drawing:

public class DrawView extends View {
Paint paint = new Paint();
static float x_touch = -1;
static float y_touch = -1;
public DrawView(Context context) {
super(context);
super.setWillNotDraw(false);
paint.setColor(Color.BLACK);
}
@Override
public void onDraw(Canvas canvas) {
super.onDraw(canvas);
Display mdisp = ((WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
Point mdispSize = new Point();
mdisp.getSize(mdispSize);
int maxX = mdispSize.x;
int maxY = mdispSize.y;
canvas.drawLine(maxX/3, 0, maxX/3, maxY, paint);
canvas.drawLine(maxX*2/3, 0, maxX*2/3, maxY, paint);
canvas.drawLine(0, maxY/3, maxX, maxY/3, paint);
canvas.drawLine(0, maxY*2/3, maxX, maxY*2/3, paint);
Log.v("id1","message1");
if (x_touch>0 && y_touch > 0)
canvas.drawText("X",x_touch,y_touch,paint);
}
}


On doing some research I found that one need to call
setWillNotDraw(false)
while overriding
onDraw()
or use
dispatchDraw()
.
I have tried both but Draw function is called only once.

I am new to android development.

Answer

Change your code as follow :

public class DrawView extends View {
Paint paint = new Paint();
float x_touch = -1;
float y_touch = -1;
public DrawView(Context context) {
    super(context);
    super.setWillNotDraw(false);
    paint.setColor(Color.BLACK);
}
@Override
public void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    Display mdisp = ((WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
    Point mdispSize = new Point();
    mdisp.getSize(mdispSize);
    int maxX = mdispSize.x;
    int maxY = mdispSize.y;
    canvas.drawLine(maxX/3, 0, maxX/3, maxY, paint);
    canvas.drawLine(maxX*2/3, 0, maxX*2/3, maxY, paint);
    canvas.drawLine(0, maxY/3, maxX, maxY/3, paint);
    canvas.drawLine(0, maxY*2/3, maxX, maxY*2/3, paint);
    Log.v("id1","message1");
    if (x_touch>0 && y_touch > 0)
        canvas.drawText("X",x_touch,y_touch,paint);
}

public void setXandY(float x, float y) {
  x_touch = x;
  y_touch = y;
  this.invalidate();
}

}

And :

@Override
public boolean onSingleTapConfirmed(MotionEvent e) {
    drawView.setXandY(e.getX(), e.getY());
    return (true);
}