0

I'm coming in from Java, it won't "Repaint", by that I mean it won't move my bitmap to where someone clicks on the screen. There's nothing wrong with my code, logcat doesn't tell me nothing, the weird thing is it worked fine yesterday but now for some reason it won't work.

Activity.class

package com.example.alex.something;


    import android.app.Activity;
    import android.os.Bundle;

    public class Animation extends Activity {

private AnimationThread a;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    a = new AnimationThread(this);
    setContentView(a);
}

@Override
protected void onPause() {
    super.onPause();
    a.pause();
}

@Override
protected void onResume() {
    super.onResume();
    a.resume();
}
  }

AnimationThread.class

package com.example.alex.something;

  import android.content.Context;
  import android.view.*;

  import android.graphics.*;

   public class AnimationThread extends SurfaceView implements Runnable{

private Thread thread = null;
private SurfaceHolder holder;
private Boolean runnable = false;
private Bitmap ball;
private float x,y;

public AnimationThread(Context context) {
    super(context);
    ball = BitmapFactory.decodeResource(getResources(),R.drawable.redball);
    x=0; y=0;
    holder = getHolder();
}

@Override
public void run() {
    while(runnable==true){
        if(!holder.getSurface().isValid()){
            continue;
        }
        Canvas c = holder.lockCanvas();
        c.drawBitmap(ball,x-ball.getWidth()/2,y-ball.getHeight()/2,null);
        holder.unlockCanvasAndPost(c);
    }

}

public void pause(){
    runnable=false;
    while(true){
        try{
            thread.join();
        }
        catch(InterruptedException e){
            e.getStackTrace();

        }

    }

}

public void resume(){
    runnable=true;
    thread = new Thread(this);
    thread.start();

}


public MotionListener getListener(){MotionListener a = new MotionListener(); return a;}


private class MotionListener implements OnTouchListener{

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        x= event.getX();
        y= event.getY();
        return false;
    }
}

          }

Again as far as I know there's nothing wrong with my code, maybe someone more experienced might be able to see something, also is it possible something maybe wrong with my emulator, it worked fine yesterday?

Alex Smith
  • 23
  • 2
  • @Tom It's started in `onResume()` in the activity. – alenz316 Apr 19 '15 at 03:11
  • @Tom Yea, I did that so so that it will be able to "repaint" it to a new location when someone clicks on the screen since I'm not really sure if android has a repaint method. – Alex Smith Apr 19 '15 at 03:12
  • @alenz316 Yes, noticed already. Really hidden. @AlexSmith Your `AnimationThread#pause` method looks really suspicous ... why is there an endless loop? – Tom Apr 19 '15 at 03:17
  • @Tom Not really sure why I put it there to be honest lol. – Alex Smith Apr 19 '15 at 03:35
  • @Tom you have any idea why it doesn't relocate tie bitmap instead of drawing a new one? – Alex Smith Apr 19 '15 at 04:22

1 Answers1

0

It doesn't look like your set the touch listener. You have an OnTouchListener class and a getter, but you never setOnTouchListener(listener) on a view.

alenz316
  • 613
  • 3
  • 10
  • Yes, it worked thank you, now do you have any idea why it just draws a new image instead of re-locating the existing one on my screen? – Alex Smith Apr 19 '15 at 04:19
  • When drawing to a canvas, it just draws whatever you tell it to. It has no idea that you want to "move" that bitmap. You can think of it as "stamping" the canvas with your image "stamp". To get the desired behavior, you need to clear the canvas. see http://stackoverflow.com/questions/6956838/how-to-erase-previous-drawing-on-canvas – alenz316 Apr 19 '15 at 05:28
  • Also, you should not be drawing unless something has changed. e.g. if your x or y has changed. ;) – alenz316 Apr 19 '15 at 05:29