2012-11-22 71 views
0

也许我想问一些明显的问题,但我找不到解决方案,所以我有一个imageview至极是一个弩,我希望它射击到我在屏幕上触摸的地方,我使用postRotate根据触摸位置的功能旋转它,问题是每次触摸它都会将新角度添加到旧的角度,依此类推。有一种方法可以获得ImageView的实际角度?获取实际角度postrotate

int scrWidth = getWindowManager().getDefaultDisplay().getWidth(); 
     int scrHeight = getWindowManager().getDefaultDisplay().getHeight(); 

     float x=event.getX();//capturamos x e y 
     float y=event.getY(); 
     float i=0; 


     if(y<(scrHeight/2)){ 
      float yf=(scrHeight/2)-y;//obtenemos la y respecto a la mitad de la pantalla 
      double alfa=Math.toDegrees(Math.atan2(yf, x));//encontramos el angulo para las x e y encontradas 
      float beta= (float) alfa; //convertimos a float para rotar 
      beta=beta+i;     
      matrix.postRotate(-beta, ballesta.getWidth()/2,ballesta.getHeight()/2); //primer valor angulo, segundo y tercero punto de ancla de la imagen 
      ballesta.setImageMatrix(matrix);     
     } 
     if(y>(scrHeight/2)){ 
      float yf=y-(scrHeight/2);//obtenemos la y respecto a la mitad de la pantalla 
      double alfa=Math.toDegrees(Math.atan2(yf, x));//encontramos el angulo para las x e y encontradas 
      float beta= (float) alfa; //convertimos a float para rotar 
      beta=beta+i; 
      matrix.postRotate(beta, ballesta.getWidth()/2,ballesta.getHeight()/2); 
      ballesta.setImageMatrix(matrix); 
     } 

回答

0

没关系,我媒体链接找到了解决办法,再加上我在我的问题错过信息(所有的代码是ontouch事件的内部)。 解决的办法是在ontouch事件之外声明一个静态变量,然后我保持每次在该变量中旋转的角度,并且在旋转的事件开始时减去静态变量。不知道我是否很清楚(我很困倦:P)。