Better quality distribution of images from resources based on resolution / density

I am struggling with a rather trivial task in an Android domain with multiple screens.

What am i trying to achieve

A layout that matches the width of the screen, with a background with nine patches that changes (only horizontally, since there is always enough vertical space). Here is a dummy image:horizontally stretchable nine patch image with two regions on left and right ends

My goal is to display the image with the highest possible resolution, depending on the screen resolution, using a set of different sizes, for example. 320x45, 480x67, 600x87, 720x101, without any scaling. I hope for a non-software solution.

An example with the above image sizes:

  • 3.7 "Nexus One (480 x 800) - A 480x67 image will look best.
  • 4.7 "Galaxy Nexus (720 x 1280) - The image is 720x101.
  • 4.7 "Nexus 4 (768 x 1280) - again the image is 720x101, stretching to the full width of 768 px and becoming 768x101.

Question

The whole distribution of Android resources revolves around dp(independent of pixel density) when in fact I want to display an image based on the actual available pixels.

If I selected a 480x67 image for res / drawable-mdpi and 600x87 for res / drawable-hdpi, then the image will display correctly on the 5.4 "480x800 display , i.e. the mdpi display. However, 4" 480x800 displays meet the hdpi criteria and the system will assign a 600x87 image, t fits the screen .

smallestWidth, -, . , 3,7 "480 x 800 (hdpi) drawable-sw320dp, drawable-sw480dp.

, ? ?

!

+4
3

, .

​​ :

  • drawable-normal-hdpi - A normal 320dp. hdpi 1,5X dp . , px normal hdpi 480 . 480 .
  • drawable-normal-xhdpi - 320dp, 2X. , 640 .
  • drawable-xlarge-mdpi - 720dp. mdpi 1X, 720 .

, , :

  • Nexus one - normal hdpi. : 480 . .
  • Galaxy nexus - normal xhdpi. 720px, 640px , , , .
  • Nexus 4 Gnex.
  • Nexus 10.1 (1280X800) - xlarge mdpi. 800px, 720px. , .

: 5-10%. : .

, ( , , .. ). , Android, , - .


smallestWidth: . hdpi 1,5. , hppi 480px 320dp. drawable-sw320dp , . , smallestWidth dpi. , , . 5%. , .

+3

, , . 2 : , , .

1 -

    DisplayMetrics displaymetrics = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
    int screenWidth = displaymetrics.widthPixels; 
    // this will determine "scale ratio" so using which image height and width won't matter 
    int imageOriginalHeight = 101; // your original image height        
    int imageOriginalWidth = 720; // your original image width
    int imageScaleHeight = (screenWidth*imageOriginalHeight) / imageOriginalWidth;

    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(screenWidth, imageScaleHeight);
    imageView.setLayoutParams(params);
    imageView.setImageResource(R.drawable.file);

2 -

ScaleImageView, .

:

, , ( ) . ImageChangeListener, change (boolean isEmpty) ImageView

.

ScaleImageView.java .

xml ScaleImageView, , ImageView (, , , /)

    <com.project.customview.ScaleImageView
        android:id="@+id/scaleImageView"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:adjustViewBounds="true"
        android:scaleType="fitCenter"
        android:src="@drawable/file" />

, , ImageView:

imageView = (ScaleImageView)findViewById(R.id.scaleImageView);
imageView.setImageResource(R.drawable.file);
+1

android hdpi, mdpi, xdpi ..

, .

, ,

, .

QVGA 240x320 (120dpi):

drawable-small-ldpi (240x320)
drawable-small-land-ldpi (320x240)

WVGA400 240x400 (x432) (120dpi):

drawable-ldpi (240 x 400) drawable-land-ldpi (400 x 240)

HVGA 320x480 (160dpi):

drawable-mdpi (320 x 480) drawable-land-mdpi (480 x 320)

HVGA 320x480 (160dpi):

drawable-large-mdpi (320 x 480) drawable-large-land-mdpi (480 x 320)

Galaxy (240 dpi):

drawable-large (600 x 1024) drawable-large-land (1024 x 600)

WVGA800 480x800 (x854) (240 dpi):

drawable-hdpi (480 x 800) drawable-land-hdpi (800 x 480)

Xoom ( , 1280x800 ) (160 dpi):

drawable-xlarge (800 x 1280) drawable-xlarge-land (1280 x 800)

-1

Source: https://habr.com/ru/post/1539699/


All Articles