Andrew V
Andrew V

Reputation: 522

Android onTouchEvent in a view class

I've tried debugging with messages and stuff but the only value i get from event.getAction() is 0 ... which i believe is ACTION_DOWN why dont i detect ACTION_MOVE or ACTION_UP??

package com.andrewxd.test01;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.view.MotionEvent;
import android.view.View;

public class Game extends View {

    private Bitmap enemy;
    private Enemy enemyN;
    private Paint paint;
    private float x,y;

    public Game(Context context) {
        super(context);
        enemy = BitmapFactory.decodeResource(getResources(), R.drawable.enemy);
        enemy = Bitmap.createScaledBitmap(enemy, 300 , 300, false);
        enemyN = new Enemy(10, 10, enemy);
        paint = new Paint();
        x = 0;
        y = 0;
    }

    @Override
    protected void onDraw(Canvas canvas){
        canvas.drawRect(x, y, 50, 50, paint);
    }


    @Override
    public boolean onTouchEvent(MotionEvent event){
        super.onTouchEvent(event);
        if(event.getAction() == MotionEvent.ACTION_DOWN){
            x = event.getX();
            y = event.getY();
            invalidate();
        } else if(event.getAction() == MotionEvent.ACTION_MOVE){
            x = event.getX();
            y = event.getY();
            invalidate();
        }
        return false;
    }
}

Am I doing something wrong? or is this supposed to work like this? And if this is supposed to be like this where do i detect touch movement

Upvotes: 0

Views: 320

Answers (1)

DeeV
DeeV

Reputation: 36035

It's because you're returning false at the end of the onTouchEvent. When you return false, you are saying that your View can not handle any more touch events so the events will no longer be sent to the View. If you want to receive MOVE and UP events, then you need to return true.

Upvotes: 2

Related Questions