androidhdpi

Resizing image for XXHDPI


I have an image that is being displayed perfectly for MDPI. Its resolution is 191 x 255.

Then I resize it to XXDPI, using 1:3 proportion, it gets to 573 x 765. Still, when the emulator displays it, the quality is not as good as the MDPI one. It gets clearly poor.

For both cases, I am using it Wrap Content

If I display both images on a image editor, they have perfect quality. Why does that happen? Am I missing something here?


Solution

  • DisplayMetrics metrics = new DisplayMetrics();
    getActivity().getWindowManager().getDefaultDisplay().getRealMetrics(metrics);
    int widthPixels = metrics.widthPixels;
    int heightPixels = metrics.heightPixels;
    float widthDpi = metrics.xdpi;
    float heightDpi = metrics.ydpi;
    float widthInches = widthPixels / widthDpi;
    float heightInches = heightPixels / heightDpi;
    double diagonalInches = Math.sqrt((widthInches * widthInches) + (heightInches * heightInches)); // this code returns the actual screen size (eg. 7,8,9,10 inches in double format ex: 7.932189832)
    diagonalInches = (double) Math.round(diagonalInches * 10) / 10; // round up to first decimal places
    
    ArrayList<Integer> imageId = new ArrayList<>();
    imageId.clear();
    
    for (Integer i : imageNum){ // uses loop since im using TransitionDrawable to change images in single view
        String stringID;
        if (isPortrait) {
            stringID = "featured_img_" + i.toString() + "_port"; // portrait images
        } else {
            if (diagonalInches >= 7 && diagonalInches <=8){
                stringID = "featured_img_" + i.toString() + "_land_b"; // landscape images this is the drawable filename
            } else {
                stringID = "featured_img_" + i.toString() + "_land_a"; //landscape images this is the drawable filename
            }
        }
        int drawableId = getResId(stringID);
        imageId.add(drawableId); // adding it to arraylist
    }