是否可以根据以下触摸事件沿圆形路径移动和旋转图像:
我看了这个问题:
Moving an Image in circular motion based on touch events in android
但它只告诉我如何沿圆移动图像,而不是旋转它。

最佳答案

更新:完整示例发布在GitHubathttps://github.com/jselbie/xkcdclock
每次你得到一个触控事件,抓取触控点的x,y坐标,并计算相对于位图中心的旋转角度。使用该值确定要绘制的位图旋转多少。
首先,假设一个逻辑坐标系,其中上面元素的中心点位于x,y空间中的(0,0)。
因此,任何接触点之间相对于中心的角度(以度为单位)可以计算如下:

double ComputeAngle(float x, float y)
{
    final double RADS_TO_DEGREES = 360 / (java.lang.Math.PI*2);
    double result = java.lang.Math.atan2(y,x) * RADS_TO_DEGREES;

    if (result < 0)
    {
        result = 360 + result;
    }

    return result;
}

注意-负角度到正角度的标准化。所以如果接触点是(20,20),上面的函数将返回45度。
要使用此方法,您的活动将需要定义以下成员变量:
float _refX;   // x coordinate of last touch event
float _refY;   // y coordinate or last touch event
float _rotation;  // what angle should the source image be rotated at
float _centerX;         // the actual center coordinate of the canvas we are drawing on
float _centerY;         // the actual center coordinate of the canvas we are drawing on

现在让我们来研究如何跟踪触摸坐标,以便始终有一个最新的“_rotation”变量。
因此,我们针对Android的“触摸处理程序”将如下所示:
boolean onTouch(View v, MotionEvent event)
{
    int action = event.getAction();
    int actionmasked = event.getActionMasked();

    if (!_initialized)
    {
        // if we haven't computed _centerX and _centerY yet, just bail
        return false;
    }

    if (actionmasked == MotionEvent.ACTION_DOWN)
    {
        _refX = event.getX();
        _refY = event.getY();
        return true;
    }
    else if (actionmasked == MotionEvent.ACTION_MOVE)
    {

        // normalize our touch event's X and Y coordinates to be relative to the center coordinate
        float x = event.getX() - _centerX;
        float y =  _centerY - event.getY();

        if ((x != 0) && (y != 0))
        {
            double angleB = ComputeAngle(x, y);

            x = _refX - _centerX;
            y = _centerY - _refY;
            double angleA = ComputeAngle(x,y);

            _rotation += (float)(angleA - angleB);

            this.invalidate();  // tell the view to redraw itself
        }
    }

还有一些细节遗漏了,比如绘制实际的位图。您可能还需要处理action_up和action_cancel事件,以将旋转规格化为始终介于0和360之间。但主要的一点是,上面的代码是一个计算位图在视图上绘制时的旋转的框架。大致如下:
void DrawBitmapInCenter(Bitmap bmp, float scale, float rotation, Canvas canvas)
{
    canvas.save();
    canvas.translate(canvas.getWidth()/2, canvas.getHeight()/2);
    canvas.scale(scale, scale);
    canvas.rotate(rotation);
    canvas.translate(-bmp.getWidth()/2, -bmp.getHeight()/2);
    canvas.drawBitmap(bmp, 0, 0, _paint);
    canvas.restore();
}

10-04 23:10
查看更多