Hola quiero crear el efecto de lanzar un objeto, en este caso un circulo que esta dibujado en el canvas
. El problema es que no entiendo como identificar donde se toco en la pantalla y mover el circulo en esa dirección. Mi idea final seria algo como esa imagen.
Cualquier ayuda sirve. Gracias
Este es el codigo que tengo hasta ahora:
public class Juego extends SurfaceView implements View.OnTouchListener{
Paint paint;
int x, y, radio = 100, velocidadX = 5, velocidadY = 5, tocoY, tocoX;
boolean mover = false;
boolean una_vez = true;
public Juego(Context context, AttributeSet attrs) {
super(context, attrs);
this.setOnTouchListener(this);
setFocusable(true);
paint = new Paint();
}
public void onDraw(Canvas canvas){
paint.setColor(Color.WHITE);
canvas.drawRect(0,0,canvas.getWidth(),canvas.getHeight(),paint);
if (una_vez == true){
x = canvas.getWidth()/2;
y = canvas.getHeight()/2;
una_vez = false;
}
paint.setColor(Color.BLACK);
canvas.drawCircle(x, y, radio, paint);
if (mover == true){
if (x >= canvas.getWidth() - radio) {
velocidadX = -5;
}
if (x <= radio) {
velocidadX = 5;
}
if (y >= canvas.getHeight() - radio) {
velocidadY = -5;
}
if (y <= radio) {
velocidadY = 5;
}
x = x + velocidadX;
y = y + velocidadY;
}
invalidate();
}
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
switch (motionEvent.getAction()) {
case (MotionEvent.ACTION_DOWN):
tocoX = (int) motionEvent.getX();
tocoY = (int) motionEvent.getY();
mover = true;
return true;
default:
return super.onTouchEvent(motionEvent);
}
}
}