package com.chaiyo.app;
import android.content.Intent;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.os.Vibrator;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.google.android.gms.ads.AdRequest.Builder;
import com.google.android.gms.ads.AdView;
import java.util.Random;
public class MainActivity extends ActionBarActivity
implements View.OnClickListener, AccelerometerListener, CompoundButton.OnCheckedChangeListener
{
public static String[] Chaiyo = { "market://details?id=com.Chaiyo.app", "market://search?q=pub:Chaiyo" };
public static HiloDice[] listDice;
private Button btnOpenClose;
private CheckBox chkSound;
private CheckBox chkVibration;
private int[] dicerollsounds = new int[4];
private ImageView imgDice1;
private ImageView imgDice2;
private ImageView imgDice3;
private boolean isOpened = false;
private LinearLayout layDice;
MediaPlayer mPlayer;
Vibrator mVibrator;
private Random myRnd = new Random();
MySharedPreference mySP = new MySharedPreference(this);
private boolean nowStatSound = true;
private boolean nowStatVibration = true;
private int numRnd1;
private int numRnd2;
private int numRnd3;
int randomNum = 0;
int resId = 0;
private resultDice resultData;
private Random rnd = new Random();
private TextView txtResult;
static
{
HiloDice[] arrayOfHiloDice = new HiloDice[6];
arrayOfHiloDice[0] = new HiloDice("one", 1, 2130837593);
arrayOfHiloDice[1] = new HiloDice("two", 2, 2130837600);
arrayOfHiloDice[2] = new HiloDice("three", 3, 2130837599);
arrayOfHiloDice[3] = new HiloDice("four", 4, 2130837585);
arrayOfHiloDice[4] = new HiloDice("five", 5, 2130837584);
arrayOfHiloDice[5] = new HiloDice("six", 6, 2130837597);
listDice = arrayOfHiloDice;
}
private void audibleFX(int paramInt)
{
if (this.mPlayer != null)
{
this.mPlayer.stop();
this.mPlayer.release();
}
this.mPlayer = MediaPlayer.create(this, paramInt);
this.mPlayer.start();
}
private void vibrationFX()
{
this.mVibrator = ((Vibrator)getSystemService("vibrator"));
this.mVibrator.vibrate(new long[] { 0L, 100L, 0L }, -1);
}
public resultDice CalculateHighLow()
{
if ((this.numRnd1 == this.numRnd2) && (this.numRnd2 == this.numRnd3))
this.resultData = resultDice.ตอง;
while (true)
{
return this.resultData;
if (listDice[this.numRnd1].getValue() + listDice[this.numRnd2].getValue() + listDice[this.numRnd3].getValue() > 10)
this.resultData = resultDice.สูง;
else
this.resultData = resultDice.ต่ำ;
}
}
public void InitialComponents()
{
this.txtResult = ((TextView)findViewById(2131296339));
this.imgDice1 = ((ImageView)findViewById(2131296341));
this.imgDice2 = ((ImageView)findViewById(2131296343));
this.imgDice3 = ((ImageView)findViewById(2131296344));
this.imgDice1.setVisibility(8);
this.imgDice2.setVisibility(8);
this.imgDice3.setVisibility(8);
this.dicerollsounds = new int[] { 2131034113, 2131034114, 2131034115, 2131034116 };
this.btnOpenClose = ((Button)findViewById(2131296345));
this.layDice = ((LinearLayout)findViewById(2131296340));
this.chkSound = ((CheckBox)findViewById(2131296347));
this.chkVibration = ((CheckBox)findViewById(2131296349));
this.btnOpenClose.setOnClickListener(this);
this.chkSound.setOnCheckedChangeListener(this);
this.chkVibration.setOnCheckedChangeListener(this);
}
public void InitialShake()
{
if (AccelerometerManager.isSupported(this))
AccelerometerManager.startListening(this);
}
public void InitialValues()
{
this.mySP.getFromSharedPreference();
this.nowStatSound = this.mySP.isSound;
this.nowStatVibration = this.mySP.isVibration;
}
public void OpenCloseKratip()
{
if (!this.isOpened)
{
this.txtResult.setText(this.resultData.toString());
this.imgDice1.setImageResource(listDice[this.numRnd1].getImage());
this.imgDice2.setImageResource(listDice[this.numRnd2].getImage());
this.imgDice3.setImageResource(listDice[this.numRnd3].getImage());
this.layDice.setBackgroundResource(2130837594);
this.imgDice1.setVisibility(0);
this.imgDice2.setVisibility(0);
this.imgDice3.setVisibility(0);
this.btnOpenClose.setText(2131427348);
this.isOpened = true;
return;
}
this.layDice.setBackgroundResource(2130837556);
this.txtResult.setText(2131427386);
this.imgDice1.setVisibility(8);
this.imgDice2.setVisibility(8);
this.imgDice3.setVisibility(8);
this.btnOpenClose.setText(2131427385);
this.isOpened = false;
}
public void RandomValue()
{
this.numRnd1 = (0 + this.rnd.nextInt(6));
this.numRnd2 = (0 + this.rnd.nextInt(6));
this.numRnd3 = (0 + this.rnd.nextInt(6));
}
public void onAccelerationChanged(float paramFloat1, float paramFloat2, float paramFloat3)
{
}
public void onCheckedChanged(CompoundButton paramCompoundButton, boolean paramBoolean)
{
if (paramCompoundButton.getId() == 2131296347)
if (paramCompoundButton.isChecked())
this.nowStatSound = true;
while (paramCompoundButton.getId() != 2131296349)
{
return;
this.nowStatSound = false;
return;
}
if (paramCompoundButton.isChecked())
{
this.nowStatVibration = true;
return;
}
this.nowStatVibration = false;
}
public void onClick(View paramView)
{
if (paramView.getId() == 2131296345)
{
CalculateHighLow();
OpenCloseKratip();
}
}
protected void onCreate(Bundle paramBundle)
{
super.onCreate(paramBundle);
setContentView(2130903063);
((AdView)findViewById(2131296350)).loadAd(new AdRequest.Builder().build());
InitialComponents();
InitialValues();
InitialShake();
RandomValue();
}
public boolean onCreateOptionsMenu(Menu paramMenu)
{
getMenuInflater().inflate(2131558400, paramMenu);
return true;
}
protected void onDestroy()
{
super.onDestroy();
this.mySP.saveToSharedPreference(this.nowStatSound, this.nowStatVibration);
}
public boolean onOptionsItemSelected(MenuItem paramMenuItem)
{
int i = paramMenuItem.getItemId();
if (i == 2131296351)
{
startActivity(new Intent("android.intent.action.VIEW", Uri.parse(chaiyo[0])));
return true;
}
if (i == 2131296352)
{
startActivity(new Intent("android.intent.action.VIEW", Uri.parse(chaiyo[1])));
return true;
}
return super.onOptionsItemSelected(paramMenuItem);
}
protected void onPause()
{
super.onPause();
this.nowStatSound = false;
this.nowStatVibration = false;
}
protected void onResume()
{
super.onResume();
this.nowStatSound = true;
this.nowStatVibration = true;
}
public void onShake(float paramFloat)
{
if (!this.isOpened)
RandomValue();
while (true)
{
if (this.nowStatSound)
{
this.randomNum = (0 + this.myRnd.nextInt(this.dicerollsounds.length));
this.resId = this.dicerollsounds[this.randomNum];
audibleFX(this.resId);
}
if (this.nowStatVibration)
vibrationFX();
return;
OpenCloseKratip();
RandomValue();
}
}
private static enum resultDice
{
static
{
ต่ำ = new resultDice("ต่ำ", 1);
ตอง = new resultDice("ตอง", 2);
resultDice[] arrayOfresultDice = new resultDice[3];
arrayOfresultDice[0] = สูง;
arrayOfresultDice[1] = ต่ำ;
arrayOfresultDice[2] = ตอง;
}
}
}
ช่วยอ่าน โค้ดจาวา ให้หน่อยครับ
import android.content.Intent;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.os.Vibrator;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.google.android.gms.ads.AdRequest.Builder;
import com.google.android.gms.ads.AdView;
import java.util.Random;
public class MainActivity extends ActionBarActivity
implements View.OnClickListener, AccelerometerListener, CompoundButton.OnCheckedChangeListener
{
public static String[] Chaiyo = { "market://details?id=com.Chaiyo.app", "market://search?q=pub:Chaiyo" };
public static HiloDice[] listDice;
private Button btnOpenClose;
private CheckBox chkSound;
private CheckBox chkVibration;
private int[] dicerollsounds = new int[4];
private ImageView imgDice1;
private ImageView imgDice2;
private ImageView imgDice3;
private boolean isOpened = false;
private LinearLayout layDice;
MediaPlayer mPlayer;
Vibrator mVibrator;
private Random myRnd = new Random();
MySharedPreference mySP = new MySharedPreference(this);
private boolean nowStatSound = true;
private boolean nowStatVibration = true;
private int numRnd1;
private int numRnd2;
private int numRnd3;
int randomNum = 0;
int resId = 0;
private resultDice resultData;
private Random rnd = new Random();
private TextView txtResult;
static
{
HiloDice[] arrayOfHiloDice = new HiloDice[6];
arrayOfHiloDice[0] = new HiloDice("one", 1, 2130837593);
arrayOfHiloDice[1] = new HiloDice("two", 2, 2130837600);
arrayOfHiloDice[2] = new HiloDice("three", 3, 2130837599);
arrayOfHiloDice[3] = new HiloDice("four", 4, 2130837585);
arrayOfHiloDice[4] = new HiloDice("five", 5, 2130837584);
arrayOfHiloDice[5] = new HiloDice("six", 6, 2130837597);
listDice = arrayOfHiloDice;
}
private void audibleFX(int paramInt)
{
if (this.mPlayer != null)
{
this.mPlayer.stop();
this.mPlayer.release();
}
this.mPlayer = MediaPlayer.create(this, paramInt);
this.mPlayer.start();
}
private void vibrationFX()
{
this.mVibrator = ((Vibrator)getSystemService("vibrator"));
this.mVibrator.vibrate(new long[] { 0L, 100L, 0L }, -1);
}
public resultDice CalculateHighLow()
{
if ((this.numRnd1 == this.numRnd2) && (this.numRnd2 == this.numRnd3))
this.resultData = resultDice.ตอง;
while (true)
{
return this.resultData;
if (listDice[this.numRnd1].getValue() + listDice[this.numRnd2].getValue() + listDice[this.numRnd3].getValue() > 10)
this.resultData = resultDice.สูง;
else
this.resultData = resultDice.ต่ำ;
}
}
public void InitialComponents()
{
this.txtResult = ((TextView)findViewById(2131296339));
this.imgDice1 = ((ImageView)findViewById(2131296341));
this.imgDice2 = ((ImageView)findViewById(2131296343));
this.imgDice3 = ((ImageView)findViewById(2131296344));
this.imgDice1.setVisibility(8);
this.imgDice2.setVisibility(8);
this.imgDice3.setVisibility(8);
this.dicerollsounds = new int[] { 2131034113, 2131034114, 2131034115, 2131034116 };
this.btnOpenClose = ((Button)findViewById(2131296345));
this.layDice = ((LinearLayout)findViewById(2131296340));
this.chkSound = ((CheckBox)findViewById(2131296347));
this.chkVibration = ((CheckBox)findViewById(2131296349));
this.btnOpenClose.setOnClickListener(this);
this.chkSound.setOnCheckedChangeListener(this);
this.chkVibration.setOnCheckedChangeListener(this);
}
public void InitialShake()
{
if (AccelerometerManager.isSupported(this))
AccelerometerManager.startListening(this);
}
public void InitialValues()
{
this.mySP.getFromSharedPreference();
this.nowStatSound = this.mySP.isSound;
this.nowStatVibration = this.mySP.isVibration;
}
public void OpenCloseKratip()
{
if (!this.isOpened)
{
this.txtResult.setText(this.resultData.toString());
this.imgDice1.setImageResource(listDice[this.numRnd1].getImage());
this.imgDice2.setImageResource(listDice[this.numRnd2].getImage());
this.imgDice3.setImageResource(listDice[this.numRnd3].getImage());
this.layDice.setBackgroundResource(2130837594);
this.imgDice1.setVisibility(0);
this.imgDice2.setVisibility(0);
this.imgDice3.setVisibility(0);
this.btnOpenClose.setText(2131427348);
this.isOpened = true;
return;
}
this.layDice.setBackgroundResource(2130837556);
this.txtResult.setText(2131427386);
this.imgDice1.setVisibility(8);
this.imgDice2.setVisibility(8);
this.imgDice3.setVisibility(8);
this.btnOpenClose.setText(2131427385);
this.isOpened = false;
}
public void RandomValue()
{
this.numRnd1 = (0 + this.rnd.nextInt(6));
this.numRnd2 = (0 + this.rnd.nextInt(6));
this.numRnd3 = (0 + this.rnd.nextInt(6));
}
public void onAccelerationChanged(float paramFloat1, float paramFloat2, float paramFloat3)
{
}
public void onCheckedChanged(CompoundButton paramCompoundButton, boolean paramBoolean)
{
if (paramCompoundButton.getId() == 2131296347)
if (paramCompoundButton.isChecked())
this.nowStatSound = true;
while (paramCompoundButton.getId() != 2131296349)
{
return;
this.nowStatSound = false;
return;
}
if (paramCompoundButton.isChecked())
{
this.nowStatVibration = true;
return;
}
this.nowStatVibration = false;
}
public void onClick(View paramView)
{
if (paramView.getId() == 2131296345)
{
CalculateHighLow();
OpenCloseKratip();
}
}
protected void onCreate(Bundle paramBundle)
{
super.onCreate(paramBundle);
setContentView(2130903063);
((AdView)findViewById(2131296350)).loadAd(new AdRequest.Builder().build());
InitialComponents();
InitialValues();
InitialShake();
RandomValue();
}
public boolean onCreateOptionsMenu(Menu paramMenu)
{
getMenuInflater().inflate(2131558400, paramMenu);
return true;
}
protected void onDestroy()
{
super.onDestroy();
this.mySP.saveToSharedPreference(this.nowStatSound, this.nowStatVibration);
}
public boolean onOptionsItemSelected(MenuItem paramMenuItem)
{
int i = paramMenuItem.getItemId();
if (i == 2131296351)
{
startActivity(new Intent("android.intent.action.VIEW", Uri.parse(chaiyo[0])));
return true;
}
if (i == 2131296352)
{
startActivity(new Intent("android.intent.action.VIEW", Uri.parse(chaiyo[1])));
return true;
}
return super.onOptionsItemSelected(paramMenuItem);
}
protected void onPause()
{
super.onPause();
this.nowStatSound = false;
this.nowStatVibration = false;
}
protected void onResume()
{
super.onResume();
this.nowStatSound = true;
this.nowStatVibration = true;
}
public void onShake(float paramFloat)
{
if (!this.isOpened)
RandomValue();
while (true)
{
if (this.nowStatSound)
{
this.randomNum = (0 + this.myRnd.nextInt(this.dicerollsounds.length));
this.resId = this.dicerollsounds[this.randomNum];
audibleFX(this.resId);
}
if (this.nowStatVibration)
vibrationFX();
return;
OpenCloseKratip();
RandomValue();
}
}
private static enum resultDice
{
static
{
ต่ำ = new resultDice("ต่ำ", 1);
ตอง = new resultDice("ตอง", 2);
resultDice[] arrayOfresultDice = new resultDice[3];
arrayOfresultDice[0] = สูง;
arrayOfresultDice[1] = ต่ำ;
arrayOfresultDice[2] = ตอง;
}
}
}