1. 程式人生 > >Android學習第四天 螢幕解析度(圖片框的寬度)

Android學習第四天 螢幕解析度(圖片框的寬度)

獲取螢幕解析度

DisplayMetrics metric = new DisplayMetrics(); getWindowManager().getDefaultDisplay().getMetrics(metric); int width =metric.widthPixels;//寬度(PX) int height =metric.heightPixels;//高度(PX) int column = 4;//每行有四個圖片框 int averageImage = width/column;//每個圖片框的畫素點數

		layout=findViewById(R.id.main_gridLayout);
        DisplayMetrics metric = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(metric);
        int width =metric.widthPixels;//寬度(PX)      
        setInfoText("width: " + width);
        int height =metric.heightPixels;//高度(PX)      
        setInfoText("height: " + height);
        float density =metric.density;//密度(0.75 / 1.0 / 1.5)      
        setInfoText("density: " + density);
        int densityDpi =metric.densityDpi;//密度DPI(120 / 160 / 240)
        setInfoText("densityDpi: " + densityDpi);
        int column = 4;
        int averageImage = width/column;
        for (int i=0;i<imagePath.length;i++)
        {
            img[i]=new ImageView(MainActivity.this);
            img[i].setImageResource(imagePath[i]);
            ViewGroup.LayoutParams params=new ViewGroup.LayoutParams(averageImage,averageImage);
            img[i].setLayoutParams(params);
            layout.addView(img[i]);
        }

在這裡插入圖片描述