Android tutorial (12) -- use DisplayMetrics to get the resolution of the mobile phone

Source: Internet
Author: User

In this chapter, we will learn how to use DisplayMetrics to obtain the resolution of a mobile phone.

Yes. When Android phones are so popular around the world, the differences between mobile phones are also due to the different strategic objectives of different manufacturers, resulting in the increasing number of mobile phone differences.

In the four years from 320 to 13 years, the resolution of Android mobile phones has also evolved from 240x960 of the Volkswagen model to 540x1280 or even 720 X with the development of technology.

User requirements are also inconsistent. Although my father's generation also uses Android phones, their demands for mobile phone configuration are still at 800*400, however, I can see that many of my friends around me have more than 960*540 mobile phones.

However, we want to develop a mobile phone that is compatible with the developed software and can be used to connect to Android 1.5 to Android 4.2.

Let's talk about how to get the screen resolution. OK.

Step 1: Create a project Ep. DisplayMetrics first.

Step 2: Put two controls on the view, one is the Button used to trigger the event and the other is the Textview used to receive the result. Code:


[Java]
<RelativeLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Xmlns: tools = "http://schemas.android.com/tools"
Android: layout_width = "match_parent"
Android: layout_height = "match_parent"
Android: paddingBottom = "@ dimen/activity_vertical_margin"
Android: paddingLeft = "@ dimen/activity_horizontal_margin"
Android: paddingRight = "@ dimen/activity_horizontal_margin"
Android: paddingTop = "@ dimen/activity_vertical_margin"
Tools: context = ". MainActivity">
 
<TextView
Android: id = "@ + id/textView1"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: layout_alignParentLeft = "true"
Android: layout_alignParentTop = "true"
Android: text = "cell phone screen resolution:"/>
 
<Button
Android: id = "@ + id/button1"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: layout_alignLeft = "@ + id/textView1"
Android: layout_below = "@ + id/textView1"
Android: layout_marginTop = "19dp"
Android: text = "get cell phone screen resolution"/>
 
</RelativeLayout>

<RelativeLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Xmlns: tools = "http://schemas.android.com/tools"
Android: layout_width = "match_parent"
Android: layout_height = "match_parent"
Android: paddingBottom = "@ dimen/activity_vertical_margin"
Android: paddingLeft = "@ dimen/activity_horizontal_margin"
Android: paddingRight = "@ dimen/activity_horizontal_margin"
Android: paddingTop = "@ dimen/activity_vertical_margin"
Tools: context = ". MainActivity">

<TextView
Android: id = "@ + id/textView1"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: layout_alignParentLeft = "true"
Android: layout_alignParentTop = "true"
Android: text = "cell phone screen resolution:"/>

<Button
Android: id = "@ + id/button1"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: layout_alignLeft = "@ + id/textView1"
Android: layout_below = "@ + id/textView1"
Android: layout_marginTop = "19dp"
Android: text = "get cell phone screen resolution"/>

</RelativeLayout>
We have done a good job in the view.

Step 3: Write an event to get the resolution of the mobile phone. Code:


[Java]
Package com. example. displaymetrics;
 
Import android. OS. Bundle;
Import android. app. Activity;
Import android. util. DisplayMetrics;
Import android. view. Menu;
Import android. view. View;
Import android. view. View. OnClickListener;
Import android. widget. Button;
Import android. widget. TextView;
 
Public class MainActivity extends Activity {
 
Private TextView txtview1;
Private Button btn1;
 
@ Override
Protected void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. activity_main );

Txtview1 = (TextView) findViewById (R. id. textView1 );
Btn1 = (Button) findViewById (R. id. button1 );

Btn1.setOnClickListener (new OnClickListener (){

Private DisplayMetrics dm;
 
@ Override
Public void onClick (View v ){
// The click event is to call the DisplayMetrics class to obtain the resolution of the mobile phone. It is easy to write. Everyone understands it.
Dm = new DisplayMetrics ();
GetWindowManager (). getDefaultDisplay (). getMetrics (dm );
Int wpx = dm. widthPixels;
Int hpx = dm. heightPixels;
String str = txtview1.getText (). toString () + wpx + "*" + hpx;
Txtview1.setText (str );
}
});
}
 
}

Package com. example. displaymetrics;

Import android. OS. Bundle;
Import android. app. Activity;
Import android. util. DisplayMetrics;
Import android. view. Menu;
Import android. view. View;
Import android. view. View. OnClickListener;
Import android. widget. Button;
Import android. widget. TextView;

Public class MainActivity extends Activity {

Private TextView txtview1;
Private Button btn1;

@ Override
Protected void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. activity_main );

Txtview1 = (TextView) findViewById (R. id. textView1 );
Btn1 = (Button) findViewById (R. id. button1 );

Btn1.setOnClickListener (new OnClickListener (){

Private DisplayMetrics dm;

@ Override
Public void onClick (View v ){
// The click event is to call the DisplayMetrics class to obtain the resolution of the mobile phone. It is easy to write. Everyone understands it.
Dm = new DisplayMetrics ();
GetWindowManager (). getDefaultDisplay (). getMetrics (dm );
Int wpx = dm. widthPixels;
Int hpx = dm. heightPixels;
String str = txtview1.getText (). toString () + wpx + "*" + hpx;
Txtview1.setText (str );
}
});
}

}


 

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.