第一步(1-12行),讀取指定的圖片。
第二步(13-14行),講圖片的二進制數據轉換為EncodedImage對象,并且縮放為原來的1/2,setScale的參數是大于0的縮放因子n,圖片會被縮放為原來的1/n。
第三步(15-20行),將圖片插入到圖片域中,并將該圖片域添加到指定的MainScreen中。
1
try {
2
byte[] imgData = new byte[512];
3
InputStream in = Class.forName("BBUIApp").getResourceAsStream("1.jpg");
4
ByteArrayOutputStream bos = new ByteArrayOutputStream();
5
int count = -1;
6
while((count = in.read(imgData)) != -1)
7
{
8
bos.write(imgData, 0 , count);
9
}
10
byte[] img = bos.toByteArray();
11
bos.close();
12
in.close();
13
EncodedImage image = EncodedImage.createEncodedImage(img, 0, img.length);
14
image.setScale(2);
15
BitmapField f = new BitmapField();
16
f.setImage(image);
17
this.add(f);
18
} catch (Exception e) {
19
20
}

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

---------------------------------------------------------
專注移動開發
Android, Windows Mobile, iPhone, J2ME, BlackBerry, Symbian