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<ResolveInfo> 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获得屏幕分辨率的两种方法
最新推荐文章于 2024-06-27 09:22:32 发布