Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
Regular Contributor
workingHard
Posts: 54
Registered: ‎04-24-2010
My Carrier: simulator
Accepted Solution

BB Storm: Screen Tilt Issue

Hi,

 

Aim is to display a series of bmp files on BB storm 9500 screen (to show as video). This works fine as long as i don't tilt the blackberry. Moment i tilt the device(while in for loop), it immediately displays last bmp file and stops. As can be seen from the code, i have implemented it as singelton to ensure only 1 instance of BMPBuff class.

public class BMPBuff extends MainScreen 
{
private Bitmap img;
private static BMPBuff instance;

private BMPBuff() {		
	BMPpThread pThread = new BMPpThread();
	pThread.start();	
}
	
public static BMPBuff GetInstance(){
synchronized (UiApplication.getUiApplication().getAppEventLock())       {
	if(instance == null){
		instance = new BMPBuff();
	}
	return instance;
  }
}

public class BMPpThread extends Thread 
{
	public void run() {
	int count;

	BMPPlayerBuffer bBuff = new BMPPlayerBuffer(); 
	for (count = 0; count < 300; count++) 
	{			
	 img = bBuff.BMPImageArray[count];
 	 UiApplication.getUiApplication().invokeAndWait(new Runnable() 
	  {
		public void run() 
		{
			invalidate(); //paint images in screen
		}
	  });
	}
    }
}

protected void paint(Graphics graphics) 
{
  graphics.drawBitmap(0, 0, img.getWidth(), img.getHeight(), img, 0, 0);	
} 
}

The way i instantiate this class is ,

BMPBuff bScreen = BMPBuff.GetInstance();
pushScreen(bScreen);

 

 I was expectiing it work fiine after implementing the Singelton. But does not. Any ideas/help?

 

Please use plain text.
Developer
Ted_Hopp
Posts: 1,305
Registered: ‎01-21-2009

Re: BB Storm: Screen Tilt Issue

You can turn off tilting while you are displaying the animation. Take a look at UiEngineInstance.setAcceptableDirections().




Solved? click "Accept as solution". Helpful? give kudos by clicking on the star.
Please use plain text.
Developer
rxan
Posts: 20
Registered: ‎02-15-2009

Re: BB Storm: Screen Tilt Issue

Looks to be a peculiar issue. Seems like the loop is either being breaked out of or deadlock is causing it not to return to the loop because it's blocked on invokeAndWait.

 

I alse noticed that you start the thread before you push the screen on the display stack. The invokeAndWait should theoretically make the thread wait until the screen actually enters the display stack. As in, the first iteration of the loop would block on invokeAndWait until the screen was placed on the stack and it can get a hold of the event lock.

 

By any chance have you tried invokeLater instead of invokeAndWait? I would have probably put code in the constructor like this. It would start the thread only after the screen got pushed onto the display stack:

 

 

BMPpThread pThread = new BMPpThread();
UiApplication.getUiApplication().invokeLater(new Runnable() {
	public void run() {
		pThread.start();
	}
});

 

 

Please use plain text.
Developer
rxan
Posts: 20
Registered: ‎02-15-2009

Re: BB Storm: Screen Tilt Issue

Oops! And you may have to mark the pThread as final in my code in order for it to work!

Please use plain text.
Regular Contributor
workingHard
Posts: 54
Registered: ‎04-24-2010
My Carrier: simulator

Re: BB Storm: Screen Tilt Issue

Hi,

 

Thanks for the response!

 

Tedd, turning off tilting is not an option for me.

 

 Rxan, i did try using invokeLater, but it did not help. I also belive that problem is somewhre in the loop part. Debugging does not help here. I get the similar behaviour, moment i select any menu item while executing the loop.

 

what makes the loop jump to last iteration is the question.

Thnx!

Please use plain text.
Developer
rxan
Posts: 20
Registered: ‎02-15-2009

Re: BB Storm: Screen Tilt Issue

Well the only other thing is that it's "throwing away" the rest of the animating events when you do another UI action.

 

I've checked some other threads for animation and people seem to be using timed animation through either Timer or Thread.sleeps. It's probably best to used timed animation anyway since newer faster processors would just zip through your animation.

 

Here's one link where someone uses sleep inbetween frames.

 

 

public class AnimatedImageField extends Field implements Runnable {

   private int currentFrame;
   private Bitmap[] animationFrames;

   public void run() {
     while(true) {
       currentFrame = (currentFrame + 1) % animationFrames.length;
       invalidate();
       Thread.sleep(100);
      }
    }

   protected void paint(Graphics g) {
      g.drawBitmap(0, 0, imageWidth, imageHeight, animationFrames[currentFrame], 0, 0);
    }
}

 

 

Please use plain text.
Regular Contributor
workingHard
Posts: 54
Registered: ‎04-24-2010
My Carrier: simulator

Re: BB Storm: Screen Tilt Issue

Adding Thread.sleep did solve the problem. But i did not understand the logic behind it. How does a 100 msec sleep between each frame display help device orientation?

 

Please use plain text.
Developer
rxan
Posts: 20
Registered: ‎02-15-2009

Re: BB Storm: Screen Tilt Issue

I'm glad to hear that it worked! Can you mark the issue as solved?

 

I don't think it had much to do with device orientation in particular. As you said the same thing happened when you pressed the menu key. I think other UI interactions would be likely to cause the same issue with your original code.

 

My guess is that it was blasting through the for loop causing you only to see the final frame. Did you see any animation with your original code? It could also be special logic in the event queue that throws away events when certain UI interactions occur in order to keep things responsive. To tell you the truth I don't know :smileytongue:

 

As for the attempted singleton fix, that's not going to have an effect. For example, Android would try to save and re-create your current activity, and a singleton may have helped in that case. But I believe BlackBerry leaves the process running and just performs a relayout on the current screen.

Please use plain text.