2012-04-22 132 views
2

我想知道如何用android中的onTouch()事件顺时针和逆时针旋转图像。以及如何通过onTouchListener找出它是顺时针还是逆时针旋转?由于如何在android中顺时针和逆时针旋转图像?

+0

使用旋转动画,也有很多很好的例子在那里...... http://developer.android.com/reference/android/view/animation/RotateAnimation.html – testingtester 2012-04-22 08:54:46

+0

谢谢,但我想知道在android中使用onTouch()事件顺时针和逆时针旋转图像的方式 – secretlm 2012-04-22 09:01:17

回答

0

基本上你可以将你的形象自己的类,扩展视图,并实现OnClickListener像这里面:

public class CustomImageView extends View implements OnClickListener{ 
    ... 
} 

覆盖您CustomImageView类中的方法的onDraw。旋转可以通过旋转画布对象在onDraw内部实现。

第三,实现onClick以获得点击事件并根据您的需要进行轮换。

的基本布局可能看起来像:

public class CustomImageView extends View implements OnClickListener{ 

    public void onClick (View v){ 

     // process click here 

     // invalidate after click processing in order to redraw 
     this.invalidate(); 
    } 


    protected void onDraw(Canvas canvas) { 

     // draw your image here, might be bitmap or other stuff 

     // rotate canvas now, your logic on clockwise or 
     // counterclockwise rotation goes here 
     canvas.rotate(-90.0f, centerx, centery); 

    }   

} 
+0

谢谢。实际上,我可以使用Matrix类以顺时针和逆时针方向旋转图像。但我想用onTouch()事件顺时针和逆时针旋转图像。 – secretlm 2012-04-22 09:23:39

+0

它与onClick结构相同。处理触摸或点击后重新绘制。 你的旋转逻辑是什么?在第一次触摸时顺时针旋转,在第二次触摸时逆时针旋转等等?你可能需要一个触摸计数器变量,这个变量可以计算出来,所以你可以在onDraw中知道图像应该处于什么样的旋转状态。 – alex 2012-04-22 10:02:08

0

您可以尝试使用此功能得到x和从MotionEvent对象的y值的旋转。 在那里,我仍然需要从给定的矢量x和矢量y的附加计算中找到方向。

private int calculateAngle(float GETX, float GETY) { 
    int direction = 1; 

    double tx = (int) GETX - object_center.x; 
    double ty = (int) GETY - object_center.y; 
    double angleInDegrees = Math.atan2(ty, tx) * 180/Math.PI; 
    int area = 0; 
    int ACTUAL_ANGLE = 270; 

    if (angleInDegrees < 0 && angleInDegrees < -90) { 
     // Need to add 
     // 270+angle degrees 
     // ================= 
     ACTUAL_ANGLE += (int) (180 + angleInDegrees) * direction; 
    } else if (angleInDegrees < 0 && angleInDegrees > -90) { 
     // Need to add 
     // 90+angle degrees 
     // ================= 
     ACTUAL_ANGLE = (int) (90 + angleInDegrees); 
    } else if (angleInDegrees > 0) 
     ACTUAL_ANGLE = 90 + (int) angleInDegrees; 
    return ACTUAL_ANGLE; 
} 
相关问题