small medium large xlarge

Generic-user-small
03 Jun 2011, 12:49
tuvia milshtein (53 posts)

Ive managed to dra and drop an image. Now I need rotate it on the screen. How do I do it ?

Generic-user-small
03 Jun 2011, 15:34
Anthony Shaw (138 posts)

It depends on if you are using 2D or 3D. For 2D probably a rotation matrix transformation, this is mentioned on page 223. For 3D probably glRotate as used on page 205 section 10.7

Generic-user-small
03 Jun 2011, 19:35
tuvia milshtein (53 posts)

Hi Anthony Im want to use the 2D rotation. Its just mentioned briefly on 223 pg but only the dragging and zooming are explained. By rotating the image I mean to change the direction of the image(head down or any other direction I want to), not to drag it in circles. Do you know where I can learn about it ?

Burnette_ed_pragsmall
07 Jun 2011, 14:53
Ed Burnette (1330 posts)

You could use ImageView.setImageMatrix() and initialize the matrix so it includes the rotation you need, or you could use View.setRotate() (api level 11 and up). See:

  • http://developer.android.com/reference/android/graphics/Matrix.html
  • http://developer.android.com/reference/android/view/View.html#setRotation(float)
Generic-user-small
08 Jun 2011, 14:22
tuvia milshtein (53 posts)

Hi Ed I couldnt use your advise by myself. Its still beyond me ! Ill appreciate if you could help me in my specific case, and add for the few lines needed. I have a declared image, which I move it with ACTION_MOVE. At the same time Id like to add to it the option of rotating it. Could you add for me the commands needed for that ?

colorPoligons[ 0] = new ColorPoligon(context,R.drawable.iii_mandarin, point1); colorPoligons[ 1] = new ColorPoligon(context,R.drawable.iii_red, point2);

. . . . @Override public boolean onTouchEvent(MotionEvent event) { int eventX = (int)event.getX(); int eventY = (int)event.getY(); int eventaction = event.getAction(); switch (eventaction ) { case MotionEvent.ACTION_DOWN: polID = 0; for (ColorPoligon poligon : colorPoligons) { // check if inside the bounds of the Poligon
// check all the bounds of the Poligon (square) if (eventX > poligon.getX() && eventX < poligon.getX()+boundX && eventY > poligon.getY() && eventY < poligon.getY()+boundY) { polID = poligon.getID(); break; } }

        break; 


   case MotionEvent.ACTION_MOVE:    // touch drag with the Poligon
   	// move the Poligons the same as the finger
       if (polID > 0) 
       {
       	     colorPoligons[polID-1].setX(eventX);
       	     colorPoligons[polID-1].setY(eventY);
       }
   	
       break; 

   case MotionEvent.ACTION_UP: 
        break; 
   } 
   // redraw the canvas
   invalidate();
   return true;
Generic-user-small
08 Jun 2011, 14:30
tuvia milshtein (53 posts)

I see that my line werent transfered correctly so Im trying again to transfer the onTouch Event method

   @Override
public boolean onTouchEvent(MotionEvent event) 
{
	   int eventX = (int)event.getX();
	   int eventY = (int)event.getY();
   int eventaction = event.getAction();
   switch (eventaction ) 
   { 
   case MotionEvent.ACTION_DOWN:
   	polID = 0;
   	for (ColorPoligon poligon : colorPoligons) {
   		if (eventX > poligon.getX() 
   		 && eventX < poligon.getX()
   		 && eventY > poligon.getY()
   		 && eventY < poligon.getY())
   		{
           	polID = poligon.getID();
           	break;
        }
      }            
   break; 
   case MotionEvent.ACTION_MOVE:
       if (polID > 0) 
       {
       	     colorPoligons[polID-1].setX(eventX);
       	     colorPoligons[polID-1].setY(eventY);
       }       	
       break; 
   case MotionEvent.ACTION_UP: 
        break; 
   } 
   invalidate();
   return true;
}
Burnette_ed_pragsmall
08 Jun 2011, 21:31
Ed Burnette (1330 posts)

If you have Honeycomb then instead of setX and setY you can call setRotate() and pass it the angle you want the object to rotate.

For earlier versions it depends on the type of your ColorPolygon object.

If the polygon is an ImageView then instead of calling setRotate you would do a Matrix matrix = new Matrix(), call matrix.postRotate(angle), and then call the polygon.setImageMatrix(matrix).

If it’s some other type you have to get creative. It’s kind of advanced but here’s a neat trick I just found in a search:

  • http://techdroid.kbeanie.com/2011/04/inverted-android-button.html

Good luck.

Generic-user-small
09 Jun 2011, 18:34
tuvia milshtein (53 posts)

I thought this might do the job. It didnt Where is the bug ?

case MotionEvent.ACTION_MOVE: if (polID > 0) { colorPolygons[polID-1].setX(eventX - boundX /2); colorPolygons[polID-1].setY(eventY - boundX /2); float angle = 5; Matrix matrix = new Matrix(); ImageView imageView = new ImageView(getContext()); if(polID == 3)imageView.setImageResource(R.drawable.ooi_up_green); imageView.setImageMatrix(matrix); matrix.postRotate(angle); } break;

The drawable image - ooi_up_green - is my polygon which I want to rotate while I move from one position to another

Burnette_ed_pragsmall
15 Jun 2011, 13:47
Ed Burnette (1330 posts)

Sorry I don’t know. Since this is getting off the topic of the book, I suggest you ask over in the StackOverflow.com forum. Use the “android” tag.

Generic-user-small
15 Jun 2011, 13:53
tuvia milshtein (53 posts)

thank you anyway for the efforts

Generic-user-small
28 Apr 2013, 03:13
Wallis LEE (2 posts)

I happened to know a 2D image rotation app though GOOGLE, it can rotate image in any angle in 360 degrees, and it can be install and integrated both in PC or mobile.

  You must be logged in to comment