0

我有要旋转的圆圈。我正在使用 SimpleOnGestureListener 来处理手势,尤其是 onScroll 方法。

        @Override
    public boolean onScroll(MotionEvent e1, MotionEvent e2,
            float distanceX, float distanceY) {

        float lWidth = e1.getX() - centeralPointX;
        float lHeight = e1.getY() - centeralPointY;
        float lRadius = FloatMath.sqrt(lWidth * lWidth + lHeight * lHeight);

        if ((lRadius <= bigCircleRadius) && (lRadius >= smallCircleRadius)) {

            mLastAngle = (float) (Math.atan(lHeight / lWidth) * 180 / Math.PI)
                    + step / 2;

            float currWidth = e2.getX() - centeralPointX;
            float currHeight = e2.getY() - centeralPointY;
            mCurrentAngle = (float) (Math.atan(currHeight / currWidth) * 180 / Math.PI)
                    + step / 2;

            mAngle = mLastAngle - mCurrentAngle;
            mLastAngle = mCurrentAngle;

            // show angles
            TextView lastANGLE, endANGLE, ANGLE;
            lastANGLE = (TextView) findViewById(R.id.currentAngle);
            lastANGLE.setText("last " + mLastAngle);
            endANGLE = (TextView) findViewById(R.id.endAngle);
            ANGLE = (TextView) findViewById(R.id.angle);

            endANGLE.setText("current " + mCurrentAngle);
            ANGLE.setText("ANGLE " + mAngle);

            rotation = (float) (-mAngle - step / 2);
            if (rotate != null && !rotate.hasEnded()) {
                rotate.cancel();
                rotate.reset();
            }
            rotate = RotateCircle(radius, radius);
            words_layout.startAnimation(rotate);
            return true;
        } else
            return false;
    }

但它不能正常工作。我希望那个圆圈能顺利旋转,但事实并非如此。我怎样才能做到这一点?我曾尝试使用 onFling 方法,但它会在运动结束后旋转我的圆圈。但我需要在移动过程中旋转它。

4

2 回答 2

1

您可以尝试View.onTouchEvent(MotionEvent e)像这样覆盖:

@Override
public boolean onTouchEvent(MotionEvent event) {
    switch(event.getAction()) {
        case MotionEvent.ACTION_DOWN:
            lastEventX = e.getX();
            lastEventY = e.getY();
            return true;

        case MotionEvent.ACTION_MOVE:
            // put your code to rotate here using lastEventX and lastEventY instead of e1.getX() and e1.getY()
            return true;
    }
    return super.onTouchEvent(MotionEvent event);
}
于 2012-11-09T12:20:16.523 回答
1

不要在每次触发 onScroll 时调用 findViewByID(),一遍又一遍地调用相同的 3 个对象。

于 2012-11-09T12:26:33.213 回答