Skip to content Skip to sidebar Skip to footer

Draw 9 Patch Tool in Android Studio

Step 1, Since you are not familiar yet, prepare a xxxhdpi png expectant image to play more or less first.

Step 2, That image coveted expandable field mustiness shorter (by removing redundant/recurrent color part) Eastern Samoa viable since 9-patch no such thing "reduce", only "expand" the image.

I personally use the ImageMagick control personal credit line tool to convert information technology, e.g.:

          convert -trim 'my_image.png' png32:my_image_trim.png #trim extra transparent encircled image rm lala*; convert my_image_trim.png -prune 310  +repage  +adjoin  png32:lala%02d.png #cut redundant/repeat center part exchange +tack on lala02.png lala05.png png32:out_right.png #append left/right images side by side convert -resize 144x144\! out_right.png png32:my_image.png #resize to the sought after dpi                  

There is one pitfall when convert: I must prefix with png32, or else will get black 9-patch image, see this thread.

Whole tone 3, Copy image to Android Studio drawable, then right-click and choose menu item "Create 9-Spell file...". A .9.png new image will be generated with the homophonic image identify. Straightaway can just delete the original image. Painstaking when playing around with refactor to rename for backup since information technology will rename the XML image Idaho too, and make you wonder wherefore the 9-patch image non working since XML still referring not-9-patch image.

Step 4, Left and top 2 black lines form an expandable angular area, while right and bottom 2 black lines form a textual matter rectangular area.

Your image size will grow on your asserted expandable area as text growing. While the text area means text only allow in that area.

You father't take to pull back the black dot/lines from scratch for simple usage, 4 smutty lines already existed hind end the top, left, bottom, rightfield on that image if your 9-Patch image generated aside Android Studio. "Zoom in" to large if you can't go through to drag that lines.

The original position of 2 black lines, vertical and horizontal, both announce an expandable domain:

  • The original set down of left vertical line is 'tween upper left to bottom left
  • The original position of top flat line is between upper left to top right.

The original position of 2 black lines, vertical and crosswise, some declare a text country:

  • The original position of right vertical line is between upper right to bottom right.
  • The original position of bottom horizontal pipeline is between bottom left to bottom ethical.

Above is the original situation of African-American lines before you start to drag to narrow down the length, to adjust the start position and end post of that lines.

Both expandable and text edition areas can be different depends on your necessarily. But usually expandable surface area should isometrical OR little than text area, a classic example will be chat bubble image:

enter image description here

The image preceding has equal upmost and rear fatal lines breadth but in good order black line is higher than left line, which also means that school tex consistently abide up to half of buns curve in either nominal size or expanded size. And IT only expand in body of text edition area.

Now you will know the two benefits of 9-Patch: the bottom and right descent together form a text domain which perfectly ensures text never overspill to unlikely of visualise arch ! And also defined which portion of text area causative to expand the image A text growing, while celebrate the curve without scaling.

Hover the line will fit to see the x,y positions in pixels, IT help to measures both lines are balanced or less in location.

You should remember the attitude of pipeline when dragging, since left line can drag out to right pull, and the right billet can drag to unexpended side, and you Crataegus laevigata perplex lost of which line is expandable line and which line is text transmission line.

In that location are 2 important checkboxes you should tick, i.e. "Show patches" and "Show bad patches" checkboxes.

In "Bear witness bad patches" checkbox, if you drawing the expandable area only covering the curved line of your image instead of a straight line, then it leave mark that domain as red to warn you. You can narrow your line to dismiss that red warning, or retributory ignore it. Keep in mind that, the bloody admonitory may misleading which may actually comprise caused by the opposite line, in this showcase, you need to narrow the other line to dismiss that red warning.

Mistreat 5, In xml, you can pertain to that image as TextView background like below, use wrap_content to make it expandable:

          <LinearLayout     android:layout_width="wrap_content"     android:layout_height="wrap_content"     android:background="@drawable/your_9_patch_image_name_excluded_.9"     android:orientation="vertical">     <TextView         android:layout_width="wrap_content"         android:layout_height="wrap_content"         />     <2nd TextView /> </LinearLayout>                  

When first time playing around with it, ensures no padding on textview or fancy raise layout, operating theatre other you may wonder why not working as expected.

Push scop example:

                      <Button         android:id="@+id/btn2"         mechanical man:layout_width="wrap_content"         android:layout_height="40dp"         android:background="@drawable/your_9_patch_image_name_excluded_.9"         mechanical man:layout_gravity="center"         />                  

Draw 9 Patch Tool in Android Studio

Source: https://stackoverflow.com/questions/17124719/creating-using-9-patch-images-in-android

Postar um comentário for "Draw 9 Patch Tool in Android Studio"