Android的canvas.translate()Android、canvas、translate

2023-09-06 16:46:48 作者:奋斗ゝ

我使用canvas.translate改变我的画布的座标,使视变化

I am using canvas.translate to change the coodinates of my canvas so that the viewport changes

我的code:

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

            }

        }

    }

}

但根本没有任何反应我见过很多例子在线,但我只是不明白我的通过量的问题-.-

but simply nothing happens I have seen many online examples but I just don't get throug my problem -.-

推荐答案

我认为你必须调用翻译(),然后再绘制对象

I think you have to call translate() first, then draw your objects