-
Notifications
You must be signed in to change notification settings - Fork 0
/
MainActivity.java
98 lines (86 loc) · 2.69 KB
/
MainActivity.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package com.example.bottlespinner;
import androidx.appcompat.app.AppCompatActivity;
import androidx.constraintlayout.widget.ConstraintLayout;
import android.os.Bundle;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.RotateAnimation;
import android.widget.Button;
import android.widget.ImageView;
import java.util.Random;
import static com.example.bottlespinner.R.drawable.*;
public class MainActivity extends AppCompatActivity {
ImageView iv;
Button b1;
ConstraintLayout c1;
private Random rng= new Random();
int j;
boolean spinning;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
iv = (ImageView) findViewById(R.id.iv);
b1 = (Button) findViewById(R.id.b1);
c1 = (ConstraintLayout) findViewById(R.id.c1);
iv.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
spin(view);
}
});
}
public void spin(View view)
{
if(!spinning)
{
int i = rng.nextInt(1800);
float pivotX = iv.getWidth() /2;
float pivotY = iv.getHeight() /2;
Animation rotate= new RotateAnimation(j, i, pivotX, pivotY);
rotate.setDuration(2500);
rotate.setFillAfter(true);
rotate.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
spinning = true;
}
@Override
public void onAnimationEnd(Animation animation) {
spinning = false;
}
@Override
public void onAnimationRepeat(Animation animation) {
}
});
j = i;
iv.startAnimation(rotate);
}
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
background();
}
});
}
public void background() {
int j = rng.nextInt(5) + 1;
switch (j) {
case 1:
c1.setBackgroundResource(bg1);
break;
case 2:
c1.setBackgroundResource(bg2);
break;
case 3:
c1.setBackgroundResource(bg3);
break;
case 4:
c1.setBackgroundResource(bg4);
break;
case 5:
c1.setBackgroundResource(bg5);
break;
}
}
}