Reputation: 37633
I have a surfaceview which draws a joystick view. That view is a custom view which has a ontouchevent and ontouch event is not being called. Why? This is the code...
the surface view:
public class GameView extends SurfaceView implements Runnable {
public void draw() {
if (ourHolder.getSurface().isValid()) {
canvas = ourHolder.lockCanvas();
//some code
joystickOnScreen.layout(10, (int)(sh*0.8f), (int)(sh*0.2f), (int)(sh*1f));
canvas.save();
joystickOnScreen.draw(canvas);
canvas.restore();
//some code
ourHolder.unlockCanvasAndPost(canvas);
}
}
}
the joystick view:
public class Joystick extends View {
public Joystick(Context context) {
super(context);
setFocusable(true);
setClickable(true);
}
@Override
public boolean onTouchEvent(MotionEvent ev) {
final int action = ev.getAction();
//some code
}
}
Upvotes: 0
Views: 170
Reputation: 1776
SurfaceViews are not ViewGroups, so, it doesn't behave like normal LinearLayout/RelativeLayout that broadcast their events to their children. Instead, you draw it manually overriding onDraw, or calling a draw method from some outside loop, correct? It's not possible to infer how you are rendering your surface view just by the code you shared, but it seems that you call this draw() method from some kind of controller activity/fragment right?
What you need to do inside the SurfaceView is to do what a ViewGroup usually do for you: find all children elements - by children I mean the views that you render inside your SurfaceView area - and call their onTouchEvent. In your case, it seems that you need just to call Joystick touchEvent.
Here is some example code:
public class GameView extends SurfaceView {
//initialization and drawing stuff
@Override
public boolean onTouchEvent(MotionEvent ev) {
return joystickOnScreen.onTouchEvent(ev);
}
}
Upvotes: 0