Android canvas.translate()

时间:2012-03-29 19:20:07

标签: android canvas viewport

我使用canvas.translate来更改画布的坐标,以便视口更改

我的代码:

public class draw extends View {
    View v;
    Paint paint;

    int width;
    int height;

    int view_x;
    int view_y;

    static final int MAX_GAME_SPEED = 25;
    static int fps;

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

        // tb.loadimg();

        Thread myThread = new Thread(new UpdateThread());
        myThread.start();
    }

    @Override
    protected void onDraw(Canvas c) {
        super.onDraw(c);
        paint = new Paint(); // Paint paint = new Paint();
        paint.setStyle(Paint.Style.FILL);

        // get screen size
        WindowManager wm = (WindowManager) this.getContext().getSystemService(
                Context.WINDOW_SERVICE);
        Display display = wm.getDefaultDisplay();

        width = display.getWidth(); // deprecated
        height = display.getHeight(); // deprecated

        // make the entire canvas white
        paint.setColor(Color.WHITE);
        c.drawPaint(paint);

        if (tb.dirt != null && tb.stone != null && tb.sand != null
                && tb.gold != null && tb.iron != null && tb.coal != null
                && tb.kies != null && tb.diamond != null && tb.redstone != null
                && tb.lava != null && tb.azur != null && tb.water != null) {
            c.drawBitmap(tb.dirt, 0, 0, paint);
            c.drawBitmap(tb.stone, 0, 50, paint);
            c.drawBitmap(tb.sand, 0, 100, paint);
            c.drawBitmap(tb.gold, 0, 150, paint);
            c.drawBitmap(tb.iron, 50, 0, paint);
            c.drawBitmap(tb.coal, 50, 50, paint);
            c.drawBitmap(tb.kies, 50, 100, paint);
            c.drawBitmap(tb.diamond, 50, 150, paint);
            c.drawBitmap(tb.redstone, 100, 0, paint);
            c.drawBitmap(tb.lava, 100, 50, paint);
            c.drawBitmap(tb.azur, 100, 100, paint);
            c.drawBitmap(tb.water, 100, 150, paint);

        }
        if (tb.map == null) {

        }
        view_x = 100;
        view_y = 100;

        c.translate(0, -4);

    }

    public Handler updateHandler = new Handler() {
        /** Gets called on every message that is received */
        // @Override
        public void handleMessage(Message msg) {

            invalidate();
            super.handleMessage(msg);
        }
    };

    public class UpdateThread implements Runnable {

        @Override
        public void run() {
            while (true) { // Game Loop

                long startTime = System.currentTimeMillis();
                draw.this.updateHandler.sendEmptyMessage(0); // veranlassen,
                                                                // dass paint()
                                                                // erneut
                                                                // aufgerufen
                                                                // werden soll
                // for (int i=0; i<999999; i++); //Bremse

                Thread.yield();
                long executionTime = System.currentTimeMillis() - startTime;

                if (executionTime < MAX_GAME_SPEED) {
                    try {
                        Thread.sleep(MAX_GAME_SPEED - (int) executionTime);
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                    fps = 1000 / MAX_GAME_SPEED;
                } else
                    fps = (int) (1000 / executionTime);

            }

        }

    }

}

但根本没有任何反应 我已经看过很多在线示例,但我只是没有通过我的问题-.-

2 个答案:

答案 0 :(得分:4)

我认为你必须先调用translate(),然后绘制你的对象

答案 1 :(得分:-1)

我想如果你翻译(0,-4), 现在你的c坐标不在屏幕上