JavaFX implements the 9patch function in android

Source: Internet
Author: User
Tags first row

9patch is a feature directly supported by android. Background images and games are a common technology. Background in css is very common. The principle is to cut a graph into nine blocks (9 patches), as shown in the following figure (lazy, directly referenced from the network ):

Repeat-x operations are performed on the top left, top right, bottom left, and bottom right of the newly generated image in Area 1, 3, 7, and 9, respectively, in area 2 and 8, perform repeat-y operations in Area 4 and area 6 and repeat-x-y operations in area 5. This solves the problem of adaptive image size. In android, the 9patch adds a pixel to the upper, lower, and lower sides of the original image, and the four black lines produced in the figure above determine the width and height of the 9 area, the black lines on the right and bottom are determined by the content, which is actually not mysterious.

JavaFX does not have this powerful function, but I understand the principle. Here I have implemented a javafx version to fully meet the daily development work:

The code is as follows: Copy code

/**
* Javafx implements 9 patches for android
 *
* @ Author zhou
 *
*/
Public final class NinePatch {
Private Image origin;
Private int top;
Private int right;
Private int bottom;
Private int left;

Public NinePatch (Image origin, int topRightBottomLeft ){
This (origin, topRightBottomLeft, topRightBottomLeft );
  }

Public NinePatch (Image origin, int topBottom, int leftRight ){
This (origin, topBottom, leftRight, topBottom, leftRight );
  }

Public NinePatch (Image origin, int top, int right, int bottom, int left ){
If (left + right> origin. getWidth ())
Throw new IllegalArgumentException ("left add right must less than origin width ");

If (top + bottom> origin. getHeight ())
Throw new IllegalArgumentException ("top add bottom must less than origin height ");

This. origin = origin;
This. top = top;
This. right = right;
This. bottom = bottom;
This. left = left;
  }

/**
* Generate an image of the specified width and height
   *
* @ Param scaledWidth
* @ Param scaledHeight
* @ Return
*/
Public Image generate (int scaledWidth, int scaledHeight ){
WritableImage result = new WritableImage (scaledWidth, scaledHeight );
PixelReader reader = origin. getPixelReader ();
PixelWriter writer = result. getPixelWriter ();
Int originHCenterWidth = (int) origin. getWidth ()-right-left;
Int originVCenterWidth = (int) origin. getHeight ()-top-bottom;

/* First row */
Writer. setPixels (0, 0, left, top, reader, 0, 0 );
For (int y = 0; y <top; y ++ ){
For (int x = left; x <scaledWidth-right; x ++ ){
Writer. setArgb (x, y, reader. getArgb (left + (x-left) % originHCenterWidth, y ));
      }
    }

Writer. setPixels (scaledWidth-right, 0, right, top, reader, (int) origin. getWidth ()-right, 0 );

/* Second row */
For (int y = top; y <scaledHeight-bottom; y ++ ){
For (int x = 0; x <left; x ++ ){
Writer. setArgb (x, y, reader. getArgb (x, top + (y-top) % originVCenterWidth ));
      }
    }

For (int y = top; y <scaledHeight-bottom; y ++ ){
For (int x = left; x <scaledWidth-right; x ++ ){
Writer. setArgb (x, y,
Reader. getArgb (left + (x-left) % originHCenterWidth, top + (y-top) % originVCenterWidth ));
      }
    }

For (int y = top; y <scaledHeight-bottom; y ++ ){
For (int x = scaledWidth-right; x <scaledWidth; x ++ ){
Writer. setArgb (x, y,
Reader. getArgb (int) origin. getWidth () + x-scaledWidth, top + (y-top) % originVCenterWidth ));
      }
    }

/* Third row */
Writer. setPixels (0, scaledHeight-bottom, left, bottom, reader, 0, (int) origin. getHeight ()-bottom );
For (int y = scaledHeight-bottom; y <scaledHeight; y ++ ){
For (int x = left; x <scaledWidth-right; x ++ ){
Writer. setArgb (x, y,
Reader. getArgb (left + (x-left) % originHCenterWidth, (int) origin. getHeight () + y-scaledHeight ));
      }
    }
Writer. setPixels (scaledWidth-right, scaledHeight-bottom, right, bottom, reader,
(Int) origin. getWidth ()-right, (int) origin. getHeight ()-bottom );

Return result;
  }

/**
   *
* @ Param gc
* @ Param x
* @ Param y
* @ Param scaledWidth
* @ Param scaledWidth
*/
Public void draw (GraphicsContext gc, int x, int y, int scaledWidth, int scaledHeight ){
Gc. drawImage (generate (scaledWidth, scaledHeight), x, y );
  }

}

The black pixel bar (original) method is not used here, because 9 areas of an image can be determined by knowing top, right, bottom, and left, the repeat operation is followed.

In the future, it is easy to see that the toprightbottomleft function is directly loaded from xxx.9.png.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.