2013-07-17 3 views
-1

내 응용 프로그램이 ppi와 함께 실행중인 화면의 대각선 (인치)을 찾으려고 노력 중이므로 xml에서 일부 개체의 크기를 조정합니다. . Unforenitly 나는 이것을하는 방법에 대한 길을 찾을 수가없는 것 같습니다. 제가 도움이 필요한 것은 인치와 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; 

하고 또한

heightInInches = height/ppi; 

, 왜 PPI에 의해 306 나누어 realppi을 받고? displayMetrics.densityDpi가 정확한 ppi를 반환하지 않습니까? 방금 테스트했는데 제대로 작동하고있는 것 같습니다.

곧 나아지시기 바랍니다. 거기에 매달려!