-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);