在实际应用中经常要获取屏幕的相关信息,比如高度、宽度、密度以及dp/dip和px之间的转换等等,下面这个示例简单介绍一下,代码如下:
GetSreenInfoActivity:
package com.home.getscreeninfo; import android.app.Activity; import android.content.Context; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; public class GetSreenInfoActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Button getBtn = (Button) findViewById(R.id.main_btn); final EditText widthPxText = (EditText) findViewById(R.id.main_et_width_px); final EditText heightPxText = (EditText) findViewById(R.id.main_et_height_px); final EditText densityText = (EditText) findViewById(R.id.main_et_density); final EditText densityDpiText = (EditText) findViewById(R.id.main_et_density_dpi); final EditText widthDipText = (EditText) findViewById(R.id.main_et_width_dip); final EditText heightDipText = (EditText) findViewById(R.id.main_et_height_dip); getBtn.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // 屏幕宽度(px) int widthPx = GetSreenInfoActivity.this.getResources() .getDisplayMetrics().widthPixels; // 屏幕高度(px) int heightPx = GetSreenInfoActivity.this.getResources() .getDisplayMetrics().heightPixels; widthPxText.setText(widthPx + ""); heightPxText.setText(heightPx + ""); // 屏幕密度:指每平方英寸中的像素数,在DisplayMetrics类中,该密度值为dpi/160 float density = GetSreenInfoActivity.this.getResources() .getDisplayMetrics().density; // 屏幕密度(dpi):指每英寸中的像素数 float densityDpi = GetSreenInfoActivity.this.getResources() .getDisplayMetrics().densityDpi; densityText.setText(density + ""); densityDpiText.setText(densityDpi + ""); // 屏幕宽度(dip) int widthDip = pxToDip(GetSreenInfoActivity.this, widthPx); // 屏幕高度(dip) int heightDip = pxToDip(GetSreenInfoActivity.this, heightPx); widthDipText.setText(widthDip + ""); heightDipText.setText(heightDip + ""); } }); } /** * px值向dip值转换 * * @param context * @param pxValue * @return */ private int pxToDip(Context context, float pxValue) { float scale = context.getResources().getDisplayMetrics().density; return (int) (pxValue / scale + 0.5f); } /** * dip值向px值转换 * * @param context * @param dipValue * @return */ public int dipToPx(Context context, float dipValue) { float scale = context.getResources().getDisplayMetrics().density; return (int) (dipValue * scale + 0.5f); } }布局xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="屏幕密度:" /> <EditText android:id="@+id/main_et_density" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginLeft="10dp" android:editable="false" android:singleLine="true" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="屏幕密度(dpi):" /> <EditText android:id="@+id/main_et_density_dpi" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginLeft="10dp" android:editable="false" android:singleLine="true" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="屏幕宽度(px):" /> <EditText android:id="@+id/main_et_width_px" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginLeft="10dp" android:editable="false" android:singleLine="true" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="屏幕高度(px):" /> <EditText android:id="@+id/main_et_height_px" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginLeft="10dp" android:editable="false" android:singleLine="true" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="屏幕宽度(dip):" /> <EditText android:id="@+id/main_et_width_dip" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginLeft="10dp" android:editable="false" android:singleLine="true" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="屏幕高度(dip):" /> <EditText android:id="@+id/main_et_height_dip" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginLeft="10dp" android:editable="false" android:singleLine="true" /> </LinearLayout> <Button android:id="@+id/main_btn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="获取屏幕信息" /> </LinearLayout>附上图片效果: