|
/**
* 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 );
}
} |