2017-10-25 4 views
2

アンドロイドでの経験を得るために、私は意思決定アプリを作っています。私は紡ぎ車(幸運のようなホイール)を追加しようとしています。アニメーションがうまくいきました(ちょっと微調整が必​​要ですが、今はうまくいきます)。私が持っている問題は、車輪が止まった場所を検出することです。回転を見て、あらかじめ計算した値にマッチさせると意味がありますが、視覚的には見えません。スピニングホイールアニメーションが正確でない

例:

開始状態は常にこのようです。私はそれをタップし、回転を開始します。

begin state

それは(視覚的に)赤色で回転停止するが、モデルでは、51を言い、それゆえ私のモデルは、黄色で回転停止検出:

stop state

model state

Cur posとPrev posは現在実装されていません。 Rnd posは回転する必要がある度合いの相対的な数です(0に対する相対角度)。 Rndの腐敗は、それが停止する前に余分な回転の数です。真のposは、回転させなければならない度合いの絶対数です。合計時間:アニメーションがかかる時間。コーナー:1ピースに含まれる度数。活性

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:

パブリッククラスルーレット{ プライベート静的ルーレットインスタンス。

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 
} 

マップは、ルーレットアイテムを度の範囲にマップするために使用されます。

私の推測では、アニメーションが長すぎるか、そんなものかと思います。しかし、私はそれを修正する方法は実際には分かりません。誰か?

ありがとうございます!

答えて

2

Um、度ではなくラジアン単位で回転を実装していませんか?

これは、ビジュアルローテーショングラフィックと数学からの数字との間のオフセットにつながる可能性があります。おそらく、すべての仮定度数計算(すなわちx/360)をラジアン計算(すなわち、x/(2*pi))に置き換えることでチェックしますか?

+0

これはおそらくそれです。くそー。私はそれを全く忘れてしまった。私はすべてを変換したときに私はあなたに戻ってきます... – JC97

+0

それはそれでした。しかし、今は値がずっと小さいので、すべてを再計算する必要があります。 – JC97

+1

うれしいことです。私はこの質問が、同じような困難を抱えているときに誰かを救うと確信しています!私は非常に多くの時間、同様のエラーを作りました.... –

関連する問題