• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Bitmap following finger is slow?

Hello,

I want a bitmap to follow my finger, this works but it's slow. The method gets called ~50 times per second.
This is my code:

Code:
@Override
    public boolean onTouchEvent(MotionEvent event) {
        int x = (int) event.getX();
        int y = (int) event.getY();
        Rect touchRect = new Rect(x - 1, y - 1, x + 1, y + 1);
        if (event.getAction() == MotionEvent.ACTION_DOWN) {
            if (startScreen) {
                if (Rect.intersects(sssb.getRect(), touchRect)) {
                    sssb.setPressed(true);
                }
                if (Rect.intersects(ssob.getRect(), touchRect)) {
                    ssob.setPressed(true);
                }
            }
            if (gameScreen) {
                for (BlackBomb blackBomb : bbs) {
                    if (Rect.intersects(blackBomb.getRectangle(), touchRect)) {
                        blackBomb.setPressed(true);
                        blackBomb.setX(x);
                        blackBomb.setY(y);
                        break;
                    }
                }
            }
            return true;
        }
        if (event.getAction() == MotionEvent.ACTION_UP) {
            if (startScreen) {
                if (Rect.intersects(sssb.getRect(), touchRect)) {
                    startScreen = false;
                    gameScreen = true;
                    countdownStart = System.nanoTime();
                    bbs.add(new BlackBomb(BitmapFactory.decodeResource(getResources(), R.drawable.bomb_black), 512, 100, 6));
                }
                if (Rect.intersects(ssob.getRect(), touchRect)) {
                    // Options button pressed
                }
                sssb.setPressed(false);
                ssob.setPressed(false);
            }
            if (gameScreen) {
                for (BlackBomb blackBomb : bbs) {
                    blackBomb.setPressed(false);
                }
            }
            return true;
        }
        if (event.getAction() == MotionEvent.ACTION_MOVE) {
            if (gameScreen) {
                System.out.println("Call");
                for (BlackBomb blackBomb : bbs) {
                    if (blackBomb.pressed) {
                        blackBomb.setX(x);
                        blackBomb.setY(y);
                    }
                }
            }
        }
        return super.onTouchEvent(event);
    }

What's wrong with this, and why does it move so slow? Thanks
 
Probably slow because your onTouchEvent method is doing too much work.
 
Actually it's not doing that much is it? Try taking out most of the code, and see how it affects the performance.
 
Actually it's not doing that much is it? Try taking out most of the code, and see how it affects the performance.

I doubt that checking one boolean and looping through one bomb is such a hard task, while it can fluently display items animated backgrounds and stuff like that. It's just that the bitmap following is kind of laggy.


It looks like the touch event isnt equal to the drawing speed (60 fps)
 
Back
Top Bottom