2010年7月2日星期五

獲取顯示屏幕的信息

通過DisplayMetrics, 可以獲取顯示屏幕的有關信息, 包括:
widthPixels: 屏幕的水平分辨率.
heightPixels: 屏幕的垂直分辨率.
density: 屏幕的邏輯密度.
densityDpi: 以每英寸多少點(dots-per-insh)顯示屏幕的邏輯密度.
scaledDensity: 字體顯示的縮放係數.
xdpi: 水平的每英寸多少確切的物理像素.
ydpi: 水平的每英寸多少確切的物理像素.

顯示屏幕的信息

main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/textscreen"
/>
</LinearLayout>


AndroidDisplayResolution.java
package com.AndroidDisplayResolution;

import android.app.Activity;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.widget.TextView;

public class AndroidDisplayResolution extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

TextView textScreen = (TextView)findViewById(R.id.textscreen);

DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
textScreen.setText(
"Screen Width: " + String.valueOf(dm.widthPixels) + "\n" +
"Screen Height: " + String.valueOf(dm.heightPixels) + "\n" +
"Density: " + String.valueOf(dm.density) + "\n" +
"DensityDPI: " + String.valueOf(dm.densityDpi) + "\n" +
"Scaled Density: " + String.valueOf(dm.scaledDensity) + "\n" +
"X DPI: " + String.valueOf(dm.xdpi) + "\n" +
"Y DPI: " + String.valueOf(dm.ydpi) + "\n");
}
}

沒有留言:

發佈留言