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
Contributor
arjuna
Posts: 48
Registered: ‎04-07-2011
My Device: 9550
My Carrier: Desktop

Bitmap moving in Blackberry Screen by using Touch Event

Hi All,

 

 

                   I wrote a code to move a bitmap on screen my code was executed bitmap got moved , but the bitmap moving is too slow. I want your suggestions or any block of code to move a bitmap speedly. 

 

Here is My Code : 

 

protected boolean touchEvent(TouchEvent message) {
int x1 = message.getX(1);
int y1 = message.getY(1);
if (message.getEvent() == TouchEvent.MOVE) {
if (x1 != -1 && y1 != -1) {
leftUp = new XYPoint(x1, y1);
 }
}

invalidate();

return  super.touchEvent(message);

}

 

protected void paint(Graphics graphics) {
super.paint(graphics);

graphics.drawBitmap(leftUp.x,leftUp.y,image.getWidth(),image.getHeight(),0,0);

}

 

By using this code my app gets too slow in Blackberry 7 OS devices.

 

Please give me your replies if you know how to fix this.

 

Thanks & Regards ,

Nagarjuna Metla.

arjuna
Please use plain text.
Developer
peter_strange
Posts: 19,595
Registered: ‎07-14-2008
My Device: Not Specified

Re: Bitmap moving in Blackberry Screen by using Touch Event

A couple of things to try

 

a) just invalidate() the area on the screen that will be changed.  This will save the BB having to recreate the whole screen.  Remember that the user can only see what is on the screen. 

 

b) Don't invalidate every time.  If the image has only moved 1 pixel, no-one is going to notice this, but it will take a full paint recycle to paint.  Instead just invalidate when you have moved a visible distance.  This actual distance will depend on the screen resolution, so you will need to calculate a specific number of pixels based on the screen resolution.  By resolution I mean dots per inch, or pixels per meter, which is the BB measurement you can get from

Display.getVerticalResolution(). 

 

Both these may need work to get looking good, but start with the basic implementation and see if it is workable.  Then you can fine tune. 

 

In addition,

 

1) Don't invalidate() every time you go through touchEvent.....

2) You could investigate how difficult the painting used in super.paint() actually is by profiling that processing.  If you have loads of fiddly fields with complicated paint processing, then you should look to reduce that.  Try this same processing with just a plain background to make sure that the super.paint() is not causing you problems

3) Avoid Display methods (getHeight, etc), if you can, I have been told that these are expensive.  Override sublayout or layout and save the current values - if the user tilts the device, sublayout and layout will be called again.  

Please use plain text.
Contributor
arjuna
Posts: 48
Registered: ‎04-07-2011
My Device: 9550
My Carrier: Desktop

Re: Bitmap moving in Blackberry Screen by using Touch Event

 Hi ,

 

          Thanks for your Response. Can you send me the code for this.

 

 

 

Thanks & Regards,

Nagarjuna Metla.

arjuna
Please use plain text.
Developer
peter_strange
Posts: 19,595
Registered: ‎07-14-2008
My Device: Not Specified

Re: Bitmap moving in Blackberry Screen by using Touch Event

Not sure what code you actually want.  But regardless it would be better for you if you tried the things I suggested with your own code.

 

You might think invalidate() is tricky, but look at the API for this and you will see that there are two forms, one of which effectively takes a rectangle as parameters and it is this method that you want to use,  You will need to invalidate the area that your rectangle was and where it now is.  Given that the movement is small, I suggest you would be better making one invalidate() call, with the outer extents of the movement. 

Please use plain text.
Contributor
arjuna
Posts: 48
Registered: ‎04-07-2011
My Device: 9550
My Carrier: Desktop

Re: Bitmap moving in Blackberry Screen by using Touch Event

 

Hi ,

 

 

                 give me your sample code that moves the bitmap fastly according to touchEvent.I will integrate that into my code.

 

 

Thanks&Regards,

Nagarjuna Metla. 

arjuna
Please use plain text.
Developer
peter_strange
Posts: 19,595
Registered: ‎07-14-2008
My Device: Not Specified

Re: Bitmap moving in Blackberry Screen by using Touch Event

Is it not possible for you to try to implemet the options I have suggested?

Please use plain text.
Contributor
arjuna
Posts: 48
Registered: ‎04-07-2011
My Device: 9550
My Carrier: Desktop

Re: Bitmap moving in Blackberry Screen by using Touch Event

 

Ya.

arjuna
Please use plain text.
Developer
peter_strange
Posts: 19,595
Registered: ‎07-14-2008
My Device: Not Specified

Re: Bitmap moving in Blackberry Screen by using Touch Event

Why not?  Did I not explain them well enough?  What don't you understand?

Please use plain text.
Contributor
arjuna
Posts: 48
Registered: ‎04-07-2011
My Device: 9550
My Carrier: Desktop

Re: Bitmap moving in Blackberry Screen by using Touch Event

 

 

 

       how i draw the bitmap without using invalidate method with respect to touchEvent

 

 

arjuna
Please use plain text.
Developer
peter_strange
Posts: 19,595
Registered: ‎07-14-2008
My Device: Not Specified

Re: Bitmap moving in Blackberry Screen by using Touch Event

Let us start with my first suggestion:

 

"a) just invalidate() the area on the screen that will be changed.  This will save the BB having to recreate the whole screen.  Remember that the user can only see what is on the screen."

 

This is using the following Field method:

 

protected void invalidate(int x,
                          int y,
                          int width,
                          int height)

 

Look at the API for more documentation.

 

Now you only have to repaint the area of the screen that had the rectangle and the area of the screen that currently has the rectangle.  Say the rectangle was width of 11 and height of 15 and was at position x = 2, y = 4.  Now you want tot move it to x = 4 and y = 5,  So the rectangle you need to repaint is

x = 2, y = 5, width = 13 and height = 16. 

I'm confident you can do the mathematics to prove these numbers.  You will have to code up this logic yourself to handle all the movements because the rectangle can be moved backwards and forwards and up and down....

 

Now my second suggestion

 

"b) Don't invalidate every time.  If the image has only moved 1 pixel, no-one is going to notice this, but it will take a full paint recycle to paint.  Instead just invalidate when you have moved a visible distance. "

 

Firstly you only need to invalidate when you move, so you should move the invalidate() inside your if as follows:

 

if (message.getEvent() == TouchEvent.MOVE) {

if (x1 != -1 && y1 != -1) {

leftUp = new XYPoint(x1, y1);

Invalidate();

}

}

 

Secondly you don't need to do this unless you move a long way.  The following code is intended to make sure you move at least 3 pixels:

 

if (message.getEvent() == TouchEvent.MOVE) {

if (x1 != -1 && y1 != -1) {

if ( (Math.abs(x1 - leftUp.x) > 3 ||  (Math.abs(y1 - leftUp.y) > 3 ) {

leftUp = new XYPoint(x1, y1);

invalidate();

}

}

}

 

I'm sure you can take it from here......

Please use plain text.