2

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

4

2 回答 2

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);

    }        

}
于 2012-04-22T09:13:35.470 回答
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;
}
于 2013-09-01T15:36:55.143 回答