在Android中移动SurfaceView

时间:2012-12-02 10:21:56

标签: android surfaceview

我正在开发一款像BSD robots这样的简单游戏。这个游戏包含一个相当大的板(超过40个单元),即使在10英寸平板电脑也不会看起来不错。我的决定是滚动(移动)表面视图而不是缩放尺寸就像指点的每个图形。 我已经阅读了很多关于表面视图的文章,但我无法理解如何移动它?

我的活动代码:

package ru.onyanov.robots;
public class MainActivity extends Activity {

public BoardView board;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
    getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
    requestWindowFeature(Window.FEATURE_NO_TITLE);

    board = new BoardView(this);
    setContentView(board);
}
}

班主席:

package ru.onyanov.robots;
public class BoardView extends SurfaceView {
private GameThread mThread;
private boolean running = false;
public final int sizeX = 50;
public final int sizeY = 35;
public final int cellSize = 64;

private int robotCount = 4;

public ArrayList<Cell> cells = new ArrayList<Cell>();
private ArrayList<Robot> robots = new ArrayList<Robot>();
private Hero hero;
public Bitmap imageCell;
public Bitmap imageRobot;
public Bitmap imageHero;

public BoardView(Context context) {
    super(context);     

    makeGraphic();
    constructCells();
    constructRobots();      
    hero = new Hero(this, 3, 2, imageHero);

    mThread = new GameThread(this);

    getHolder().addCallback(new SurfaceHolder.Callback() {
        public void surfaceDestroyed(SurfaceHolder holder) {
            boolean retry = true;
            mThread.setRunning(false);
            while (retry) {
                try {
                    mThread.join();
                    retry = false;
                } catch (InterruptedException e) {
                }
            }
        }

        public void surfaceCreated(SurfaceHolder holder) {
            mThread.setRunning(true);
            mThread.start();
        }

        public void surfaceChanged(SurfaceHolder holder, int format,
                int width, int height) {


        }
    });
}

private void makeGraphic() {
    Bitmap cellImageSource = BitmapFactory.decodeResource(getResources(),
            R.drawable.cell);
    imageCell = Bitmap.createScaledBitmap(cellImageSource, cellSize,
            cellSize, false);
    imageRobot = BitmapFactory.decodeResource(getResources(),
            R.drawable.robot);
    imageHero = BitmapFactory.decodeResource(getResources(),
            R.drawable.hero);

}

private void constructRobots() {
    Robot robot;
    Random rand = new Random();
    for (int r = 0; r < robotCount; r++) {
        int x = rand.nextInt(sizeX);
        int y = rand.nextInt(sizeY);
        robot = new Robot(this, x, y, imageRobot);
        robots.add(robot);
    }
    return;
}

private void constructCells() {
    Cell cell;
    for (int y = 0; y < sizeY; y++) {
        for (int x = 0; x < sizeX; x++) {
            cell = new Cell(this, x, y, imageCell);
            cells.add(cell);
        }
    }
    return;
}

protected void onDraw(Canvas canvas) {
    canvas.drawColor(Color.WHITE);
    Cell cell;
    for (int c = 0; c < cells.size(); c++) {
        cell = cells.get(c);
        cell.onDraw(canvas);
    }
    Robot robot;
    for (int r = 0; r < robots.size(); r++) {
        robot = robots.get(r);
        robot.onDraw(canvas);
    }
    hero.onDraw(canvas);
}

public boolean onTouchEvent(MotionEvent e) {
    int shotX = (int) e.getX();
    int shotY = (int) e.getY();

    if (e.getAction() == MotionEvent.ACTION_MOVE){
        //TODO move board
        showToast("move Board");
    } else if (e.getAction() == MotionEvent.ACTION_UP) {
        showToast("touchPoint: " + shotX + ", "+shotY);
        hero.moveByDirection(shotX, shotY);
        this.scrollTo(shotX, shotY);
    }
    return true;
}

public void showToast(String mes) {
    Toast toast = Toast.makeText(getContext(), mes, Toast.LENGTH_LONG);
    toast.show();
}

public class GameThread extends Thread {
    private BoardView view;

    public GameThread(BoardView view) {
        this.view = view;

    }

    public void setRunning(boolean run) {
        running = run;
    }

    public void run() {
        while (running) {
            Canvas canvas = null;
            try {
                canvas = view.getHolder().lockCanvas();
                synchronized (view.getHolder()) {
                    onDraw(canvas);                     
                }
                canvas.scale(300, 300);
            } catch (Exception e) {
            } finally {
                if (canvas != null) {
                    view.getHolder().unlockCanvasAndPost(canvas);
                }
            }
        }
    }
}
}

这里的一些神奇数字是暂时的。现在我只想移动BoardView;

1 个答案:

答案 0 :(得分:0)

问题是向SurfaceView添加xy字段,在onTouchEvent更改它们,并使用x和y偏移绘制画布中的所有元素。奇怪的是我没有收到任何答案......