Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wrong widthPixels?

Tags:

android

pixel

    DisplayMetrics displaymetrics = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);

    int w = displaymetrics.widthPixels;
    int h = displaymetrics.heightPixels;

I'm using a "Nexus One"

W shoud be 480 and H shoud be 800 ...

But for me W is 320 and H is 533...

What am I doing wrong???

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

    float CAMERA_WIDTH = displayMetrics.widthPixels
            * getResources().getDisplayMetrics().density;
    ;
    float CAMERA_HEIGHT = displayMetrics.heightPixels
            * getResources().getDisplayMetrics().density;
    ;

    Log.v("" + CAMERA_WIDTH + "---"
            + CAMERA_HEIGHT);

VERBOSE: 320.0---533.0

like image 257
Karl Avatar asked Mar 10 '11 20:03

Karl


2 Answers

Karl, you have to take the screen density into account. You will need to multiply each of those values by...

density = getResources().getDisplayMetrics().density;
int w = displaymetrics.widthPixels * density;
int h = displaymetrics.heightPixels * density;

That will give you the actual screen size

like image 25
Will Tate Avatar answered Oct 12 '22 23:10

Will Tate


Needs a

    <uses-sdk android:minSdkVersion="3" android:targetSdkVersion="10" />
like image 117
Karl Avatar answered Oct 12 '22 22:10

Karl