1

我尝试在 onDraw 期间显示位图列表中的一个。
当我将列表传递到画布时,所有内容都显示并留在它们的位置。
当我传递一个随机位图时,它一直在重绘画布。
当我使用 public void drawEnemy(Canvas canvas) 时,所有工作都有效,但与使用 public void drawEn(Canvas canvas) 时不完全一样。
我想显示一个随机位图,然后几秒钟后,将其删除并显示其他位图。我认为问题在于我如何实现 onDrow() 方法。它一直在重绘画布。

活动:

public class NewGameActivity extends Activity{
NewGame newgame;
public void onCreate(Bundle savedInstanceState){
     super.onCreate(savedInstanceState);
        // Landscape mode
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
        // no title
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        // content Newgame.java
        newgame = new NewGame(this);
        setContentView(newgame);
 }

线:

public class MainThread extends Thread{

private SurfaceHolder surfaceHolder;
private NewGame screen;

public MainThread(SurfaceHolder surfaceHolder, NewGame ekran) {
    super();
    this.surfaceHolder = surfaceHolder;
    this.screen= screen;
}

private boolean running;
public void setRunning(boolean running) {
    this.running = running;
}

@Override
public void run() {
    Canvas canvas;
    while (running) {
        canvas = null;
        try {
            canvas = this.surfaceHolder.lockCanvas();
            synchronized (surfaceHolder) {
                this.screen.onDraw(canvas);
            }
        } finally {
            if (canvas != null) {
                surfaceHolder.unlockCanvasAndPost(canvas);
            }
        }
    }

}

}

表面视图:

public class NewGame extends SurfaceView implements SurfaceHolder.Callback{

private MainThread thread;
private EnemyManager manager;

public NewGame(Context context) {
    super(context);
     getHolder().addCallback(this);
     thread = new MainThread(getHolder(), this);
     manager = new EnemyManager();
    // TODO Auto-generated constructor stub
//adding enemy
     Enemy e1 = new Enemy(BitmapFactory.decodeResource(getResources(), R.drawable.card), 1);
     Enemy e2 = new Enemy(BitmapFactory.decodeResource(getResources(), R.drawable.horse), 2);
     EnemyLocation l1 = new EnemyLocation(60, 180);
     EnemyLocation l2 = new EnemyLocation(60, 50);
     manager.AddEnemy(e1, l1);
     manager.AddEnemy(e2, l2);
     setFocusable(true);
}

@Override
protected void onDraw(Canvas canvas) {
     canvas.drawBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.saloon), 0, 0, null);
        manager.drawEn(canvas);

}

@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
        int height) {
    // TODO Auto-generated method stub

}

@Override
public void surfaceCreated(SurfaceHolder holder) {
    thread.setRunning(true);
    thread.start();
}


@Override
public void surfaceDestroyed(SurfaceHolder holder) {
     thread.setRunning(false);
     thread.stop();
}

@Override
public boolean onTouchEvent(MotionEvent event) {
    if (event.getAction() == MotionEvent.ACTION_DOWN) {
        manager.handleActionDown((int)event.getX(), (int)event.getY());
    }
    return true;
}

}

敌人经理:

public class EnemyManager {
private ArrayList<Enemy> enemyList;
private ArrayList<Enemy> suspects;
private Enemy cow;
private String message;
private int suspectID;
private Random rnd;


public String getMessage() {
    return message;
}

public EnemyManager(){
    enemyList = new ArrayList<Enemy>();
    suspects = new ArrayList<Enemy>();
}


public void AddEnemy(Enemy enemy, EnemyLocation loc){
    // set x,y enemy localization
    enemy.setX(loc.getX());
    enemy.setY(loc.getY());
    enemyList.add(enemy);
}

public void clearEnemy() {
    enemyList.clear();
}

// message if enemy touched
public void handleActionDown(int x, int y) {
    for (Enemy enemy: enemyList) {
        if (enemy.wasTouched(x, y)) {
            message = enemy.getId();
            return;
        }
    }
}

public void PrepareEnemy(){
    suspectID = enemyList.get(rnd.nextInt(enemyList.size()+1)).getId();
    suspects = new ArrayList<Enemy>();
    suspects.add(getSuspectByID(suspectID));
}

private Enemy SingleEnemy(){
    Double i = 1 + Math.random() * ((enemyList.size()-1)+1);
    cow = getSuspectByID(i.intValue());
    return cow;
}

private Enemy getSuspectByID(int suspectID) {
    for (Enemy s: enemyList) {
        if (s.getId() == suspectID) {
            return s;
        }
    }
    return null;
}


public void drawEn(Canvas canvas){
    try {
        Enemy k = SingleEnemy();
        canvas.drawBitmap(cow.picture, cow.x, cow.y, null);
    } catch (Exception e) {
        // TODO: handle exception
    }
}

// draw enemy
public void drawEnemy(Canvas canvas) {
    try {
        for (Enemy enemy: enemyList) {
            canvas.drawBitmap(enemy.picture, enemy.x, enemy.y, null);
        }
    } catch (Exception e) {
        // TODO: handle exception
    }

}

}

达斯

4

1 回答 1

0

据了解,您正在尝试做这样的事情(如果不是,请纠正我):这是用所有组件渲染画布:

  • 绘制背景
  • 绘制敌人

要“刷新”画布,您只需执行以下操作:

  • 绘制背景

更新

要暂停渲染,您可以执行以下操作:

int lastUpdateTime;
int delayTime = 2000; 2 seconds

if(System.currenttimeMillis() > lastUpdateTime + delayTime) {
// Finished waiting
}

您应该只定义lastUpdateTime您想要等待的时间,而不是在每次迭代中。

注意:不要在渲染线程中调用 Thread.sleep() !

于 2012-07-30T19:19:15.863 回答