本文通过为大家展示一段示例代码来介绍Android开发过程中如何获取屏幕像素,相信其在思维上对大家有一定的帮助。
- <span style="font-size:18px;">private void alertScreenDialog() {
- DisplayMetrics dm = new DisplayMetrics();
- getWindowManager().getDefaultDisplay().getMetrics(dm);
- String optStr = "手机屏幕分辨率为: "+dm.widthPixels+" x "+dm.heightPixels;
- Builder builder = new Builder(this);
- builder.setMessage(optStr);
- builder.setIcon(R.drawable.firefox);
- builder.setTitle("获取屏幕像素");
- //点击确定按钮
- builder.setPositiveButton("确定", new DialogInterface.OnClickListener(){
- @Override
- public void onClick(DialogInterface dialog, int which) {
- Toast.makeText(GetScreenActivity.this, "获取到像素", Toast.LENGTH_LONG).show();
- }
- });
- builder.setNegativeButton("返回", new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialog, int which) {
- Toast.makeText(GetScreenActivity.this, "返回", Toast.LENGTH_LONG).show();
- }
- });
- builder.create().show();
- }</span>