更新:完整示例发布在 GitHub https://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度。
要使用此方法,您的 Activity 需要定义以下成员变量:
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 事件以将 _rotation 标准化为始终介于 0 和 360 之间。但要点是上面的代码是一个框架,用于计算应在视图上绘制位图的 _rotation。类似于以下内容:
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();
}