move Bitmap using onTouch()

Tutorials concerning the OpenGL® ES cross-platform API for full-function 2D and 3D graphics on the Google-Android platform.

move Bitmap using onTouch()

Postby viju85 » Mon Jan 18, 2010 7:11 am

Hi,
I have a checkers board created within the application with all the pieces created using Bitmaps and Canvas.
If i want to move a particular piece on the board, how do i go about it? I have seen a few documentation about onTouch() but i am not able to move the selected bitmap.

Please help as I am very interested in progressing further with the development and i am stuck here for 3 days.

Thanks,
Vj.
viju85
Junior Developer
Junior Developer
 
Posts: 20
Joined: Mon Feb 23, 2009 10:17 pm

Top

Postby zorro » Mon Jan 18, 2010 12:24 pm

This is an OpenGL section, for Canvas you should probably post elsewhere.
Anyway, you want to move the bitmap with your finger?
User avatar
zorro
Experienced Developer
Experienced Developer
 
Posts: 71
Joined: Mon Aug 10, 2009 3:11 pm
Location: Romania

Postby viju85 » Mon Jan 18, 2010 9:36 pm

yes.
viju85
Junior Developer
Junior Developer
 
Posts: 20
Joined: Mon Feb 23, 2009 10:17 pm

Postby zorro » Tue Jan 19, 2010 8:33 am

In your activity class, you have to implement the following method, with a content similar to this:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2.  
  3.  
  4. public boolean onTouchEvent(final MotionEvent event)
  5.  
  6. {
  7.  
  8.         if (event.getAction() == MotionEvent.ACTION_DOWN)
  9.  
  10.         {
  11.  
  12.             x = event.getX();
  13.  
  14.             y = event.getY();
  15.  
  16.            // (...)    
  17.  
  18.         }
  19.  
  20.  
  21.  
  22.         if (event.getAction() == MotionEvent.ACTION_MOVE)
  23.  
  24.         {
  25.  
  26.            // (...)
  27.  
  28.         }
  29.  
  30.  
  31.  
  32.         if (event.getAction() == MotionEvent.ACTION_UP)
  33.  
  34.         {
  35.  
  36.             x = event.getX();
  37.  
  38.             y = event.getY();
  39.  
  40.            // (...)
  41.  
  42.         }
  43.  
  44.  
  45.  
  46.         try
  47.  
  48.         {
  49.  
  50.             Thread.sleep(50);
  51.  
  52.         }
  53.  
  54.         catch (InterruptedException e)
  55.  
  56.         {
  57.  
  58.         }      
  59.  
  60. }
  61.  
  62.  
Parsed in 0.034 seconds, using GeSHi 1.0.8.4


The last block is added to force a 50ms delay to prevent a flood of touch events that will slow down your app. I use OpenGL for rendering, not Canvas, but I presume the touch input code is the same.
User avatar
zorro
Experienced Developer
Experienced Developer
 
Posts: 71
Joined: Mon Aug 10, 2009 3:11 pm
Location: Romania

Postby viju85 » Tue Jan 19, 2010 8:45 am

Thanks,
Actually i figured it out.

I actually want to move a bitmap diagonally. I am able to move it wherever i want right now. But i was wondering if there is a way in which i can move the bitmap only in the diagonal direction?

Thanks,
Vj.
viju85
Junior Developer
Junior Developer
 
Posts: 20
Joined: Mon Feb 23, 2009 10:17 pm

Postby hardcoras » Tue Jan 19, 2010 1:30 pm

Move your object by equal steps in both directions.

For example:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. int x = event.getX();
  2.  
  3. int y = event.getY();
  4.  
  5. int moveBy = x>y?x:y;
  6.  
  7.  
  8.  
  9. object.move(moveBy, moveBy);
Parsed in 0.031 seconds, using GeSHi 1.0.8.4
hardcoras
Experienced Developer
Experienced Developer
 
Posts: 62
Joined: Sat Nov 14, 2009 2:31 pm
Location: Lithunia

Top

Return to Android 2D/3D Graphics - OpenGL Tutorials

Who is online

Users browsing this forum: No registered users and 3 guests