views:

3197

answers:

3

How to draw png images with specific size and position on the screen?

+3  A: 

Somewhere in an initialization function:

 Image myImage = Image.createImage("/myimage.png");

And in the paint function of your canvas:

 g.drawImage(myImage, posX, posY, Graphics.TOP|Graphics.LEFT);

(where g is the Graphics object you get from the paint function)

edit: fixed small error as pointed out in comments

Toad
completely right! will change it
Toad
+1  A: 

If you use net.rim.device.api.system.PNGEncodedImage or one of the other classes extended from net.rim.device.api.system.EncodedImage you can use the scaleImage32(int scaleX, int scaleY) method (available in OS 4.2 and latter) to scale the image to the size you want. Be aware though that scaleX and scaleY, though typed as int are actually net.rim.device.api.math.Fixed32 so to display the image at one half size:

EncodedImage halfSize = myImage.scaleImage32(Fixed32.toFP(2), Fixed32.toFP(2));

Or for an image twice original size:

EncodedImage twiceSize = myImage.scaleImage32(Fixed32.tenThouToFP(5000), Fixed32.tenThouToFP(5000));
Richard
only drawback is that the code uses rim only classes and is therefore not portable to any other java device.
Toad
True, but your answer only fulfills one of the two requirements specified by the OP.
Richard
+10  A: 
Max Gontar