Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I Calculate screen height and width at run time?

I have following class in android, where I am defining some functions to calculate motion of Bubbles on the screen.

public class floatBubble  {
     private Bitmap img; // the image of the ball
     private int coordX = 512; // the x coordinate at the canvas
     private int coordY = 600; // the y coordinate at the canvas 
     private int id; // gives every ball his own id, for now not necessary
     private static int count = 1;
     private boolean goRight = true;
     private boolean goDown = true;
   public floatBubble(Context context, int drawable) {
     BitmapFactory.Options opts = new BitmapFactory.Options();
       opts.inJustDecodeBounds = true;
       img = BitmapFactory.decodeResource(context.getResources(), drawable); 
       id=count;
     count++;
    }

    public static int getCount() {
        return count;
    }
    void setX(int newValue) {
        coordX = newValue;
        }
    public int getX() {
        return coordX;
    }
    void setY(int newValue) {
        coordY = newValue;
        }
    public int getY() {
        return coordY;
    }

    public int getID() {
        return id;
    }
    public Bitmap getBitmap() {
        return img;
    }

    public void moveBall(int goX, int goY) {
        // check the borders, and set the direction if a border has reached

        if (coordX > 1024){
            goRight = false;
        }
        if (coordX < -100){
            goRight = false;
            coordX = 512;
            coordY = 600;
        }
        if (coordY > 600){
            goDown = false;
        }
        if (coordY < -100){
            coordY = 600;
            goDown = false;
        }
        // move the x and y 
        if (goRight){
            coordX += goX;
        }else
        {
            coordX -= goX;
        }
        if (goDown){
            coordY += goY;
        }else
        {
            coordY -= goY;
        }
    }
}

THis for the screen resolution 1024*600px. BUt I want to calculate the screensize at runtime. means need to define coordX and coordY at runtime. Can you help me in modifying the code.

I tried to use the following code in my moveBall(), but the code didn't worked there.

DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);      
coordX = dm.widthPixels;
coordY = dm.heightPixels;

Anybody can suggest a solution?

like image 783
jyotiprakash Avatar asked Jan 15 '12 13:01

jyotiprakash


1 Answers

If you want the the display dimensions in pixels you can use

Display display = getWindowManager().getDefaultDisplay(); 
int width = display.getWidth();
int height = display.getHeight();

If you don't have access to an activity to call getWindowManager on. You can use:

Display display = ((WindowManager) context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
int width = display.getWidth();
int height = display.getHeight();
like image 103
Basbous Avatar answered Sep 18 '22 17:09

Basbous