-1
我正在尝试查找屏幕的对角线(以英寸为单位),我的应用程序随ppi一起运行,因此它将调整我在xml中设置的某些对象的大小。不言而喻,我似乎无法找到如何做到这一点的方法。我需要帮助的是如何找到英寸和PPI的对角线。查找屏幕大小和对角线以调整对象的大小
public class MainActivity extends Activity implements OnClickListener {
Button v1, v2, v3, v4, v5, v6, v7, v8, stop;
Vibrator v;
int screenWidth, screenHeight, size, ppi, realppi, height, width, inwidth = 33, inheight = 7;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
DisplayMetrics displayMetrics = new DisplayMetrics();
WindowManager wm = (WindowManager) getApplicationContext().getSystemService(Context.WINDOW_SERVICE); // the results will be higher than using the activity context object or the getWindowManager() shortcut
wm.getDefaultDisplay().getMetrics(displayMetrics);
screenWidth = displayMetrics.widthPixels;
screenHeight = displayMetrics.heightPixels;
ppi = displayMetrics.densityDpi;
realppi = 306/ppi;
DisplayMetrics metrics=new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
float height=metrics.heightPixels/metrics.xdpi;
float width=metrics.widthPixels/metrics.ydpi;
size = (int) FloatMath.sqrt(height*height+width*width);
height = inwidth * size * realppi;
width = inwidth * size * realppi;
v1.setHeight((int) height);
v1.setWidth((int) width);
setContentView(R.layout.activity_main);
stop = (Button) findViewById(R.id.bstop);
stop.setOnClickListener(this);
v1 = (Button) findViewById(R.id.bvibrate1);
v1.setOnClickListener(this);