안녕하세요. 맨날 팁만 낼름 줏어가는

허접 개발자 낭만폭풍입니다..


손으로 돌리면 돌아가는

룰렛판 같은 뷰를 하나 만들고 있습니다. (아직은 반원에서만 돌리고 있습니다.)

대략 소스는 다음과 같습니다.

(아직 전체소스 공개 못하는점 양해바랍니다.)


public void rotate(float distance) {


int rotateWidth = getWidth() / 2;

int rotateHeight = getHeight() / 2;


float degrees = -(distance/1.5f);

// 분모의 값을 낮추면 회전속도가 빨라집니다.

currentDegrees = currentDegrees + degrees;


if (currentDegrees > 360) {

currentDegrees = currentDegrees - 360;

}


Log.i("Rotate", "current : " + currentDegrees);

Matrix m = new Matrix();

m.setRotate(currentDegrees, rotateWidth, rotateHeight);

setImageMatrix(m);

}


public void fitRotate() {


int rotateWidth = getWidth() / 2;

int rotateHeight = getHeight() / 2;

float fitdegrees = currentDegrees;


if (fitdegrees >= 0) {

if (fitdegrees % 45 < 22.5f) {

fitdegrees = currentDegrees - (currentDegrees % 45);

} else if (fitdegrees % 45 > 22.5f) {

fitdegrees = currentDegrees + (45 - (currentDegrees % 45));

}

} else if (fitdegrees < 0) {

if (fitdegrees % 45 < -22.5f) {

fitdegrees = currentDegrees + (-45 - (currentDegrees % 45));

} else if (fitdegrees % 45 > -22.5f) {

fitdegrees = currentDegrees - (currentDegrees % 45);

}

}

// 360도를 45도 구간으로 나누어서 가까운 쪽의 각도로 보정합니다.


Log.i("Rotate", "animate from : " + currentDegrees + " to : "

+ fitdegrees);


RotateAnimation rotate = new RotateAnimation(currentDegrees,

fitdegrees, RotateAnimation.RELATIVE_TO_SELF, 0.5f,

RotateAnimation.RELATIVE_TO_SELF, 0.5f);

rotate.setDuration(1000);

startAnimation(rotate);

// 보정되기 전의 각도에서 보정된 각도로 애니메이션을 실행합니다.


Matrix m = new Matrix();

m.setRotate(fitdegrees, rotateWidth, rotateHeight);

setImageMatrix(m);

// 실제로 보정된 각도로 이미지를 회전시킵니다.

Log.i("Rotate", "animate from : " + currentDegrees + " to : "

+ fitdegrees);


currentDegrees = fitdegrees;

// 애니메이션이 끝나면 현재 각도에 보정된 각도를 대입합니다.

if (listen != null) {

listen.onDegreesChanged(currentDegrees);

// 인터페이스 메서드 호출

}

}



위의 두 메서드는 각각

스크롤시 손가락을 따라 이미지를 회전시키는 메서드

스크롤이 끝나면 45도 단위로 각도를 보정하는 메서드

입니다.


회전도 잘 되고, 각도 보정도 의도한 대로 잘 동작합니다만,

회전시킨 각도에서 보정된 각도애니메이션이 들어가는데,

이 애니메이션만이 의도한 대로 동작하질 않습니다.

예를 들어 178도로 회전해서 손가락을 놓으면

178도에서 180도로 애니메이션이 들어가야 하는데

전혀 예상치 못한 각도 구간에서 애니메이션이 일어나고 나서는

제가 보정하고자 하던 각도로 맞춰집니다....



RotateAnimation
 rotate = new RotateAnimation(currentDegrees,

fitdegreesRotateAnimation.RELATIVE_TO_SELF0.5f,

RotateAnimation.RELATIVE_TO_SELF0.5f);



currentDegrees는 이미지가 회전할때 현재 상태의 각도를 가지고 있는 변수이고(물론 float형입니다.)

fitdegrees는 45도단위로 보정된 각도를 가지고 있는 변수입니다.



Log
.i("Rotate""animate from : " + currentDegrees + " to : " + fitdegrees);


로그도 열심히 찍어댔습니다. 역시나 의도한 대로 잘 표시됩니다.



데체 뭐가 문제인지 전혀 감이 오지 않습니다..

한가지 미심쩍은건

유독 "0도" 또는 "180도" 로 보정되어야 하는 각도에서는 이게 제대로 동작한다는 겁니다.


고수분들의 도움이 절실히 필요한 시점입니다....ㅠㅠ

안드로이드펍 irc에서 동네북을 맡고있는 퐁퐁입니다.
iOS/Android/Python/PHP
타의로 배운 기술은 잘 까먹습니다.. ㅠㅠ