Back to project page Do-not-get-annoyed.
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.
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; } }