Android获得屏幕分辨率的两种方法

package com.wufen.allapp;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.util.DisplayMetrics;
import android.view.Display;
import android.view.Menu;
import android.view.WindowManager;
import android.widget.TextView;

import java.util.List;

public class MainActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        final PackageManager packageManager = this.getPackageManager();
        final Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
        mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
       // mainIntent.setPackage(packageName);       
        final List apps = packageManager.queryIntentActivities(mainIntent, 0);
        
        
        // 方法1 Android获得屏幕的宽和高  
        WindowManager windowManager = getWindowManager();  
        Display display = windowManager.getDefaultDisplay();  
        int screenWidth = screenWidth = display.getWidth();  
        int screenHeight = screenHeight = display.getHeight();  
          
        // 方法2 
        DisplayMetrics dm = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(dm);
        TextView tv = (TextView)this.findViewById(R.id.tv);
        float width=dm.widthPixels*dm.density; 
        float height=dm.heightPixels*dm.density; 
        tv.setText("First method:"+dm.toString()+"\n"+"Second method:"+"Y="+screenWidth+";X="+screenHeight); 

        //dm.widthPixels,dm.heightPixels


    }

}

你可能感兴趣的:(android)