Posts Tagged ‘java me’

Mobile Mtv Blog Uploader: yet another FlashLite and J2ME apps!

Friday, July 18th, 2008

As told some days ago, MtvMobile launched 2 phones with its initial offer as virtual mobile operator. And, for these 2 phones (a Nokia 5320 and a SE W760), 2 cool applications were lauched. I’ve already talked about Mtv OnDemand, a FlashLite and Java ME streaming video player that allows users to view selected MTV videos, and download related content (like ringtones and wallpapers).

Now it’s time to view the other one: it’s called Mtv.it Blog, and it’s a simple and effective mobile blog uploader.

Mtv.it blog Uploader screenshots

Using this app, users are able to upload contents (photos, videos, audio or simple text) from their mobile phone directly on their Mtv blog. As for Mtv OnDemand, there are both a  Java ME and a Flash Lite version of this app, quite identical in both their interface and functionalities.

As for the other one, I’ll post complete videos of this app as soon as I have it working again :)

Mtv On Demand: new FlashLite and Java ME apps released!

Tuesday, July 15th, 2008

Last friday (July 11th) was a big mobile day here in Italy: apart from the worldwide release of iPhone 3G (sold in Italy at something like 3 times the USA price…), MTV became virtual mobile operator!

MTV Mobile logo

For its initial launch (you can see some photos of the launch event on Flickr) MTV released 2 phones: a Nokia 5320 Xpress Music and a Sony Ericsson W760, both heavily customized with special covers and themes.

But, what really interests to us, is the launch of 2 embedded applications on these phones :)

The first one is called Mtv On Demand, and it’s a streaming video player that allows to view selected videos from MTV programs. The interface is pretty slick, and allows users to navigate video galleries using LEFT/RIGHT keys, while with UP/DOWN they’ll scroll videos within a single category.

MtvOnDemand screenshots

Interesing thing is that this app was built on each phone using different technologies: FlashLite for Nokia 5320 and Java ME for SE W760, so, reproducing the same look&feel and functionalities with 2 traditionally so-different technologies.

Since I currently have not an MTV SIM and phone, I cannot make a video of the app itself, but I’ll post it (and also post about the 2nd application ;) ) asap!

How to add sliding transitions between Canvas in J2ME

Monday, June 23rd, 2008

Today we’ll see a simple way to add sliding transition between Canvas in a Java ME application. Just take a look at it in the emulator page to see how it performs: Canvas sliding transitions in action!

j2me canvas sliding transition screenshot

The source code

FxBaseCanvas

First of all, we know that Canvas paint() method is protected, so, to access it from the class we’ll build, we must extend its visibility by extending Canvas class itself. We’ll do it by defining our FxBaseClass in this simple way:

import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Graphics;
 
public class FxBaseCanvas extends Canvas
{
	public void paint(Graphics g)
	{
	}
}

CanvasSlideFx

Now, we start building our CanvasSlideFx class, that will extend Canvas itself. We start defining some useful properties:

// time related properties
long startTime = 0;
long duration = 0;
 
// current state of the transition
boolean running = false;
 
// direction of sliding
int direction = LEFT;
 
// the previous and next Canvas instances
FxBaseCanvas fromCanvas = null;
FxBaseCanvas toCanvas = null;
 
// the current Display object
Display display = null;
 
// properties used to correctly place the 2 Canvas
int deltaX = 0;
int deltaY = 0;

Now, we define our class constructor, that will initialize coordinate properties according to the specified transition direction. The detailed argument list is:

  • The current Display object, that will be used to retrieve the current displayed Canvas, and to set the next one
  • The destination Canvas, that will extend the previously defined FxBaseCanvas class
  • The transition direction (one of Canvas.UP, RIGHT, DOWN or LEFT)
  • The transition duration, in milliseconds
public CanvasSlideFx(Display display, FxBaseCanvas toCanvas, int direction, long duration)
{
	this.display = display;
	this.fromCanvas = (FxBaseCanvas)display.getCurrent();
	this.toCanvas = toCanvas;
	this.direction = direction;
	this.duration = duration;
 
	switch(direction)
	{
	case UP:
		deltaY = - getHeight(); break;
	case RIGHT:
		deltaX = getWidth(); break;
	case DOWN:
		deltaY = getHeight(); break;
	case LEFT:
		deltaX = - getWidth(); break;
	}
}

Now, we define a startTransition() method, that will actually start the animation. To implement the animation itself, we let our class implement Runnable.

void startTransition()
{
	this.startTime = System.currentTimeMillis();
 
	running = true;
 
	new Thread(this).start();
}

And here’s the run() method implementation, that we must implement from Runnable interface. As long as the animation is running, we will repaint our animated Canvas, with a given interval of 50 milliseconds (but you can freely change this). When it finishes (so, running is false), we will exit the main loop and set the current Displayable to our toCanvas.

public void run()
{
	try
	{
		while(running)
		{
			repaint();
 
			synchronized(this)
			{
				wait(50L);
			}
		}
	}
	catch(Exception e)
	{
		e.printStackTrace();
	}
	display.setCurrent(toCanvas);
}

Finally, we have our paint() method, that will actually paint our transition. It will do this by painting both the source and the destination Canvas, translating them according to the transition direction, and to the elapsed time.

protected void paint(Graphics g)
{
	if(!running)
		startTransition();
 
	long diff = System.currentTimeMillis() - startTime;
 
	if(diff >= duration)
	{
		running = false;
 
		diff = duration;
	}
 
	int perc = (int)(100 * diff / duration);
 
	int dx = deltaX * perc / 100;
	int dy = deltaY * perc / 100;
 
	g.translate(dx, dy);
 
	fromCanvas.paint(g);
 
	g.translate(- deltaX, - deltaY);
 
	toCanvas.paint(g);
 
	g.translate(deltaX - dx, deltaY - dy);
}

How to use it?

Now we’ll see how to integrate the CanvasSlideFx within an existing code that already uses Canvas.
So, let’s assume that our application currently has, somewhere in its code, these lines:

MyCanvas firstCanvas = new MyCanvas();
 
Display.getDisplay(myMidlet).setCurrent(firstCanvas);
...
//and somewhere else
MyOtherCanvas secondCanvas = new MyOtherCanvas();
 
Display.getDisplay(myMidlet).setCurrent(secondCanvas);

where MyCanvas and MyOtherCanvas will likely extend Canvas.

Now here are the required steps to integrate transitions in our code:

  1. First of all, we must make MyCanvas and MyOtherCanvas extend FxBaseCanvas instead of directly Canvas. So, we’ll have:
    public class MyCanvas extend FxBaseCanvas ...
  2. Then, let’s say we want to animate the transition between firstCanvas and secondCanvas, we will remove the code:
    Display.getDisplay(myMidlet).setCurrent(secondCanvas);

    and replace it with:

    CanvasSlideFx fxCanvas = new CanvasSlideFx(
    	Display.getDisplay(myMidlet),
    	secondCanvas,
    	Canvas.LEFT,
    	500L
    );
     
    Display.getDisplay(myMidlet).setCurrent(fxCanvas);
  3. and we’ve done it! :)

Resources and download

You can download the code explained in this article with the following links: