이미지 회전 수동으로 약간의 고통이 될 수 지속적으로 서피스 뷰 SurfaceView (예. 산책주기) 내부에 뭔가를 애니메이션하는 가장 좋은 방법을 알고 싶습니다,하지만 여기에 내가했던 방법 그것.
private void animateRotation(int degrees, float durationOfAnimation){
long startTime = SystemClock.elapsedRealtime();
long currentTime;
float elapsedRatio = 0;
Bitmap bufferBitmap = carBitmap;
Matrix matrix = new Matrix();
while (elapsedRatio < 1){
matrix.setRotate(elapsedRatio * degrees);
carBitmap = Bitmap.createBitmap(bufferBitmap, 0, 0, width, height, matrix, true);
//draw your canvas here using whatever method you've defined
currentTime = SystemClock.elapsedRealtime();
elapsedRatio = (currentTime - startTime)/durationOfAnimation;
}
// As elapsed ratio will never exactly equal 1, you have to manually draw the last frame
matrix = new Matrix();
matrix.setRotate(degrees);
carBitmap = Bitmap.createBitmap(bufferBitmap, 0, 0, width, height, matrix, true);
// draw the canvas again here as before
// And you can now set whatever other notification or action you wanted to do at the end of your animation
}
이렇게하면 지정된 시간과 마지막 프레임을 그리는 데 걸리는 시간에 지정한 각도로 carBitmap이 회전합니다. 그러나, 잡기가있다. 이것은 화면상의 위치를 적절히 조정하지 않고 carBitmap을 회전시킵니다. 비트 맵을 그리는 방법에 따라 비트 맵의 왼쪽 상단 구석을 유지하면서 carBitmap을 회전시킬 수 있습니다. 자동차가 회전 할 때 비트 맵은 새로운 자동차 크기에 맞게 늘리고 조정되어 주변의 틈을 투명 픽셀로 채 웁니다. 이것이 어떻게 보일지에 대해 설명하기 어렵다, 그래서 여기에 사각형을 회전하는 예이다 :
회색 영역은 비트 맵의 전체 크기를 나타내며, 투명 픽셀으로 가득합니다. 이 문제를 해결하려면 삼각법을 사용해야합니다. 조금 복잡합니다 ... 이것이 문제가되는 경우 (캔버스에 비트 맵을 그리는 방법이 무엇인지 모르겠으므로 그렇지 않을 수 있습니다), 솔루션을 해결할 수는 없습니다. 나 알고 있고 내가 어떻게했는지 게시 할게.
(이 방법이 가장 효율적인 방법인지는 잘 모르겠지만 비트 맵이 300x300 미만인 한 원활하게 작동합니다. 누군가가 더 좋은 방법을 알고 있다면 우리에게 말해주세요!)
반복적으로 비트 맵을 생성하면 성능 문제가 발생할 가능성이 있습니다. 대신 그것을 한 번 만들고 행렬을'canvas.drawBitmap()'에 전달하십시오. 또한, while 루프는 턴이 끝날 때까지 다른 것들이 움직이지 않게합니다. 게임 루프를 사용해보십시오. http://www.koonsolo.com/news/dewitters-gameloop/ – idbrii