屏幕区域的获取:
activity.getWindowManager().getDefaultDisplay();
应用区域的获取:
Rect outRect = new Rect();
activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(outRect);
view图形绘制区域的获取:
Rect outRect = new Rect();
activity.getWindow().findViewById(Window.ID_ANDROID_CONTENT).getDrawingRect(outRect);
下面来测试一下:
package com.example.testactivity; import android.app.Activity;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView; public class TextActivity extends Activity {
private TextView tv;
private Button btn;
//获取手机屏幕分辨率的类
private DisplayMetrics dm;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv=(TextView)findViewById(R.id.textView1);
btn=(Button)findViewById(R.id.button1);
btn.setOnClickListener(new OnClickListener() { @Override
public void onClick(View v) {
// TODO Auto-generated method stub
dm=new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
//获得手机宽高
String strPM="手机分辨率为:"+dm.widthPixels+"*"+dm.heightPixels;
tv.setText(strPM);
}
});
} @Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
} }
效果如图: