为什么';我的drawBitmap函数在第一次执行后工作(Android、Java)?
MainThread.java为什么';我的drawBitmap函数在第一次执行后工作(Android、Java)?,java,android,Java,Android,MainThread.java package com.example.myapplication; import android.graphics.Canvas; import android.view.SurfaceHolder; public class MainThread extends Thread { private SurfaceHolder surfaceHolder; private GameView gameView; private boole
package com.example.myapplication;
import android.graphics.Canvas;
import android.view.SurfaceHolder;
public class MainThread extends Thread {
private SurfaceHolder surfaceHolder;
private GameView gameView;
private boolean running;
public static Canvas canvas;
private int targetFPS = 30;
private double averageFPS;
public MainThread(SurfaceHolder surfaceHolder, GameView gameView){
super();
this.surfaceHolder=surfaceHolder;
this.gameView=gameView;
}
@Override
public void run(){
long startTime;
long timeMillis;
long waitTime;
long totalTime = 0;
int frameCount =0;
long targetTime = 1000/targetFPS;
while(running){
startTime = System.nanoTime();
canvas = null;
try{
canvas=this.surfaceHolder.lockCanvas();
synchronized (surfaceHolder){
this.gameView.update();
this.gameView.draw(canvas);
}
} catch (Exception e){
e.printStackTrace();
} finally {
if (canvas!=null){
try{
surfaceHolder.unlockCanvasAndPost(canvas);
} catch(Exception e){ e.printStackTrace(); }
}
}
timeMillis = (System.nanoTime() - startTime) / 1000000;
waitTime = targetTime-timeMillis;
try{
this.sleep(waitTime);
}catch(Exception e){ e.printStackTrace(); }
totalTime += System.nanoTime()-startTime;
frameCount++;
if(frameCount == targetFPS)
{
averageFPS = 1000/((totalTime/frameCount)/1000000);
frameCount =0;
totalTime = 0;
System.out.println("AverageFPS: "+averageFPS);
}
}
}
public void setRunning(boolean isRunning){
running=isRunning;
}
}
Number.java
package com.example.myapplication;
import android.graphics.Bitmap;
import android.graphics.Canvas;
public class Number {
private Bitmap number;
private int x, y;
Number(Bitmap num, int x, int y){
number=num;
this.x=x;
this.y=y;
}
public void draw(Canvas canvas){
canvas.drawBitmap(number, x, y, null);
}
public void update(){
x++;
}
}
GameView.java
package com.example.myapplication;
import android.content.Context;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
public class GameView extends SurfaceView implements SurfaceHolder.Callback{
private MainThread thread;
private Number One, Two, Three;
public GameView(Context context){
super(context);
getHolder().addCallback(this);
thread=new MainThread(getHolder(), this);
setFocusable(true);
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
One=new Number(BitmapFactory.decodeResource(getResources(), R.drawable.one), 50, 50);
Two=new Number(BitmapFactory.decodeResource(getResources(), R.drawable.ena), 50, 200);
Three=new Number(BitmapFactory.decodeResource(getResources(), R.drawable.one), 50, 350);
thread.setRunning(true);
thread.start();
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
boolean retry=true;
while (retry){
try{
thread.setRunning(false);
thread.join();
} catch(InterruptedException e){
e.printStackTrace();
}
retry=false;
}
}
public void update(){
One.update();
Two.update();
Three.update();
}
@Override
public void draw(Canvas canvas) {
super.draw(canvas);
if(canvas!=null) {
One.draw(canvas);
Two.draw(canvas);
Three.draw(canvas);
}
}
}
第一次绘制精灵,但在第一次绘制之后,它不会移动,除非我取消隐藏并隐藏操作栏
我知道:
- x变量将被更新
- 我的number类中的draw方法被调用
- 线程的循环正常工作