[Android]获得Andriod手机屏幕分辨率的两种方法

public class AndroidTest extends Activity {
	private static String TAG = "AndroidTest";

	private TextView textview1;

	// 获取手机屏幕分辨率的类
    private DisplayMetrics dm;

	@Override
	public void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		// 获取布局中TextView
		textview1 = (TextView)findViewById(R.id.hello);

		// 方法1 Android获得屏幕的宽和高 
	    int screenWidth; 
	    int screenHeight; 

	    WindowManager windowManager = getWindowManager(); 
	    Display display = windowManager.getDefaultDisplay(); 
	    screenWidth = display.getWidth(); 
	    screenHeight = display.getHeight(); 

	    Log.d(TAG, "screenWidth:" + screenWidth);
	    Log.d(TAG, "screenHeight:" + screenHeight);

	    // 方法2
	    dm = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(dm);

        //获得手机的宽带和高度像素单位为px
        String str = "方法1 手机屏幕分辨率为:" + screenWidth
        	+" * "+ screenHeight +
        	"\n方法2  手机屏幕分辨率为:" + dm.widthPixels
        	+" * "+ dm.heightPixels;
        textview1.setText(str);

	}
}
posted @ 2014-06-07 08:54  It's_Lee  阅读(248)  评论(0编辑  收藏  举报