我有两个位图:一个是spritesheet,另一个是circle(base)。我正在尝试围绕它的中心旋转spriteesheet的一个子集,它本身位于基座的中心。
以下是onDraw
:
canvas.drawBitmap(base, 0.0f, 0.0f, null);
canvas.save();
canvas.rotate(rotate, getMeasuredWidth() / 2, getMeasuredHeight() / 2);
canvas.drawBitmap(spriteSheet, srcRec, dstRec, null);
canvas.restore();
onMeasure
:
setMeasuredDimension(base.getWidth(), base.getHeight());
首先,这似乎有效,但旋转spritesheet显示它略微偏离中心。有什么建议吗?
编辑:下面显示了矩形的边界。 “base”和spritesheet都有200px的高度,所以我只是改变左右:
@Override
public boolean onTouchEvent(MotionEvent e) {
float pointerX = e.getX(0), pointerY = e.getY(0);
int currentFrame;
switch(e.getAction()) {
case MotionEvent.ACTION_MOVE:
rotate = -1 * (-180 + (int) Math.toDegrees(Math.atan2((int) pointerX - (int) tempX, (int) pointerY - (int) tempY)));
int dist = (int) Math.sqrt(Math.pow((pointerX - tempX), 2) + Math.pow((pointerY - tempY), 2));
int nextFrame = (int)((float) dist / frameCount);
if(nextFrame == 0) {
currentFrame = 1;
} else if(nextFrame >= frameCount) {
currentFrame = 15;
} else {
currentFrame = nextFrame;
}
srcRec.left = currentFrame * spriteWidth;
srcRec.right = srcRec.left + spriteWidth;
invalidate();
break;
case MotionEvent.ACTION_UP:
currentFrame = 0;
srcRec.left = currentFrame * spriteWidth;
srcRec.right = srcRec.left + spriteWidth;
tempX = 0.0f;
tempY = 0.0f;
rotate = 0.0f;
invalidate();
break;
case MotionEvent.ACTION_DOWN:
currentFrame = 1;
srcRec.left = currentFrame * spriteWidth;
srcRec.right = srcRec.left + spriteWidth;
tempX = pointerX;
tempY = pointerY;
invalidate();
break;
default: break;
}
return true;
}