2013-07-17 28 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?!我只是测试它,它似乎对我来说正常工作。

我希望你早日有更好的财富。挂在那里!

相关问题