Basic Coder
Basic Coder

Reputation: 11422

Android OpenGL: Bitmap.getHeight() > actual height

Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.background);

My background.png has a size of 512x512px

I use BitmapFactory to load the Bitmap of the background resource file.

The strang thing:

Log.d("bitmap height", bmp.getHeight() + "px");

always returns 768px.

How is this possible?

My openGL is setup like this:

width = 480;
height = 800;
gl.glViewport(0, 0, width, height);
gl.glMatrixMode(GL10.GL_PROJECTION);
gl.glLoadIdentity();
gl.glOrthof(0.0f, width, 0.0f, height, -1.0f, 1.0f);

gl.glShadeModel(GL10.GL_FLAT);
gl.glEnable(GL10.GL_BLEND);
gl.glBlendFunc(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);
gl.glColor4x(0x10000, 0x10000, 0x10000, 0x10000);
gl.glEnable(GL10.GL_TEXTURE_2D);


gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_FASTEST);
gl.glClearColor(0.5f, 0.5f, 0.5f, 1);
gl.glDisable(GL10.GL_DEPTH_TEST);
gl.glDisable(GL10.GL_DITHER);
gl.glDisable(GL10.GL_LIGHTING);
gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);

Upvotes: 1

Views: 1200

Answers (2)

Matthew Marshall
Matthew Marshall

Reputation: 5873

BitmapFactory will scale the bitmap when the bitmap's DPI doesn't match the screen's DPI. The solution is to set the inScaled option to false:

BitmapFactory.Options opts = new BitmapFactory.Options();
opts.inScaled = false;
Bitmap bmp = BitmapFactory.decodeResource(getResources(),
    R.drawable.background, opts);

Upvotes: 2

Zappescu
Zappescu

Reputation: 1439

768 / 512 = 1.5 This is the factor used for your screen resolution.

Upvotes: 2

Related Questions