Android discussion --- getting the phone screen size

Source: Internet
Author: User

 

Android discussion --- getting the phone screen size

SurfaceView simple example

Go to Android --- UI --- ZoomControls to zoom in and out images

For more information, here is a summary of the operations on the mobile phone screen in android:

1. Get the screen resolution (because when processing the display in android, you need to go to different la s or display different materials according to the screen resolution)

Java code

// Obtain it through WindowManager

DisplayMetrics dm = new DisplayMetrics ();

GetWindowManager (). getDefaultDisplay (). getMetrics (dm );

System. out. println ("heigth:" + dm. heightPixels );

System. out. println ("width:" + dm. widthPixels );

// Obtain from Resources

DisplayMetrics dm2 = getResources (). getDisplayMetrics ();

System. out. println ("heigth1:" + dm2.heightPixels );

System. out. println ("width1:" + dm2.widthPixels );

// Obtain the default screen resolution

Display display = getWindowManager (). getDefaultDisplay ();

System. out. println ("width-display:" + display. getWidth ());

System. out. println ("heigth-display:" + display. getHeight ());

// Obtain it through WindowManager

DisplayMetrics dm = new DisplayMetrics ();

GetWindowManager (). getDefaultDisplay (). getMetrics (dm );

System. out. println ("heigth:" + dm. heightPixels );

System. out. println ("width:" + dm. widthPixels );

// Obtain from Resources

DisplayMetrics dm2 = getResources (). getDisplayMetrics ();

System. out. println ("heigth1:" + dm2.heightPixels );

System. out. println ("width1:" + dm2.widthPixels );

// Obtain the default screen resolution

Display display = getWindowManager (). getDefaultDisplay ();

System. out. println ("width-display:" + display. getWidth ());

System. out. println ("heigth-display:" + display. getHeight ());

2. Remove the screen title and full screen display

Java code

// Remove the title

RequestWindowFeature (Window. FEATURE_NO_TITLE );

// Set full screen

GetWindow (). setFlags (WindowManager. LayoutParams. FLAG_FULLSCREEN,

WindowManager. LayoutParams. FLAG_FULLSCREEN );

// Remove the title

RequestWindowFeature (Window. FEATURE_NO_TITLE );

// Set full screen

GetWindow (). setFlags (WindowManager. LayoutParams. FLAG_FULLSCREEN,

WindowManager. LayoutParams. FLAG_FULLSCREEN );

3. Set the screen direction

Configure Activity attributes in the Manifest. xml file

Xml Code

<Activity android: name = ". AnimateActivity" android: label = "@ string/app_name"

Android: screenOrientation = "landscape"> <! -- Landscape portrait -->

<Intent-filter>

<Action android: name = "android. intent. action. MAIN"/>

<Category android: name = "android. intent. category. LAUNCHER"/>

</Intent-filter>

</Activity>

<Activity android: name = ". AnimateActivity" android: label = "@ string/app_name"

Android: screenOrientation = "landscape"> <! -- Landscape portrait -->

<Intent-filter>

<Action android: name = "android. intent. action. MAIN"/>

<Category android: name = "android. intent. category. LAUNCHER"/>

</Intent-filter>

</Activity>

It is usually controlled in the onCreate and onDestroy methods in the Activity, because when the screen direction changes,

The system restarts the Activity. Therefore, you need to save the relevant data before the Activity is destroyed to facilitate the next onCreate method.

Reload and update the screen Layout

Java code

Public void onCreate (Bundle savedInstanceState ){

// Force horizontal screen

SetRequestedOrientation (ActivityInfo. SCREEN_ORIENTATION_LANDSCAPE );

// TODO updates the screen Layout

}

 

Public void onDestroy (){

If (getRequestedOrientation () =

ActivityInfo. SCREEN_ORIENTATION_LANDSCAPE ){

// Save data

} Else if (getRequestedOrientation () =

ActivityInfo. SCREEN_ORIENTATION_PORTRAIT ){

// Save data

}

}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.