So the thing is i want to imitate the process of bubble sort algorithm for my homework. The way i want to do this is - to play the first fade out animation, then stop my for loop, wait until the animation is done, reassign values, play the second animation, wait until it's done and resume the for loop. I found Thread.sleep()
but in the end my app is freezing or the screen is black until all the animations are done. I have experimented a lot so my code looks pretty bad(sorry).
public class BubbleSort extends AppCompatActivity {
Button element1;
Button element2;
Animation fadeIn;
Animation fadeOut;
int[] values;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_bubble_sort);
fadeIn = AnimationUtils.loadAnimation(getApplicationContext(),
R.anim.fade_in);
fadeOut = AnimationUtils.loadAnimation(getApplicationContext(),
R.anim.fade_out);
}
public void onSort(View view){
HashMap<Integer, Integer> indexes = sort();
Set<Integer> keySet = indexes.keySet();
Integer[] keys = keySet.toArray(new Integer[keySet.size()]);
for (int key : keys) {
initElements(key, indexes.get(key));
element1.startAnimation(fadeOut);
element2.startAnimation(fadeOut);
try {
Thread.sleep(2000);
element1.startAnimation(fadeIn);
element1.setText("" + values[key]);
element2.startAnimation(fadeIn);
element2.setText("" + values[indexes.get(key)]);
} catch (Exception e) {
Log.i("test", e.toString());
}
}
}
void initElements(int from, int to){
switch (from){
case 0:{
element1 = (Button) findViewById(R.id.element_1);
break;
}
case 1:{
element1 = (Button) findViewById(R.id.element_2);
break;
}
case 2:{
element1 = (Button) findViewById(R.id.element_3);
break;
}
case 3:{
element1 = (Button) findViewById(R.id.element_4);
break;
}
case 4:{
element1 = (Button) findViewById(R.id.element_5);
break;
}
case 5:{
element1 = (Button) findViewById(R.id.element_6);
break;
}
case 6:{
element1 = (Button) findViewById(R.id.element_7);
break;
}
case 7:{
element1 = (Button) findViewById(R.id.element_8);
break;
}
case 8:{
element1 = (Button) findViewById(R.id.element_9);
break;
}
case 9:{
element1 = (Button) findViewById(R.id.element_10);
break;
}
default:{
element1 = null;
break;
}
}
switch (to){
case 0:{
element2 = (Button) findViewById(R.id.element_1);
break;
}
case 1:{
element2 = (Button) findViewById(R.id.element_2);
break;
}
case 2:{
element2 = (Button) findViewById(R.id.element_3);
break;
}
case 3:{
element2 = (Button) findViewById(R.id.element_4);
break;
}
case 4:{
element2 = (Button) findViewById(R.id.element_5);
break;
}
case 5:{
element2 = (Button) findViewById(R.id.element_6);
break;
}
case 6:{
element2 = (Button) findViewById(R.id.element_7);
break;
}
case 7:{
element2 = (Button) findViewById(R.id.element_8);
break;
}
case 8:{
element2 = (Button) findViewById(R.id.element_9);
break;
}
case 9:{
element2 = (Button) findViewById(R.id.element_10);
break;
}
default:{
element2 = null;
break;
}
}
}
HashMap<Integer, Integer> sort(){
HashMap<Integer, Integer> indexes = new HashMap<>();
values = new int[10];
boolean sorting = true;
int loop = 1;
Random random = new Random();
for (int index = 0; index < values.length; index++){
values[index] = random.nextInt(10);
}
int offset = 0;
while (sorting){
System.out.println("To sort:");
for (int element : values)
System.out.print(" " + element);
System.out.println();
sorting = false;
for (int index = 0; index < values.length - 1 - offset; index++) {
if (values[index] > values[index + 1]) {
int temp = values[index];
values[index] = values[index + 1];
values[index + 1] = temp;
sorting = true;
indexes.put(index, index + 1);
}
}
System.out.println("Loop: " + loop);
for (int element : values)
System.out.print(" " + element);
System.out.println();
loop++;
offset++;
}
return indexes;
}
}