|
|
|
| |
| |
Matching application images to different screen sizes
Games, Players, Any application that uses images as background
| |
All devices and platforms
SUBJECTIVE - query the screen size of the device and resize images to the desired screen size.
1. Querying Screen size: [ example]
1.1 During application start-up use the Canvas class to determine Screen width and height (a dummy Canvas can be created for this purpose)
2. Resize images to the desired size according to the Screen size [ example]
2.1 Read image data into a graphics buffer
2.2 Create a new image buffer with the right size
2.3 Write image data while "streching" or "shrinking" to the new size
Example for querying screen size
...
/* This code can be called anywhere in the application.
* recommended to use during startApp
int width and int height are class members*/
Canvas dummyCanvas = new Canvas()
// get the dimensions of the screen:
width = dummyCanvas.getWidth ();
height = dummyCanvas.getHeight();
...
|
/**
* This methog resizes an image by resampling its pixels
* @param src The image to be resized
* @return The resized image
*/
private Image resizeImage(Image src) {
int srcWidth = src.getWidth();
int srcHeight = src.getHeight();
Image tmp = Image.createImage(screenWidth, srcHeight);
Graphics g = tmp.getGraphics();
int ratio = (srcWidth << 16) / screenWidth;
int pos = ratio/2;
//Horizontal Resize
for (int x = 0; x < screenWidth; x++) {
g.setClip(x, 0, 1, srcHeight);
g.drawImage(src, x - (pos >> 16), 0, Graphics.LEFT | Graphics.TOP);
pos += ratio;
}
Image resizedImage = Image.createImage(screenWidth, screenHeight);
g = resizedImage.getGraphics();
ratio = (srcHeight << 16) / screenHeight;
pos = ratio/2;
//Vertical resize
for (int y = 0; y < screenHeight; y++) {
g.setClip(0, y, screenWidth, 1);
g.drawImage(tmp, 0, y - (pos >> 16), Graphics.LEFT | Graphics.TOP);
pos += ratio;
}
return resizedImage;
}//resize image
|
| |
All devices
| |
Screen, Size, Sprites, Games, Background, image, image objects, MIDP 1.0, MIDP 2.0
