How do you use Nine-patch?



  • Can you explain what this format is for? I understand it's made for optimization. png I've got pictures in the masturbation. ♪ ♪

    Then two questions,

    • why all pictures are not used in default immediately in this format If it's so convenient?
    • Why not use it? svg the format, then?

    It's just that I read about this format today and I'm trying to figure out where I can use it in my project and I can't think of it... I've got all the pictures that look good, and if you need anything, I'll use it. svg

    I thought it might be an outdated format? Since those articles that found everything from 2011-2013... Maybe it's not used anymore.

    Help me understand)



  • Nine-patch (9-patch) is a special format which allows for the identification of the areas of vegetation ( left and upper edge) and the placement of content on the image (right and lower edge). This format is not used to optimize, on the contrary, it requires more calculations and the use of it without usability (not directly applicable) makes no sense.

    scrn1

    This format is used mainly for graphically complex beckgruntss where the total size of the view may change or its relative dimensions (proportions) may vary. Like buttons.

    Let's say we need to put a button like this:

    scrn2

    It's easier to paint in a vegetation than a vector or primitive form, but it's a problem that when relative size changes (e.g., 5 times larger than width), it's natural for a button, because it can be small, and it can be on the whole screen width and narrowly, the image will be distorted and so horrible:

    scrn3

    Of course, it's not appropriate, and it's 9-patch for help, asking this flashing area background where there's no image that can be distorted when it's stretched, it's a contour line (on the green spot) and a content area so that the text in the button is not overlapping and is not contoured with the contour of the image, only the viral area:

    scrn4

    Now, our button will always have a geometric right proportion, regardless of the proportions and size of the view, and the text inside it will never go beyond the area assigned to it by going to the line:

    scrn5

    It is either impossible or difficult to do so by any other means, including the SVG format, which is only proportional.

    This format is not outdated and is used, if necessary, and, for example, in the modern Google- AppCompat Library. Okay, the flashlight. Spinner (white triangle down to right) performed 9-patch - file https://github.com/android/platform_frameworks_support/blob/master/v7/appcompat/res/drawable-xxxhdpi/abc_spinner_mtrl_am_alpha.9.png or tab indicator - file https://github.com/android/platform_frameworks_support/blob/master/v7/appcompat/res/drawable-xxxhdpi/abc_tab_indicator_mtrl_alpha.9.png - make such, externally simple backgrounds Shape The use of 9-patch would be more difficult and justified. It is also necessary to draw attention to their size - the files are very small (both a few picels) and contain only the necessary images, all of which are added to the areas of the planting. This allows for a slight reduction in the volume of the APK resulting.

    It should be noted that modern design requirements are in favour of simplifying the appearance of visions - simple shapes, primitive contours and, at present, the same problem is usually addressed by graphic primitives (classes) Shape or equivalent in xml-calculation) - they are excellent, including distortion of proportions, easier for the processor and a preference for the 9-patch format should be given only when by means of Shape It's impossible to draw the desired view or difficult.

    PS: The pictures are https://habrahabr.ru/post/113623/ there's a little more open.




Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2