如何在android中顺时针和逆时针旋转图像?

时间:2012-04-22 08:32:57

标签: android

我想知道如何使用android中的onTouch()事件顺时针和逆时针旋转图像。如何通过onTouchListener查找它是顺时针还是逆时针旋转?感谢

2 个答案:

答案 0 :(得分:0)

基本上你可以将你的图像放在你自己的类中,扩展View并实现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);

    }        

}

答案 1 :(得分:0)

您可以尝试使用此函数从MotionEvent对象的x和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;
}