How i can rotate Drawable?

Problems with Canvas, OpenGL, etc...

How i can rotate Drawable?

Postby Minor » Sat Feb 19, 2011 10:34 pm

Hello, i have 2 classes...
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. public class Head extends GameObject{
  2.  
  3.     public Head(Drawable image) {
  4.         super(image);
  5.         mAngle = 270;
  6.     }
  7.  
  8.     @Override
  9.     protected void UpdatePoint() {
  10.     }
  11.  
  12.     public void fire () {
  13.  
  14.     }
  15.  
  16.     public void rotate (int x, int y) {
  17.         float a = (float) Math.sqrt(Math.abs(Math.pow((x-this.getCenterX()),2)+Math.pow((y-this.getCenterY()),2)));
  18.         float b = (float) Math.sqrt(Math.abs(Math.pow((x - this.getCenterX()), 2)));
  19.         float c = (float) Math.sqrt(Math.abs(Math.pow((y-this.getCenterY()),2)));
  20.  
  21.         float angle = (float) Math.acos((a*a+b*b+c*c)/2*a*b);
  22.  
  23.         if (x<this.getCenterX()) angle+= 90;
  24.         if (y>this.getCenterY())
  25.             if (x<this.getCenterX()) angle +=180;
  26.                 else angle += 270;
  27.  
  28.         // HOW ROTATE ?
  29.     }
  30. }
Parsed in 0.034 seconds, using GeSHi 1.0.8.4


Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. public class GameView extends SurfaceView implements SurfaceHolder.Callback{
  2.     private int mx, my;
  3.  
  4.     private GameThread mThread;
  5.  
  6.     private Head mHead;
  7.  
  8.     private static final String TAG = "ZZZZZuma";
  9.     private int randomBall;
  10.  
  11.     public GameView(Context context, AttributeSet attrs) {
  12.         super(context, attrs);
  13.  
  14.         getHolder().addCallback(this);
  15.         mThread = new GameThread(getHolder(),this);
  16.  
  17.         Resources res = context.getResources();
  18.         mHead = new Head(res.getDrawable(R.drawable.head));
  19.         InitPositions(800,480);
  20.     }
  21.  
  22.      @Override
  23.     public boolean onTouchEvent(MotionEvent event) {
  24.         mx = (int) event.getX();
  25.         my = (int) event.getY();
  26.  
  27.         switch (event.getAction()){
  28.             case MotionEvent.ACTION_DOWN: {
  29.                 mHead.fire();
  30.             } break;
  31.  
  32.             case MotionEvent.ACTION_MOVE: {
  33.                mHead.rotate(mx,my);
  34.             } break;
  35.  
  36.             case MotionEvent.ACTION_UP: {
  37.  
  38.             } break;
  39.         }
  40.         return true;
  41.     }
  42.  
  43.     public void InitPositions(int ScreenWidth, int ScreenHeight){
  44.         mHead.setCoordinate((float) ScreenWidth / 2, (float) ScreenHeight / 2);
  45.     }
  46.  
  47.     public void updateObjects(){
  48.         //Log.v(TAG,"I'm update!");
  49.         mHead.update();
  50.     }
  51.  
  52.     @Override
  53.     public void onDraw(Canvas canvas){
  54.         //Log.v(TAG,"I'm draw!");
  55.         canvas.drawColor(Color.BLACK);
  56.  
  57.         mHead.onDraw(canvas);
  58.     }
  59.  
  60. }
Parsed in 0.037 seconds, using GeSHi 1.0.8.4


When i touch the screen, head turns in the direction of pressing. How do I rotate an image (Drawable) at a given angle? Sorry for my bad English.
Minor
Once Poster
Once Poster
 
Posts: 1
Joined: Sat Feb 19, 2011 10:26 pm

Top

Re: How i can rotate Drawable?

Postby cloudjubei » Wed Feb 23, 2011 6:34 pm

When you draw the object, so in your case mHead do this:

canvas.save();
canvas.rotate(calculateRotation(), (float)centreX, (float)centreY);
mHead.setBounds(topLeftX, topLeftY, topLeftX+width, topLeftY+height);
mHead.draw(canvas);
canvas.restore();

now in the place of calculateRotation() you can just put the angle which has to be in degrees if I remember correctly.
The canvas.save and restore, basically make sure that you don't rotate anything else.
Hope this helps!
cloudjubei
Freshman
Freshman
 
Posts: 8
Joined: Wed Feb 23, 2011 3:50 pm

Top

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

Who is online

Users browsing this forum: No registered users and 5 guests