Android Open Source - Do-not-get-annoyed Dice






From Project

Back to project page Do-not-get-annoyed.

License

The source code is released under:

Apache License

If you think the Android project Do-not-get-annoyed listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package mn100013d.pmu.models;
//  w ww.  j  a  v a 2s  .  c  om
import java.io.Serializable;

import mn100013d.pmu.R;
import mn100013d.pmu.exceptions.PlayerNotRegisteredException;
import mn100013d.pmu.services.SoundService;
import android.content.Context;
import android.os.Vibrator;
import android.util.Log;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.LinearLayout;

public class Dice implements Serializable{
  private View diceHolder;
  private Context context;
  private ImageView dice;
  private int current_value = 6;
  private Animation animation;
  private Vibrator vibrator;
  private static int[] diceImageIds = new int[6];
  static {
    diceImageIds[0] = R.drawable.one;
    diceImageIds[1] = R.drawable.two;
    diceImageIds[2] = R.drawable.three;
    diceImageIds[3] = R.drawable.four;
    diceImageIds[4] = R.drawable.five;
    diceImageIds[5] = R.drawable.six;
  }

  public Dice(View v,Context context) {
    this.context = context;
    this.diceHolder = v;
    dice = new ImageView(context);
    dice.setImageResource(diceImageIds[5]);
    ((LinearLayout) diceHolder).addView(dice);
    animation = AnimationUtils.loadAnimation(this.context, R.anim.dice_rotation);
    vibrator = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);
  }

  public void startRoll() {
    long[] pattern = { 0, 200, 500 };
    vibrator.vibrate(pattern, 0);

    try {
      SoundService.getInstance().play(SoundService.DICE_ROLL, true);
    } catch (PlayerNotRegisteredException e1) {
      // TODO Auto-generated catch block
      e1.printStackTrace();
    }
    dice.startAnimation(animation);
    
  }

  public void stopRoll() {
    vibrator.cancel();
    try {
      SoundService.getInstance().stop(SoundService.DICE_ROLL);
      SoundService.getInstance().play(SoundService.DICE_THROW, false);
    } catch (PlayerNotRegisteredException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
    final int i = (int) (Math.random() * 6);
    dice.clearAnimation();
    setValue(i+1);
    changeImage(i);
    Log.i("Nikola", "Dice value is = "+getValue());
    
    
  }
  
  public void changeImage(int i) {
    dice.setImageResource(diceImageIds[i]);
  }
  public int getValue(){
    return current_value;
  }
  public void setValue(int i){
    this.current_value = i;
  }
  

  

}




Java Source Code List

mn100013d.pmu.BeginingActivity.java
mn100013d.pmu.GameTypeFragment.java
mn100013d.pmu.NewGameActivity.java
mn100013d.pmu.NewGameFragment.java
mn100013d.pmu.PauseFragment.java
mn100013d.pmu.PauseGameFragment.java
mn100013d.pmu.ScoresFragment.java
mn100013d.pmu.SettingsFragment.java
mn100013d.pmu.StartActivity.java
mn100013d.pmu.StartGameFragment.java
mn100013d.pmu.controllers.GameController.java
mn100013d.pmu.data.GameDataDbHelper.java
mn100013d.pmu.data.GameSettingsEditor.java
mn100013d.pmu.data.GameTableContract.java
mn100013d.pmu.exceptions.ContextNotSetException.java
mn100013d.pmu.exceptions.GameExceptions.java
mn100013d.pmu.exceptions.PlayerNotRegisteredException.java
mn100013d.pmu.models.Board.java
mn100013d.pmu.models.CPUGamePlayer.java
mn100013d.pmu.models.Color.java
mn100013d.pmu.models.Dice.java
mn100013d.pmu.models.Field.java
mn100013d.pmu.models.FinishField.java
mn100013d.pmu.models.GamePlayer.java
mn100013d.pmu.models.HomeField.java
mn100013d.pmu.models.HumanGamePlayer.java
mn100013d.pmu.models.PathField.java
mn100013d.pmu.models.Pawn.java
mn100013d.pmu.models.Result.java
mn100013d.pmu.services.FragmentProvider.java
mn100013d.pmu.services.GamePlayerFactory.java
mn100013d.pmu.services.PopupService.java
mn100013d.pmu.services.Randomizer.java
mn100013d.pmu.services.ShakeDetector.java
mn100013d.pmu.services.SoundService.java