0

I developing Application for Galaxy Tab2. It has screen resolution of 600*1024(169 PPI). According to Android Document,It comes under HDPI Android resolution bucket.Then smallest Width of this device is 600 PPI. Then divide by 1.50 gives 400 dp. So its smallestWidth is 400 dp. Then it goes to sw-320dp folder. But actually it should be in sw600dp.

So my doubt is the Tab2 id HDPI or MDPI? or I done any mistakes in calculations?

My Code:

public void calculate(View view)
    {
        EditText widthResolution=(EditText)findViewById(R.id.dip_et_screenwidthres);
        EditText heightResolution=(EditText)findViewById(R.id.dip_et_screenheightres);
        EditText size=(EditText)findViewById(R.id.dip_et_screensize);


        String screenWidth = widthResolution.getText().toString();
        Double screenWidthD=Double.valueOf(screenWidth);

        String screenHeight = heightResolution.getText().toString();
        Double screenHeightD=Double.valueOf(screenHeight);

        String screenSize = size.getText().toString();
        Double screenSizeD=Double.valueOf(screenSize);

        Double sumOfXY2=(screenWidthD*screenWidthD)+(screenHeightD*screenHeightD);
        double totalDiagonalRes = Math.sqrt(sumOfXY2);

        Double diagonalPPI =totalDiagonalRes/screenSizeD;

        //used for calculation
        int diagonalPPIInt=diagonalPPI.intValue();

        //Diagonal Resolution in PPI
        String resultDiagonalResolution=String.format("%.2f", diagonalPPI);

        //Android Resolution Bucket
        String resultAndroidResBucket="";
        String resultDiagonalDPSize="";
        String resultWidthHeightDP="";
        String resultSmallestWidthDP="";
        String resultLayoutFolder="";
        String resultDrawableFolder="";

        double smallestWidthDPTemp = 0;

        if(diagonalPPIInt<120)
        {
            resultAndroidResBucket="LDPI"; //0.75

            //Diagonal size
            double diagonalDPSize=diagonalPPI/0.75;
            resultDiagonalDPSize=String.format("%.2f", diagonalDPSize);

            //Width * Height
            double screenWidthDP=screenWidthD/0.75;
            String screenWidthTemp=String.format("%.2f", screenWidthDP);

            double screenHeightDP=screenHeightD/0.75;
            String screenHeightTemp=String.format("%.2f", screenHeightDP);

            resultWidthHeightDP=screenWidthTemp+" dp * "+screenHeightTemp+" dp";

            //Smallest Width
            smallestWidthDPTemp=Math.min(screenWidthDP, screenHeightDP);
            resultSmallestWidthDP=String.format("%.2f", smallestWidthDPTemp);

            //DrawableFolder
            resultDrawableFolder="drawable-ldpi";
        }
        else if(diagonalPPIInt >= 120 && diagonalPPIInt < 160)
        {
            resultAndroidResBucket="MDPI"; //0.00

            //Diagonal size
            double diagonalDPSize=diagonalPPI;
            resultDiagonalDPSize=String.format("%.2f", diagonalDPSize);

            //Width * Height
            double screenWidthDP=screenWidthD;
            String screenWidthTemp=String.format("%.2f", screenWidthDP);

            double screenHeightDP=screenHeightD;
            String screenHeightTemp=String.format("%.2f", screenHeightDP);

            resultWidthHeightDP=screenWidthTemp+" dp * "+screenHeightTemp+" dp";

            //Smallest Width
            smallestWidthDPTemp=Math.min(screenWidthDP, screenHeightDP);
            resultSmallestWidthDP=String.format("%.2f", smallestWidthDPTemp);

            //DrawableFolder
            resultDrawableFolder="drawable-mdpi";
        }
        else if(diagonalPPIInt >= 160 && diagonalPPIInt < 240)
        {
            resultAndroidResBucket="HDPI"; //1.50

            //Diagonal size
            double diagonalDPSize=diagonalPPI/1.50;
            resultDiagonalDPSize=String.format("%.2f", diagonalDPSize);

            //Width * Height
            double screenWidthDP=screenWidthD/1.50;
            String screenWidthTemp=String.format("%.2f", screenWidthDP);

            double screenHeightDP=screenHeightD/1.50;
            String screenHeightTemp=String.format("%.2f", screenHeightDP);

            resultWidthHeightDP=screenWidthTemp+" dp * "+screenHeightTemp+" dp";

            //Smallest Width
            smallestWidthDPTemp=Math.min(screenWidthDP, screenHeightDP);
            resultSmallestWidthDP=String.format("%.2f", smallestWidthDPTemp);

            //DrawableFolder
            resultDrawableFolder="drawable-hdpi";
        }
        else if(diagonalPPIInt >= 240 && diagonalPPIInt < 320)
        {
            resultAndroidResBucket="XHDPI"; //2.00

            //Diagonal size
            double diagonalDPSize=diagonalPPI/2.00;
            resultDiagonalDPSize=String.format("%.2f", diagonalDPSize);

            //Width * Height
            double screenWidthDP=screenWidthD/2.00;
            String screenWidthTemp=String.format("%.2f", screenWidthDP);

            double screenHeightDP=screenHeightD/2.00;
            String screenHeightTemp=String.format("%.2f", screenHeightDP);

            resultWidthHeightDP=screenWidthTemp+" dp * "+screenHeightTemp+" dp";

            //Smallest Width
            smallestWidthDPTemp=Math.min(screenWidthDP, screenHeightDP);
            resultSmallestWidthDP=String.format("%.2f", smallestWidthDPTemp);

            //DrawableFolder
            resultDrawableFolder="drawable-xhdpi";
        }
        else if(diagonalPPIInt >= 320)
        {
            resultAndroidResBucket="XXHDPI";

            //Diagonal size
            double diagonalDPSize=diagonalPPI/2.50;
            resultDiagonalDPSize=String.format("%.2f", diagonalDPSize);

            //Width * Height
            double screenWidthDP=screenWidthD/2.50;
            String screenWidthTemp=String.format("%.2f", screenWidthDP);

            double screenHeightDP=screenHeightD/2.50;
            String screenHeightTemp=String.format("%.2f", screenHeightDP);

            resultWidthHeightDP=screenWidthTemp+" dp * "+screenHeightTemp+" dp";

            //Smallest Width
            smallestWidthDPTemp=Math.min(screenWidthDP, screenHeightDP);
            resultSmallestWidthDP=String.format("%.2f", smallestWidthDPTemp);

            //DrawableFolder
            resultDrawableFolder="drawable-xxhdpi";
        }


        if(smallestWidthDPTemp >= 320 && smallestWidthDPTemp < 480 )
        {
            //Mobile
            resultLayoutFolder="layout-sw320dp";

        }
        else if(smallestWidthDPTemp >= 480 && smallestWidthDPTemp < 600 )
        {
            //5" Mobile
            resultLayoutFolder="layout-sw480dp";
        }
        else if(smallestWidthDPTemp >= 600 && smallestWidthDPTemp < 720 )
        {
            //7" Tablet
            resultLayoutFolder="layout-sw600dp";
        }
        else if(smallestWidthDPTemp >= 720)
        {
            //10" Tablet
            resultLayoutFolder="layout-sw720dp";
        }


        TextView diagonalResTV=(TextView)findViewById(R.id.dip_result_stv_diagonalres);
        diagonalResTV.setText(resultDiagonalResolution+" PPI");

        TextView androidResTV=(TextView)findViewById(R.id.dip_result_stv_androidresbucket);
        androidResTV.setText(resultAndroidResBucket);

        //For Android Developer Units in DP

        TextView diagonalsizeTV=(TextView)findViewById(R.id.dip_result_stv_diagonalsize);
        diagonalsizeTV.setText(resultDiagonalDPSize+" dp");

        TextView widthHeightTV=(TextView)findViewById(R.id.dip_result_stv_widthheight);
        widthHeightTV.setText(resultWidthHeightDP);

        TextView smallestWidthTV=(TextView)findViewById(R.id.dip_result_stv_smallestwidth);
        smallestWidthTV.setText(resultSmallestWidthDP+" dp");

        TextView layoutFolderTV=(TextView)findViewById(R.id.dip_result_stv_layoutfolder);
        layoutFolderTV.setText(resultLayoutFolder);

        TextView drawableFolderTV=(TextView)findViewById(R.id.dip_result_stv_drawablefolder);
        drawableFolderTV.setText(resultDrawableFolder);

    }
4

0 回答 0