NinePatchChunk


Source link: https://github.com/Anatolii/NinePatchChunk

NinePatchChunk

This is a simple Android library which allows you to create a chunk for NinePatchDrawable at runtime. So you are able to load 9.png images, for example, from assets of your application or from other source.
The solution based on this implementation from Android Open Source Project.
Additional information about 9-patch chunk structure you can find here.


Usage

The main class of the library is NinePatchChunk.
Global constants:
public static final int NO_COLOR = 0x00000001; - The 9 patch segment is not a solid color.
public static final int TRANSPARENT_COLOR = 0x00000000; - The 9 patch segment is completely transparent.

Firstly let's go through the fields of the class object you would use.

  • boolean wasSerialized - The indicator if this chunk was serialized or not. I didn't saw images without this variable equals true. If you will - feel free to contact me. This value is true by default.
  • ArrayList<Div> xDivs - An array with stretchable areas goes on X axes. The 0 start pixel is pixel of actual bitmap without 9-patch 1-pixel border. The Div class will be described bellow. By default this value is null
  • ArrayList<Div> yDivs - The same as xDivs but for the Y axes.
  • Rect padding - The paddings from the ends of bitmap which you set while creating regular 9-patch image by defining right and bottom borders in Android 9-patch editor.
    Let's put some example. Let's say that you have 7x7 9-patch image. and you want the content will be exactly in the middle of the image.
    So your final image size without 9-patch specific borders would be 5x5 and if you want the contend will be placed inside 3-d pixel area you should define paddings as Rect(2,2,2,2)
  • int colors[] - The colors of arrays which represents areas crossing X and Y stretchable and unstretchable areas of 9-patch bitmap. For more information look on struct Res_png_9patch commentaries here.
    Long story short, if you have the same color in that area - the color for thar area will be that color. If color of that area is Transparent (has alpha 0) - the color should be Color.TRANSPARENT. If the area is filled with different colors - the array should content NO_COLOR value. See static methods description to see how easily properly create this array.

The class has only one object-dependent method:

  • byte[] toBytes() - serializes your current chunk instance to a byte array so you can use it creating NinePatchDrawable object.

Also NinePatchChunk class has some static methods. Let's go through them.

  • NinePatchChunk parse(byte[] data) - Parses any serialized chunk into the object you can use or edit.
  • NinePatchDrawable create9PatchDrawable(Resources resources, Bitmap bitmap, String srcName) - Creates NinePatchDrawable right from raw Bitmap object. So resulting drawable will have width and height 2 pixels less if it is raw, not compiled 9-patch resource.
  • NinePatchChunk createEmptyChunk() - Jut creates empty chunk object to you have ability to change it.
  • int[] createColorsArray(NinePatchChunk chunk, int bitmapWidth, int bitmapHeight) - Creates a proper color array sized according to your X and Y divs.
  • void createColorsArrayAndSet(NinePatchChunk chunk, int bitmapWidth, int bitmapHeight) - The same as previous but this method also sets created array to your chunk if it is not null.
  • boolean isRawNinePatchBitmap(Bitmap bitmap) - This method checks if the bitmap is 9-patch image.

And finally, Div object description. This object is used to define stretchable areas:

  • int start - the starting pixel of stretchable area. The count starts from 0.
  • int stop - the ending pixel of stretchable area. This pixel is right after the latest Color.BLACK pixel of the stretchable area.

Building from sources

NinePatchChunk is a library which delivers as open source project.
As build tool it uses Gradle build system.

In order to build the library you need this environment installed:

  • Java JDK 6
  • Android SDK with build tools version 18.1.1

For success build this environment variables should be defined in your system:

  • JAVA_HOME - will point to your JDK location
  • ANDROID_HOME - will point to Android SDK location

For the next step go to command line and then go to location where NinePatchChunk library sources are stored
cd /NinePatchChunk (where gradlew script file stored)

From this point in order to get output files you just need to run this command from command line:
gradlew assemble to build release and debug aar files
gradlew assembleDebug to build debug aar file only
gradlew assembleRelease to build release aar file only

That's it. So now you can find the output files inside this folder:
NinePatchChunk\NinePatchChunkLib\build\libs


This is it. The using is simple as that. See the java doc to learn more.
If you have some additional question - feel free to contact me.

Thanks!

Resources

Its a Date Range Picker View with animation and improved UI.

Show case card view.

Reactive implementation of Firebase Cloud Messaging.

Android library to provide chooser for files in external storage.

Publish multiple modules of a project, be it an android library, one containing NDK, a pure java one or even an annotation processor. Whatever you want to publish in Bintray, do it in a mega easy and customizable way.

This plugin also supports dependencies resolution between local modules (since the POM cant contain local references for logical reasons) in a fully transparent way.

This simple library is just a wrap on newly updated Location APIs.

Topics


2D Engines   3D Engines   9-Patch   Action Bars   Activities   ADB   Advertisements   Analytics   Animations   ANR   AOP   API   APK   APT   Architecture   Audio   Autocomplete   Background Processing   Backward Compatibility   Badges   Bar Codes   Benchmarking   Bitmaps   Bluetooth   Blur Effects   Bread Crumbs   BRMS   Browser Extensions   Build Systems   Bundles   Buttons   Caching   Camera   Canvas   Cards   Carousels   Changelog   Checkboxes   Cloud Storages   Color Analysis   Color Pickers   Colors   Comet/Push   Compass Sensors   Conferences   Content Providers   Continuous Integration   Crash Reports   Credit Cards   Credits   CSV   Curl/Flip   Data Binding   Data Generators   Data Structures   Database   Database Browsers   Date &   Debugging   Decompilers   Deep Links   Dependency Injections   Design   Design Patterns   Dex   Dialogs   Distributed Computing   Distribution Platforms   Download Managers   Drawables   Emoji   Emulators   EPUB   Equalizers &   Event Buses   Exception Handling   Face Recognition   Feedback &   File System   File/Directory   Fingerprint   Floating Action   Fonts   Forms   Fragments   FRP   FSM   Functional Programming   Gamepads   Games   Geocaching   Gestures   GIF   Glow Pad   Gradle Plugins   Graphics   Grid Views   Highlighting   HTML   HTTP Mocking   Icons   IDE   IDE Plugins   Image Croppers   Image Loaders   Image Pickers   Image Processing   Image Views   Instrumentation   Intents   Job Schedulers   JSON   Keyboard   Kotlin   Layouts   Library Demos   List View   List Views   Localization   Location   Lock Patterns   Logcat   Logging   Mails   Maps   Markdown   Mathematics   Maven Plugins   MBaaS   Media   Menus   Messaging   MIME   Mobile Web   Native Image   Navigation   NDK   Networking   NFC   NoSQL   Number Pickers   OAuth   Object Mocking   OCR Engines   OpenGL   ORM   Other Pickers   Parallax List   Parcelables   Particle Systems   Password Inputs   PDF   Permissions   Physics Engines   Platforms   Plugin Frameworks   Preferences   Progress Indicators   ProGuard   Properties   Protocol Buffer   Pull To   Purchases   Push/Pull   QR Codes   Quick Return   Radio Buttons   Range Bars   Ratings   Recycler Views   Resources   REST   Ripple Effects   RSS   Screenshots   Scripting   Scroll Views   SDK   Search Inputs   Security   Sensors   Services   Showcase Views   Signatures   Sliding Panels   Snackbars   SOAP   Social Networks   Spannable   Spinners   Splash Screens   SSH   Static Analysis   Status Bars   Styling   SVG   System   Tags   Task Managers   TDD &   Template Engines   Testing   Testing Tools   Text Formatting   Text Views   Text Watchers   Text-to   Toasts   Toolkits For   Tools   Tooltips   Trainings   TV   Twitter   Updaters   USB   User Stories   Utils   Validation   Video   View Adapters   View Pagers   Views   Watch Face   Wearable Data   Wearables   Weather   Web Tools   Web Views   WebRTC   WebSockets   Wheel Widgets   Wi-Fi   Widgets   Windows   Wizards   XML   XMPP   YAML   ZIP Codes