1 package code
.kalter
.longflight
;
3 import java
.io
.IOException
;
4 import javax
.microedition
.lcdui
.Graphics
;
5 import javax
.microedition
.lcdui
.Image
;
8 * Реализация анимации: покадровая смена кадра из массива картинок за
9 * определённое время. Можно запустить как конечную анимацию, так и бесконечную
13 public class Animation
{
15 public static Animation
createAnimation(String path
, int width
,
16 int deltaTime
) throws IOException
{
17 final Loader loader
= Loader
.getInstance();
18 final Image image
= loader
.getImage(path
);
19 return createAnimation(image
, width
, deltaTime
);
22 public static Animation
createAnimation(String path
, int width
, int height
,
23 int deltaTime
) throws IOException
{
24 final Loader loader
= Loader
.getInstance();
25 final Image image
= loader
.getImage(path
);
26 return createAnimation(image
, width
, height
, deltaTime
);
29 public static Animation
createAnimation(Image source
, int width
,
31 final int height
= source
.getHeight();
32 return createAnimation(source
, width
, height
, deltaTime
);
35 public static Animation
createAnimation(Image source
, int width
, int height
,
37 final Image
[] still
= new Image
[source
.getWidth() / width
];
38 for (int i
= 0; i
< still
.length
; i
++) {
39 still
[i
] = Image
.createImage(source
, i
* width
, 0,
42 return createAnimation(still
, width
, height
, deltaTime
);
45 public static Animation
createAnimation(Image
[] still
, int deltaTime
) {
46 int width
= still
[0].getWidth();
47 int height
= still
[0].getHeight();
48 for (int i
= 1; i
< still
.length
; i
++) {
49 if (still
[i
].getWidth() > width
) {
50 width
= still
[i
].getWidth();
52 if (still
[i
].getHeight() > height
) {
53 height
= still
[i
].getHeight();
56 return createAnimation(still
, width
, height
, deltaTime
);
59 public static Animation
createAnimation(Image
[] still
,
60 int width
, int height
, int deltaTime
) {
61 return new Animation(still
, width
, height
, deltaTime
);
64 private final Image
[] still
; // кадры
65 private final int width
;
66 private final int height
;
67 //=======================
68 private long lastTime
; // необходимо для засекания времени
69 private int deltaTime
; // скорость смены кадров
70 //=======================
72 private boolean animationLoop
;
74 private Animation(Image
[] still
, int width
, int height
, int deltaTime
) {
78 this.deltaTime
= deltaTime
;
79 lastTime
= System
.currentTimeMillis();
84 public Image
getStill(int index
) {
86 throw new IllegalArgumentException("index < 0");
88 if (index
>= still
.length
) {
89 throw new ArrayIndexOutOfBoundsException("i >= length");
94 public int getWidth() {
98 public int getHeight() {
102 public int getDeltaTime() {
106 public void setDeltaTime(int deltaTime
) {
108 throw new IllegalArgumentException("delta time < 0");
110 this.deltaTime
= deltaTime
;
113 public int getPosition() {
117 // использование нежелательно, но безопасно
118 public void setPosition(int position
) {
120 throw new IllegalArgumentException("index < 0");
122 if (position
>= still
.length
) {
123 throw new ArrayIndexOutOfBoundsException("i >= length");
125 this.position
= position
;
128 public void start() {
129 animationLoop
= true;
134 animationLoop
= false;
137 public boolean getState() {
138 return animationLoop
;
141 public void nextToInfinity() {
142 final long time
= System
.currentTimeMillis() - lastTime
;
143 if (time
> deltaTime
) {
144 lastTime
= System
.currentTimeMillis();
145 if (++position
>= still
.length
) {
152 final long time
= System
.currentTimeMillis() - lastTime
;
153 if (getState() && time
> deltaTime
) {
154 lastTime
= System
.currentTimeMillis();
155 if (++position
>= still
.length
) {
161 public void paint(Graphics graphics
, int x
, int y
) {
163 graphics
.drawImage(still
[position
], x
, y
, 0);