2013-07-17 25 views
-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); 

回答

0

我不知道你爲什麼要這樣做。我希望它的作品了:

似乎

diagonalInches = Math.sqrt( (widthInInches*widthInInches) + (heightInInches * heightInInches)) 

其中

widthInInches = width/ppi; 

​​3210

而且,你爲什麼除以306 PPI越來越realppi? displayMetrics.densityDpi沒有返回準確的ppi?!我只是測試它,它似乎對我來說正常工作。

我希望你早日有更好的財富。掛在那裏!

相關問題