Scale a fixed SurfaceView to fill vertically and maintain aspect ratio

I am trying to find an answer and cannot find it (this is a rather unconventional approach to programming a game for Android, so I think this can be expected).

I am trying to create a GBA style RPG game in Android (for nostalgia). OpenGL was not good enough for me because it was not customizable enough in the game loop (I only need the call to be called when there is something to draw, since I want this game to be more effective in how she uses the battery, which means that it acts more like a polling application than a game). I created a custom surface view with my own thread, which is called when I need to draw a drawing (I modeled it after the sample LunarLander android game in the SDK).

The fact is that I want the game itself to be a fixed size (208x160), and then scaled to the screen size. The problem I am facing, in any case, apparently cannot be done from the usual parameters of the extended SurfaceView in XML. The game in the current state is shown below. I try to make it stretched to the height of the screen and maintain the ratio in width, and also not add to the game viewing (fixing it regardless of the screen size).

http://i.stack.imgur.com/EWIdE.png (I was going to post the actual image in a line. Anti-spam might bite me>. <)

I am currently getting a SurfaceView canvas and drawing directly to it and supply my size as a hard-coded pixel size (208 pixels, 160 pixels).

. , , , ?

@Override
    public void run()
    {
        Canvas c = null;
        try
        {
            c = surfaceHolder.lockCanvas(null);
            synchronized (surfaceHolder)
            {
                draw(c);
            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        finally
        {
            if (c != null)
                surfaceHolder.unlockCanvasAndPost(c);
        }
    }

draw - (StringBuilder - ):

public void draw(Canvas canvas)
{       
    canvas.drawColor(Color.GRAY);
    StringBuilder stringBuilder = new StringBuilder(canvas, Color.BLACK, letters);
    stringBuilder.drawString("Oh, hello there!");
    stringBuilder.setLocation(10, 20);
    stringBuilder.drawString("Why am I so small?");
}

?

+5
2

, draw drawBase. :

public void draw(Canvas canvas)
{
    final float scaleFactor = Math.min( getWidth() / 208.f, getHeight() / 160.f );
    final float finalWidth = 208.f * scaleFactor;
    final float finalHeight = 160.f * scaleFactor;
    final float leftPadding = ( getWidth() - finalWidth ) / 2;
    final float topPadding =  ( getHeight() - finalHeight ) / 2;

    final int savedState = canvas.save();
    try {
        canvas.clipRect(leftPadding, topPadding, leftPadding + finalWidth, topPadding + finalHeight);

        canvas.translate(leftPadding, topPadding);
        canvas.scale(scaleFactor, scaleFactor);

        drawBase(canvas);
    } finally {
        canvas.restoreToCount(savedState);
    }
}
+4

( K-ballo , 2,5 , , , , . , . )

SurfaceHolder # setFixedSize(). , . , View , , . , " " Grafika's ( ).

, ( Grafika ) View to letter pillar. Grafika AspectFrameLayout, .

:

OpenGL , ( , , -, ...

. -, GLSurfaceView # setRenderMode(), . -, GLSurfaceView OpenGL. GLES Grafika SurfaceView TextureView.

+1

All Articles