de.enough.polish.ui
Class ScreenChangeAnimation

java.lang.Object
  extended by javax.microedition.lcdui.Displayable
      extended by javax.microedition.lcdui.Canvas
          extended by de.enough.polish.ui.Canvas
              extended by de.enough.polish.ui.ScreenChangeAnimation
All Implemented Interfaces:
Displayable, Runnable
Direct Known Subclasses:
BottomScreenChangeAnimation, BottomShutterScreenChangeAnimation, BwToColorScreenChangeAnimation, CageScreenChangeAnimation, CardScreenChangeAnimation, CubeScreenChangeAnimation, DancingPixelScreenChangeAnimation, DiagonalScreenChangeAnimation, DisplayScreenChangeAnimation, DominoScreenChangeAnimation, FadeOutFadeInScreenChangeAnimation, FadeScreenChangeAnimation, FlashScreenChangeAnimation, GradientScreenChangeAnimation, ItemScreenChangeAnimation, LeftScreenChangeAnimation, LeftShutterScreenChangeAnimation, NewWaveScreenChangeAnimation, PaperScreenChangeAnimation, ParticleScreenChangeAnimation, PixelScreenChangeAnimation, RainScreenChangeAnimation, RightScreenChangeAnimation, RightShutterScreenChangeAnimation, Rotate3DScreenChangeAnimation, RotateScreenChangeAnimation, RotatingScreenM3GScreenChangeAnimation, ScaleScreenChangeAnimation, SqueezeScreenChangeAnimation, TopScreenChangeAnimation, TopShutterScreenChangeAnimation, VerticalFlashScreenChangeAnimation, ZoomBothScreenChangeAnimation, ZoomInAndHideScreenChangeAnimation, ZoomOutScreenChangeAnimation

public abstract class ScreenChangeAnimation
extends Canvas
implements Runnable

Paints a transition of two screens for a nice effect.

Using a screen change animation is easy:
Use the screen-change-animation CSS attribute for specifying which animation you would like to have. You can also finetune some animations. Note that some animations have certain conditions like support of the MIDP 2.0 profile.

 screen-change-animation: left;
 left-screen-change-animation-speed: 5;
 

You can easily implement your own screen change animations by following these steps:

  1. Extend de.enough.polish.ui.ScreenChangeAnimation
  2. Implement the animate() method for doing the animation, use the fields lastCanvasImage and nextCanvasImage for your manipulation and consider the isForwardAnimation field.
  3. Implement the paintAnimation(Graphics) method.
  4. Override the show() method if you need to get parameters from the style.
  5. In case you want to manipulate the RGB data, you should set the useNextCanvasRgb and/or useLastCanvasRgb fields to true - you can then access the nextCanvasRgb and lastCanvasRgb fields for manupulating the data.

You can now use your animation by specifying the screen-change-animation CSS attribute accordingly:

 screen-change-animation: new com.company.ui.MyScreenChangeAnimation();
 

You can also ease the usage by registering your animation in ${polish.home}/custom-css-attributes:

 <attribute name="screen-change-animation">
                <mapping from="myanimation" to="com.company.ui.MyScreenChangeAnimation()" />
 </attribute>
 

Now your animation is easier to use:

 screen-change-animation: myanimation;
 

Copyright (c) Enough Software 2005 - 2009

 history
        27-May-2005 - rob creation
 

Author:
Robert Virkus, j2mepolish@enough.de
See Also:
onShow(Style, Display, int, int, Displayable, Displayable, boolean), animate()

Field Summary
protected  boolean abort
           
protected  Display display
           
protected  boolean isForwardAnimation
           
protected  Image lastCanvasImage
           
protected  int[] lastCanvasRgb
           
protected  int lastContentX
           
protected  int lastContentY
           
protected  Canvas nextCanvas
           
protected  Image nextCanvasImage
           
protected  int[] nextCanvasRgb
           
protected  int nextContentX
           
protected  int nextContentY
           
protected  Displayable nextDisplayable
           
protected  int screenHeight
           
protected  int screenWidth
           
protected  boolean supportsDifferentScreenSizes
           
protected  boolean useLastCanvasRgb
          set to true in subclasses for populating lastCanvasRgb
protected  boolean useNextCanvasRgb
          set to true in subclasses for populating nextCanvasRgb
 
Fields inherited from class de.enough.polish.ui.Canvas
_commands, DOWN, FIRE, GAME_A, GAME_B, GAME_C, GAME_D, KEY_NUM0, KEY_NUM1, KEY_NUM2, KEY_NUM3, KEY_NUM4, KEY_NUM5, KEY_NUM6, KEY_NUM7, KEY_NUM8, KEY_NUM9, KEY_POUND, KEY_STAR, LEFT, RIGHT, UP
 
Constructor Summary
ScreenChangeAnimation()
          Creates a new ScreenChangeAnimation.
 
Method Summary
 void abort()
          Aborts this screen change animation by setting the abort flag which is checked for in the run() method.
protected abstract  boolean animate()
          Animates this animation.
 void hideNotify()
          Notifies this animation that it will be hidden shortly.
 void keyPressed(int keyCode)
          Handles key pressed events.
 void keyReleased(int keyCode)
          Handles key released events.
 void keyRepeated(int keyCode)
          Handles key repeat events.
protected  void onShow(Style style, Display dsplay, int width, int height, Displayable lstDisplayable, Displayable nxtDisplayable, boolean isForward)
          Starts the animation.
 void paint(Graphics g)
          Renders the Canvas.
protected abstract  void paintAnimation(Graphics g)
          Paints the animation.
 void pointerDragged(int x, int y)
          Forwards pointer dragged events to the next screen.
 void pointerPressed(int x, int y)
          Forwards pointer pressed events to the next screen.
 void pointerReleased(int x, int y)
          Forwards pointer pressed events to the next screen.
 void run()
          Runs this animation - subclasses need to ensure to call this.display.callSerially( this ); at the end of the paint method.
protected  void setStyle(Style style)
          Sets the style for this animation.
 void showNotify()
          Notifies this animation that it will be shown shortly.
 void sizeChanged(int width, int height)
          Notifies this animation that the screen space has been changed.
protected  Image toImage(Displayable displayable, Screen nextScreen, Screen lastScreen, int width, int height)
           
protected  void updateNextScreen(Canvas next, Image nextImage, int[] rgb)
          Updates the image and possibly the RGB data of the next screen.
 
Methods inherited from class de.enough.polish.ui.Canvas
_hideNotify, _hideNotifyExternal, _showNotify, addCommand, getCommandListener, getCommands, getGameAction, getHeight, getKeyCode, getKeyName, getPolishTicker, getTitle, getWidth, handlePointerTouchDown, handlePointerTouchUp, hasPointerEvents, hasPointerMotionEvents, hasRepeatEvents, hideNotifyExternal, isDoubleBuffered, isShown, removeCommand, setCommandListener, setFullScreenMode, setTicker, setTitle
 
Methods inherited from class javax.microedition.lcdui.Canvas
repaint, repaint, serviceRepaints
 
Methods inherited from class javax.microedition.lcdui.Displayable
addCommand, getTicker, removeCommand, setCommandListener, setTicker
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

display

protected Display display

nextCanvas

protected Canvas nextCanvas

lastCanvasImage

protected Image lastCanvasImage

lastCanvasRgb

protected int[] lastCanvasRgb

useLastCanvasRgb

protected boolean useLastCanvasRgb
set to true in subclasses for populating lastCanvasRgb


nextCanvasImage

protected Image nextCanvasImage

nextCanvasRgb

protected int[] nextCanvasRgb

useNextCanvasRgb

protected boolean useNextCanvasRgb
set to true in subclasses for populating nextCanvasRgb


screenWidth

protected int screenWidth

screenHeight

protected int screenHeight

nextDisplayable

protected Displayable nextDisplayable

isForwardAnimation

protected boolean isForwardAnimation

nextContentX

protected int nextContentX

nextContentY

protected int nextContentY

lastContentX

protected int lastContentX

lastContentY

protected int lastContentY

supportsDifferentScreenSizes

protected boolean supportsDifferentScreenSizes

abort

protected boolean abort
Constructor Detail

ScreenChangeAnimation

public ScreenChangeAnimation()
Creates a new ScreenChangeAnimation. All subclasses need to implement the default constructor.

Method Detail

onShow

protected void onShow(Style style,
                      Display dsplay,
                      int width,
                      int height,
                      Displayable lstDisplayable,
                      Displayable nxtDisplayable,
                      boolean isForward)
Starts the animation. Please note that an animation can be re-used for several screens.

Parameters:
style - the associated style.
dsplay - the display, which is used for setting this animation
width - the screen's width
height - the screen's height
lstDisplayable - the screen that has been shown until now
nxtDisplayable - the next screen that should be displayed when this animation finishes (as a Displayable)
isForward - true when the animation should run in the normal direction/mode - false if it should run backwards

toImage

protected Image toImage(Displayable displayable,
                        Screen nextScreen,
                        Screen lastScreen,
                        int width,
                        int height)

setStyle

protected void setStyle(Style style)
Sets the style for this animation. Subclasses can override this for adapting to different design settings.

Parameters:
style - the style

animate

protected abstract boolean animate()
Animates this animation.

Returns:
true when the animation should continue, when false is returned the animation will be stopped and the next screen will be shown instead.

paintAnimation

protected abstract void paintAnimation(Graphics g)
Paints the animation.

Parameters:
g - the graphics context

paint

public final void paint(Graphics g)
Description copied from class: Canvas
Renders the Canvas. The application must implement this method in order to paint any graphics.

The Graphics object's clip region defines the area of the screen that is considered to be invalid. A correctly-written paint() routine must paint every pixel within this region. This is necessary because the implementation is not required to clear the region prior to calling paint() on it. Thus, failing to paint every pixel may result in a portion of the previous screen image remaining visible.

Applications must not assume that they know the underlying source of the paint() call and use this assumption to paint only a subset of the pixels within the clip region. The reason is that this particular paint() call may have resulted from multiple repaint() requests, some of which may have been generated from outside the application. An application that paints only what it thinks is necessary to be painted may display incorrectly if the screen contents had been invalidated by, for example, an incoming telephone call.

Operations on this graphics object after the paint() call returns are undefined. Thus, the application must not cache this Graphics object for later use or use by another thread. It must only be used within the scope of this method.

The implementation may postpone visible effects of graphics operations until the end of the paint method.

The contents of the Canvas are never saved if it is hidden and then is made visible again. Thus, shortly after showNotify() is called, paint() will always be called with a Graphics object whose clip region specifies the entire displayable area of the Canvas. Applications must not rely on any contents being preserved from a previous occasion when the Canvas was current. This call to paint() will not necessarily occur before any other key or pointer methods are called on the Canvas. Applications whose repaint recomputation is expensive may create an offscreen Image, paint into it, and then draw this image on the Canvas when paint() is called.

The application code must never call paint(); it is called only by the implementation.

The Graphics object passed to the paint() method has the following properties:

Specified by:
paint in class Canvas
Parameters:
g - - the Graphics object to be used for rendering the Canvas

pointerPressed

public void pointerPressed(int x,
                           int y)
Forwards pointer pressed events to the next screen.

Overrides:
pointerPressed in class Canvas
Parameters:
x - the horizontal coordinate of the clicked pixel
y - the vertical coordinate of the clicked pixel
See Also:
updateNextScreen(Canvas, Image, int[])

pointerReleased

public void pointerReleased(int x,
                            int y)
Forwards pointer pressed events to the next screen.

Overrides:
pointerReleased in class Canvas
Parameters:
x - the horizontal coordinate of the clicked pixel
y - the vertical coordinate of the clicked pixel
See Also:
updateNextScreen(Canvas, Image, int[])

pointerDragged

public void pointerDragged(int x,
                           int y)
Forwards pointer dragged events to the next screen.

Overrides:
pointerDragged in class Canvas
Parameters:
x - the horizontal coordinate of the clicked pixel
y - the vertical coordinate of the clicked pixel
See Also:
updateNextScreen(Canvas, Image, int[])

showNotify

public void showNotify()
Notifies this animation that it will be shown shortly. The default implementation switches into fullscreen mode

Overrides:
showNotify in class Canvas

hideNotify

public void hideNotify()
Notifies this animation that it will be hidden shortly. This is ignored by the default implementation.

Overrides:
hideNotify in class Canvas

sizeChanged

public void sizeChanged(int width,
                        int height)
Notifies this animation that the screen space has been changed. This is ignored by the default implementation.

Specified by:
sizeChanged in interface Displayable
Overrides:
sizeChanged in class Canvas
Parameters:
width - the width
height - the height
See Also:
in class Displayable

keyRepeated

public void keyRepeated(int keyCode)
Handles key repeat events. The implementation forwards this event to the next screen and then updates the nextCanvasImage field.

Overrides:
keyRepeated in class Canvas
Parameters:
keyCode - the code of the key
See Also:
nextCanvasImage, updateNextScreen(Canvas, Image, int[])

keyReleased

public void keyReleased(int keyCode)
Handles key released events. The implementation forwards this event to the next screen and then updates the nextCanvasImage field.

Overrides:
keyReleased in class Canvas
Parameters:
keyCode - the code of the key
See Also:
nextCanvasImage, updateNextScreen(Canvas, Image, int[])

keyPressed

public void keyPressed(int keyCode)
Handles key pressed events. The implementation forwards this event to the next screen and then updates the nextCanvasImage field.

Overrides:
keyPressed in class Canvas
Parameters:
keyCode - the code of the key
See Also:
nextCanvasImage, updateNextScreen(Canvas, Image, int[])

updateNextScreen

protected void updateNextScreen(Canvas next,
                                Image nextImage,
                                int[] rgb)
Updates the image and possibly the RGB data of the next screen.

Parameters:
next - the next screen
nextImage - the image to which the screen should be painted
rgb - the RGB data, can be null

abort

public void abort()
Aborts this screen change animation by setting the abort flag which is checked for in the run() method.


run

public void run()
Runs this animation - subclasses need to ensure to call this.display.callSerially( this ); at the end of the paint method.

Specified by:
run in interface Runnable
See Also:
Runnable.run()