1
\$\begingroup\$

I am developing a game with 1024x600 resolution. I can't target smaller resolution devices at this resolution, so I intend to use two graphics for the game. How do I detect which graphics to use for which device?

\$\endgroup\$

2 Answers 2

2
\$\begingroup\$

In your main activity, you can call:

DisplayMetrics displayMetrics = new DisplayMetrics();
this.getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);

displayMetrics will then give you a bunch of data, for example:

displayMetrics.density;

displayMetrics.densityDpi;

displayMetrics.heightPixels;

And so forth. In the comments below, there is a request for sample code to handle variable screen resolutions (i.e., to eliminate letterboxing). Sample code to accomplish this is available here.

\$\endgroup\$
13
  • \$\begingroup\$ I want to ask that is it correct to use two graphics? Because game size increased atleast 50% of normal game size. \$\endgroup\$
    – Siddharth
    Feb 24, 2013 at 4:34
  • \$\begingroup\$ @Siddharth It's not "incorrect" in any official sense, although it's more common with AndEngine to develop at a lower resolution and let the engine scale it up. \$\endgroup\$ Feb 24, 2013 at 4:35
  • \$\begingroup\$ @Siddharth for what it's worth, I use the displayMetrics just to determine the aspect ratio of the device. In other words, I define a set height, and use the aspect ratio to figure out the width. AndEngine is really designed to take care of scaling for you. \$\endgroup\$ Feb 24, 2013 at 4:40
  • \$\begingroup\$ Please you can provide some code snippet for what you said and what graphics as you perspective I need to prepare from graphics designer. Thank for your response. \$\endgroup\$
    – Siddharth
    Feb 24, 2013 at 4:44
  • \$\begingroup\$ @Siddharth added some code per your request. \$\endgroup\$ Feb 24, 2013 at 4:49
0
\$\begingroup\$

I have found other solution for this and test it my self.

Dealing with all the device resolutions scattered around the globe. The existing resolution policies offer either a stretch-to-fit scale, black bars or leave it to you to do the scaling.

The class I have here is another option that scales your screen up to fill the screen, whilst retaining the 1:1 assets ratio of your graphics.

public class CroppedResolutionPolicy extends BaseResolutionPolicy
{

    private final float mCameraWidth;
    private final float mCameraHeight;
    private float mMarginVertical;  // Pixels from top of canvas to visible area, and from bottom of canvas to visible area
    private float mMarginHorizontal;        // Pixels from left of canvas to visible area, and from right of canvas to visible area


    public CroppedResolutionPolicy(final float nCameraWidth, final float nCameraHeight)
    {
            this.mCameraWidth = nCameraWidth;
            this.mCameraHeight = nCameraHeight;

            this.mMarginVertical = 0;
            this.mMarginHorizontal = 0;
    }

    // ===========================================================
    // Getter & Setter
    // ===========================================================
    public float getMarginVertical()
    {
            return this.mMarginVertical;
    }

    public float getMarginHorizontal()
    {
            return this.mMarginHorizontal;
    }


    @Override
    public void onMeasure(RenderSurfaceView pRenderSurfaceView, int pWidthMeasureSpec, int pHeightMeasureSpec)
    {
            BaseResolutionPolicy.throwOnNotMeasureSpecEXACTLY(pWidthMeasureSpec, pHeightMeasureSpec);

            int measuredWidth = MeasureSpec.getSize(pWidthMeasureSpec);
            int measuredHeight = MeasureSpec.getSize(pHeightMeasureSpec);

            final float nCamRatio = (float)mCameraWidth /  (float)mCameraHeight;
            final float nCanvasRatio = (float)measuredWidth /  (float)measuredHeight;

            if(  (float)measuredWidth /  (float)measuredHeight < nCamRatio )
            {
                    // Scale to fit height, width will crop
                    measuredWidth = (int) ( measuredHeight * nCamRatio);
                    this.mMarginHorizontal = ( this.mCameraWidth - ( (float) this.mCameraHeight * nCanvasRatio ) ) / 2.0f;
            }
            else
            {
                    // Scale to fit width, height will crop
                    measuredHeight = (int) ( measuredWidth / nCamRatio );
                    this.mMarginVertical = ( this.mCameraHeight - ( (float) this.mCameraWidth / nCanvasRatio ) ) / 2.0f;
            }

            pRenderSurfaceView.setMeasuredDimensionProxy(measuredWidth, measuredHeight);
    }
}

Use this class in engine creation in the following manner.

final CroppedResolutionPolicy canvasSurface = new CroppedResolutionPolicy( CAMERA_WIDTH, CAMERA_HEIGHT );
final EngineOptions engineOptions = new EngineOptions( true, ScreenOrientation.LANDSCAPE_FIXED, canvasSurface, new Camera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT) );
\$\endgroup\$
1

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .