You are on page 1of 9

<activity android:name="com.Lottery.

Main"
android:screenOrientation="portrait"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
public class Main extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}
import android.app.Activity;
import android.os.Bundle;
import com.example.R;
public class Main extends Activity implements SensorEventListener {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}
import android.hardware.SensorEventListener;
@Override
public void onSensorChanged(SensorEvent event) {
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
import android.hardware.Sensor;
import android.hardware.SensorEvent;
private SensorManager senSensorManager;
private Sensor senAccelerometer;
import android.hardware.SensorManager;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
senSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
senAccelerometer = senSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMET
ER);
senSensorManager.registerListener(this, senAccelerometer , SensorManager.SEN
SOR_DELAY_NORMAL);
}
public class Main extends Activity implements SensorEventListener {
private SensorManager senSensorManager;
private Sensor senAccelerometer;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
senSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVI
CE);
senAccelerometer = senSensorManager.getDefaultSensor(Sensor.TYPE_ACCELER
OMETER);
senSensorManager.registerListener(this, senAccelerometer , SensorManager
.SENSOR_DELAY_NORMAL);
}
@Override
public void onSensorChanged(SensorEvent sensorEvent) {
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
}
public class Main extends Activity implements SensorEventListener {
private SensorManager senSensorManager;
private Sensor senAccelerometer;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
senSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVI
CE);
senAccelerometer = senSensorManager.getDefaultSensor(Sensor.TYPE_ACCELER
OMETER);
senSensorManager.registerListener(this, senAccelerometer , SensorManager
.SENSOR_DELAY_NORMAL);
}

@Override
public void onSensorChanged(SensorEvent sensorEvent) {
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
}
protected void onResume() {
super.onResume();
senSensorManager.registerListener(this, senAccelerometer, SensorManager.SENS
OR_DELAY_NORMAL);
}
private long lastUpdate = 0;
private float last_x, last_y, last_z;
private static final int SHAKE_THRESHOLD = 600;
public void onSensorChange(SensorEvent sensorEvent) {
Sensor mySensor = sensorEvent.sensor;
if (mySensor.getType() == Sensor.TYPE_ACCELEROMETER) {
}
}
public void onSensorChange(SensorEvent sensorEvent) {
Sensor mySensor = sensorEvent.sensor;
if (mySensor.getType() == Sensor.TYPE_ACCELEROMETER) {
float x = sensorEvent.values[0];
float y = sensorEvent.values[1];
float z = sensorEvent.values[2];
}
}
public void onSensorChange(SensorEvent sensorEvent) {
Sensor mySensor = sensorEvent.sensor;
if (mySensor.getType() == Sensor.TYPE_ACCELEROMETER) {
float x = sensorEvent.values[0];
float y = sensorEvent.values[1];
float z = sensorEvent.values[2];
long curTime = System.currentTimeMillis();
if ((curTime - lastUpdate) > 100) {
long diffTime = (curTime - lastUpdate);
lastUpdate = curTime;
}
}

public void onSensorChange(SensorEvent sensorEvent) {


Sensor mySensor = sensorEvent.sensor;
if (mySensor.getType() == Sensor.TYPE_ACCELEROMETER) {
float x = sensorEvent.values[0];
float y = sensorEvent.values[1];
float z = sensorEvent.values[2];
long curTime = System.currentTimeMillis();
if ((curTime - lastUpdate) > 100) {
long diffTime = (curTime - lastUpdate);
lastUpdate = curTime;
float speed = Math.abs(x + y + z - last_x - last_y - last_z)/ diffTi
me * 10000;
if (speed > SHAKE_THRESHOLD) {
}
last_x = x;
last_y = y;
last_z = z;
}
}
}

<?xml version="1.0" encoding="utf-8"?>


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:weightSum="6"
android:orientation="horizontal">
<FrameLayout
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_margin="5dp"
android:layout_weight="2"
android:id="@+id/ball_1"
android:background="@drawable/blue">
<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/number_1"
android:gravity="center"
android:layout_gravity="center_vertical"
android:textColor="@android:color/white"/>
</FrameLayout>
<FrameLayout

android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_margin="5dp"
android:layout_weight="2"
android:id="@+id/ball_2"
android:background="@drawable/blue">
<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/number_2"
android:gravity="center"
android:layout_gravity="center_vertical"
android:textColor="@android:color/white"/>
</FrameLayout>
<FrameLayout
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_margin="5dp"
android:layout_weight="2"
android:id="@+id/ball_3"
android:background="@drawable/blue">
<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/number_3"
android:gravity="center"
android:layout_gravity="center_vertical"
android:textColor="@android:color/white"/>
</FrameLayout>
</LinearLayout>
<LinearLayout
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:weightSum="6"
android:orientation="horizontal">
<FrameLayout
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_margin="5dp"
android:layout_weight="2"
android:id="@+id/ball_4"
android:background="@drawable/blue">
<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/number_4"
android:gravity="center"
android:layout_gravity="center_vertical"
android:textColor="@android:color/white"/>
</FrameLayout>
<FrameLayout
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_margin="5dp"
android:id="@+id/ball_5"
android:layout_weight="2"
android:background="@drawable/blue">
<TextView
android:layout_width="fill_parent"

android:layout_height="fill_parent"
android:id="@+id/number_5"
android:gravity="center"
android:layout_gravity="center_vertical"
android:textColor="@android:color/white"/>
</FrameLayout>
<FrameLayout
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_margin="5dp"
android:id="@+id/ball_6"
android:layout_weight="2"
android:background="@drawable/blue">
<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/number_6"
android:gravity="center"
android:layout_gravity="center_vertical"
android:textColor="@android:color/white"/>
</FrameLayout>
</LinearLayout>
</LinearLayout>

private void getRandomNumber() {


ArrayList numbersGenerated = new ArrayList();
for (int i = 0; i < 6; i++) {
Random randNumber = new Random();
int iNumber = randNumber.nextInt(48) + 1;
if(!numbersGenerated.contains(iNumber)) {
numbersGenerated.add(iNumber);
} else {
i--;
}
}
}

import java.util.ArrayList;
import java.util.Random;

private void getRandomNumber() {


ArrayList numbersGenerated = new ArrayList();
for (int i = 0; i < 6; i++) {
Random randNumber = new Random();
int iNumber = randNumber.nextInt(48) + 1;
if(!numbersGenerated.contains(iNumber)) {
numbersGenerated.add(iNumber);

} else {
i--;
}
}
TextView text = (TextView)findViewById(R.id.number_1);
text.setText(""+numbersGenerated.get(0));
text = (TextView)findViewById(R.id.number_2);
text.setText(""+numbersGenerated.get(1));
text = (TextView)findViewById(R.id.number_3);
text.setText(""+numbersGenerated.get(2));
text = (TextView)findViewById(R.id.number_4);
text.setText(""+numbersGenerated.get(3));
text = (TextView)findViewById(R.id.number_5);
text.setText(""+numbersGenerated.get(4));
text = (TextView)findViewById(R.id.number_6);
text.setText(""+numbersGenerated.get(5));
FrameLayout ball1 = (FrameLayout) findViewById(R.id.ball_1);
ball1.setVisibility(View.INVISIBLE);
FrameLayout ball2 = (FrameLayout) findViewById(R.id.ball_2);
ball2.setVisibility(View.INVISIBLE);
FrameLayout ball3 = (FrameLayout) findViewById(R.id.ball_3);
ball3.setVisibility(View.INVISIBLE);
FrameLayout ball4 = (FrameLayout) findViewById(R.id.ball_4);
ball4.setVisibility(View.INVISIBLE);
FrameLayout ball5 = (FrameLayout) findViewById(R.id.ball_5);
ball5.setVisibility(View.INVISIBLE);
FrameLayout ball6 = (FrameLayout) findViewById(R.id.ball_6);
ball6.setVisibility(View.INVISIBLE);
Animation a = AnimationUtils.loadAnimation(this, R.anim.move_down_ball_first
);
ball6.setVisibility(View.VISIBLE);
ball6.clearAnimation();
ball6.startAnimation(a);
ball5.setVisibility(View.VISIBLE);
ball5.clearAnimation();
ball5.startAnimation(a);
ball4.setVisibility(View.VISIBLE);
ball4.clearAnimation();
ball4.startAnimation(a);
ball3.setVisibility(View.VISIBLE);
ball3.clearAnimation();
ball3.startAnimation(a);
ball2.setVisibility(View.VISIBLE);

ball2.clearAnimation();
ball2.startAnimation(a);
ball1.setVisibility(View.VISIBLE);
ball1.clearAnimation();
ball1.startAnimation(a);
}

import
import
import
import
import

android.view.View;
android.view.animation.Animation;
android.view.animation.AnimationUtils;
android.widget.FrameLayout;
android.widget.TextView;

<?xml version="1.0" encoding="utf-8"?>


<set xmlns:android="http://schemas.android.com/apk/res/android"
android:fillAfter="true"
android:interpolator="@android:anim/bounce_interpolator">
<scale
android:duration="1500"
android:fromXScale="1.0"
android:fromYScale="-10.0"
android:toXScale="1.0"
android:toYScale="1.0" />
</set>

<?xml version="1.0" encoding="utf-8"?>


<set xmlns:android="http://schemas.android.com/apk/res/android"
android:fillAfter="true"
android:interpolator="@android:anim/bounce_interpolator">
<scale
android:duration="1500"
android:fromXScale="1.0"
android:fromYScale="-10.0"
android:toXScale="1.0"
android:toYScale="1.0" />
</set>
All that's left for us to do is call getRandomNumber in onSensorChanged in the M
ain class. Take a look at the complete implementation of onSensorChanged shown b
elow.
01
02
03
04
05
06
07
08
09

10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
public void onSensorChange(SensorEvent sensorEvent) {
Sensor mySensor = sensorEvent.sensor;
if (mySensor.getType() == Sensor.TYPE_ACCELEROMETER) {
float x = sensorEvent.values[0];
float y = sensorEvent.values[1];
float z = sensorEvent.values[2];
long curTime = System.currentTimeMillis();
if ((curTime - lastUpdate) > 100) {
long diffTime = (curTime - lastUpdate);
lastUpdate = curTime;
float speed = Math.abs(x + y + z - last_x - last_y - last_z)/ diffTi
me * 10000;
if (speed > SHAKE_THRESHOLD) {
getRandomNumber();
}
last_x = x;
last_y = y;
last_z = z;
}
}
}

You might also like