要获得Android的经验,我正在制作一个决策应用程序。我正在尝试添加一个旋转轮(如命运之轮)。我有一个很好的动画(只需要一些小调整,但现在很好)。我遇到的问题是检测车轮停在哪里。如果我查看旋转并将其与预先计算的值匹配,那么它是有意义的,但在视觉上它似乎是关闭的。
例如:
开始状态总是这样。我轻拍它,它开始旋转。
它停止以红色(视觉)旋转,但在模型中它显示为51,因此我的模型检测到它停止以黄色旋转:
目前尚未实施Cur pos和Prev pos。 Rnd pos是需要旋转的相对度数(相对于0)。 Rnd rot是停止前必须进行的额外旋转次数。真实位置是它必须旋转的绝对度数。总时间:动画所用的时间。角落:一件有多少度。
活动中的SpinWheel方法:
private void spinWheel() {
Roulette r = Roulette.getInstance();
r.init(rouletteItemsDEBUG);
r.spinRoulette();
final int truePos = r.getTruePosition();
final long totalTime = r.getTotalTime();
final ObjectAnimator anim = ObjectAnimator.ofFloat(imgSpinningWheel, "rotation", 0, truePos);
anim.setDuration(totalTime);
anim.setInterpolator(new DecelerateInterpolator());
anim.addListener(new Animator.AnimatorListener() {
@Override
public void onAnimationStart(Animator animation) {
imgSpinningWheel.setEnabled(false);
}
@Override
public void onAnimationEnd(Animator animation) {
imgSpinningWheel.setEnabled(true);
txtResult.setText(Roulette.getInstance().getSelectedItem().getValue());
Log.d(TAG, Roulette.getInstance().toString());
Log.d(TAG, imgSpinningWheel.getRotation() + "");
Log.d(TAG, imgSpinningWheel.getRotationX() + "");
Log.d(TAG, imgSpinningWheel.getRotationY() + "");
}
@Override
public void onAnimationCancel(Animator animation) {
}
@Override
public void onAnimationRepeat(Animator animation) {
}
});
anim.start();
}
Roulette.java:
public class Roulette { 私有静态轮盘实例;
private static final long TIME_IN_WHEEL = 1000; //time in one rotation (speed of rotation)
private static final int MIN_ROT = 5;
private static final int MAX_ROT = 6;
private static final Random rnd = new Random();
private RouletteItem currentItem;
private RouletteItem[] rouletteItems;
private NavigableMap<Integer, RouletteItem> navigableMap;
private int randomRotation;
private int randomPosition;
private int truePosition;
private int corner;
private long totalTime;
private Roulette() {
}
public void init(RouletteItem[] rouletteItems) {
this.rouletteItems = rouletteItems;
navigableMap = new TreeMap<>();
for (int i = 0; i < getNumberOfItems(); i++) {
navigableMap.put(i * 51, rouletteItems[i]);
}
}
public void spinRoulette() {
if (rouletteItems == null || rouletteItems.length < 2) {
throw new RouletteException("You need at least 2 rouletteItems added to the wheel!");
}
calculateCorner();
calculateRandomPosition();
calculateRandomRotation();
calculateTruePosition();
calculateTotalTime();
}
/**
* Pinpoint random position in the wheel
*/
private void calculateRandomPosition() {
randomPosition = corner * rnd.nextInt(getNumberOfItems());
}
/**
* Calculates the points one RouletteItem has
*/
private void calculateCorner() {
corner = 360 / getNumberOfItems();
}
/**
* Calculates the time needed to spin to the chosen random position
*/
private void calculateTotalTime() {
totalTime = (TIME_IN_WHEEL * randomRotation + (TIME_IN_WHEEL / 360) * randomPosition);
}
/**
* Calculates random rotation
*/
private void calculateRandomRotation() {
randomRotation = MIN_ROT + rnd.nextInt(MAX_ROT - MIN_ROT);
}
/**
* Calculates the true position
*/
private void calculateTruePosition() {
truePosition = (randomRotation * 360 + randomPosition);
}
//getters and to string omitted
}
地图用于将rouletteItems映射到一定程度的范围。
我的猜测是动画需要太长时间或类似的东西。但我真的没有看到如何解决它。谁做了?
提前致谢!
答案 0 :(得分:2)
嗯,Java不是以弧度而不是度数实现旋转吗?
这可能导致视觉旋转图形与数学中的数字之间的偏移。也许通过用弧度计算(即x/360
)替换所有假设的度数计算(即x/(2*pi)
)进行检查?