我的程序不会“重绘()”

时间:2015-04-19 03:03:49

标签: java android

我是从Java进来的,它赢了#34;重绘",我的意思是它不会将我的位图移动到有人点击屏幕的位置。我的代码没有任何问题,logcat没有告诉我什么,奇怪的是它昨天工作得很好,但现在由于某种原因它不会工作。

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;
    }
}

          }

据我所知,我的代码没有什么问题,也许有经验的人可能会看到一些东西,也许我的模拟器可能出现问题,昨天工作正常吗?

1 个答案:

答案 0 :(得分:0)

它看起来不像你设置的触控器。您有一个OnTouchListener课程和一个获取者,但您在视图上永远不会setOnTouchListener(listener)