I need to know how to get drawn image length and width.

I have this code so far where I draw Radar in left top corner with hard-coded coordinates:

int _SRC = R.drawable._item; 


float vertices[] = {
            0f,  0f,  0.0f,
            0f,  2.0f,0.0f,
            2.0f,0f,  0.0f,         



public void addRadarToView(float x, float y) {      
    this.x = x;
    this.y = y;     


private void updateScreenData(float screenWidth, float screenHeight){
    // takes screen Height and Width
    this.mScreenWidth = (screenWidth > 0) ? screenWidth : 1f;
    this.mScreenHeight = screenHeight;
    wRatio = 10f/mScreenWidth;
    hRatio = mScreenHeight/10f;     

public void loadTextures(GL10 gl) {
    Log.e(LOG_TAG, "loadTextures");  

    InputStream is;

    is = mContext.getResources().openRawResource(_SRC);

    Bitmap bitmap = BitmapFactory.decodeStream(is);

    try {
        is = null;
    } catch (IOException e) {

    gl.glGenTextures(TEXTURE_COUNT, textures, 0);

    gl.glBindTexture(GL10.GL_TEXTURE_2D, textures[0]);
    GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bitmap, 0);



 protected void drawRadar(GL10 gl){
    try {           
        float transx =  (wRatio * (x)); 
        float transy =  (mScreenHeight*wRatio) - (wRatio * (y)) - 1/hRatio; 
        // Log.e(LOG_TAG, "transx: " + transx + "; transy: " + transy + "; sprite.x: "+  sprite.x + "; sprite.y: " + sprite.y);  

        gl.glTranslatef(transx, transy, 0.0f);

        //###########  draw ##############

        gl.glBindTexture(GL10.GL_TEXTURE_2D, textures[0]);

        gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, floatBufferArray[mFrame]);


        gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertexBuffer);
        gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, vertices.length / 3);


    } catch (NullPointerException e) {

I used addRadarToView(0,240); to set x=0 and y= 240;

So as I see x represents left border of vertices. When 240 is y that represents bottom border. Strange.


In canvas i used something like:

 int srcX = currentFrame * width;

Rect  src = new Rect(srcX, 0, srcX+width, height);
Rect  dst = new Rect(x, y, x+width, y+height);
canvas.drawBitmap(bitmap, src, dst, null);

Where width and height are:


Please, help me to sort it out.


  • \$\begingroup\$ Did you call updateScreenData() with the correct resolution? \$\endgroup\$
    – Mario
    Commented Jul 25, 2013 at 9:54
  • \$\begingroup\$ yep, from Parent class, its working code, just want to replace hard-coded part: addRadarToView(0,240); \$\endgroup\$ Commented Jul 25, 2013 at 10:53
  • \$\begingroup\$ Not really sure what you're asking then? Do you want to calculate the actual coordinates where the radar is drawn to? \$\endgroup\$
    – Mario
    Commented Jul 25, 2013 at 10:54
  • \$\begingroup\$ when I draw vertice on the screen, I tell where to draw it (x,y). But position should be based on Bitmap size. otherwise if image is big, it can be out of the screen \$\endgroup\$ Commented Jul 25, 2013 at 10:57
  • \$\begingroup\$ Still not really sure what you're looking for. Are you unhappy with the origin of the radar screen (i.e. the point you pass to draw it)? \$\endgroup\$
    – Mario
    Commented Jul 25, 2013 at 11:06

1 Answer 1


This is one of the great things about openGL over canvas. OpenGL and android will handle the scaling of the image for you. Depending on how you build your renderer class will determine exactly how you specify your vertices location, but generally if you follow a how to guide you will always be working in a plane from -1 to +1 for 2D games. This means that if you declare a bitmap within this plane you can simply use translatef() to move the image where it needs to be.

After looking at your code I believe the problems you are having is because you are attempting to use pixels to determine an amount to translate your image by. It is important to use raw measurements from a 1.0 dpi device to get your base pixel location, otherwise it will not scale properly.

This is how I did it here and here

To summarize I believe your confusion is coming from the fact that openGL does not use pixels to determine location but instead uses a vertices that are translated during run.


You must log in to answer this question.

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