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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
package com.move;

import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.Toast;

import java.util.Random;

public class MyActivity extends Activity implements Animation.AnimationListener, View.OnClickListener {

    int maxX, maxY;
    Button button1, button2;
    int duration = 500;
    RelativeLayout layout;

    private int getXValue() {
        Random random = new Random();
        return random.nextInt(maxX);
    }

    private int getYValue() {
        Random random = new Random();
        return random.nextInt(maxY);
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        layout = (RelativeLayout) findViewById(R.id.layout);
        DisplayMetrics displaymetrics = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
        maxY = displaymetrics.heightPixels - 50;
        maxX = displaymetrics.widthPixels - 50;

        button1 = (Button) findViewById(R.id.button1);
        button2 = (Button) findViewById(R.id.button2);
//        button1.setFocusable(true);
//        button1.setFocusableInTouchMode(true);
//        button1.setClickable(true);

        CoolAsync async = new CoolAsync();
        async.execute(button1, button2);
        button1.setOnClickListener(this);
        button2.setOnClickListener(this);

    }

    public void onAnimationStart(Animation animation) {

    }

    public void onAnimationEnd(Animation animation) {
//        if (animation == animation1) {
//            int x = getXValue();
//            int y = getYValue();
//            animation1 = new TranslateAnimation(Animation.ABSOLUTE, fromX, Animation.ABSOLUTE, x, Animation.ABSOLUTE, fromY, Animation.ABSOLUTE, y);
//            animation1.setDuration(duration);
//            animation1.setAnimationListener(this);
//            fromX = x;
//            fromY = y;
//            button1.setAnimation(animation1);
//            button1.setClickable(true);
//        } else if (animation == animation2) {
//            int x1 = getXValue();
//            int y1 = getYValue();
//            animation2 = new TranslateAnimation(Animation.ABSOLUTE, fromX1, Animation.ABSOLUTE, x1, Animation.ABSOLUTE, fromY1, Animation.ABSOLUTE, y1);
//            animation2.setDuration(duration + 500);
//            animation2.setAnimationListener(this);
//            fromX1 = x1;
//            fromX1 = x1;
//            fromY1 = y1;
//            button2.setAnimation(animation2);
//            button2.setClickable(true);
//        }
    }

    public void onAnimationRepeat(Animation animation) {

    }

    public void onClick(View view) {
        Toast toast = Toast.makeText(MyActivity.this, ((Button) view).getText(), Toast.LENGTH_SHORT);
        toast.show();
    }

    class CoolAsync extends AsyncTask<Button, Button, Void> implements Animation.AnimationListener {
        Animation animation1, animation2;
        int fromX = 0;
        int fromY = 0;
        int fromX1 = 0;
        int fromY1 = 0;
        int maxX, maxY;
        Button buttonA1, buttonA2;

        public CoolAsync() {
            DisplayMetrics displaymetrics = new DisplayMetrics();
            getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
            maxY = displaymetrics.heightPixels - 50;
            maxX = displaymetrics.widthPixels - 50;
            int x, y, x1, y1;
            x = getXValue();
            y = getYValue();
            x1 = getXValue();
            y1 = getYValue();
            animation1 = new TranslateAnimation(Animation.ABSOLUTE, fromX, Animation.ABSOLUTE, x, Animation.ABSOLUTE, fromY, Animation.ABSOLUTE, y);
            animation2 = new TranslateAnimation(Animation.ABSOLUTE, fromX1, Animation.ABSOLUTE, x1, Animation.ABSOLUTE, fromY1, Animation.ABSOLUTE, y1);
            animation1.setDuration(duration);
            animation2.setDuration(duration + 500);
            animation1.setFillAfter(true);
            animation2.setFillAfter(true);
            animation1.setAnimationListener(this);
            animation2.setAnimationListener(this);


        }

        @Override
        protected Void doInBackground(Button... buttons) {
            buttonA1 = buttons[0];
            buttonA2 = buttons[1];
            buttonA1.setAnimation(animation1);
            buttonA2.setAnimation(animation2);
            return null;
        }

        public void onAnimationStart(Animation animation) {
            //To change body of implemented methods use File | Settings | File Templates.
        }

        public void onAnimationEnd(Animation animation) {
            if (animation == animation1) {
                int x = getXValue();
                int y = getYValue();
                animation1 = new TranslateAnimation(Animation.ABSOLUTE, fromX, Animation.ABSOLUTE, x, Animation.ABSOLUTE, fromY, Animation.ABSOLUTE, y);
                animation1.setDuration(duration);
                animation1.setAnimationListener(this);
                fromX = x;
                fromY = y;
                buttonA1.setAnimation(animation1);
                buttonA1.setClickable(true);
            } else if (animation == animation2) {
                int x1 = getXValue();
                int y1 = getYValue();
                animation2 = new TranslateAnimation(Animation.ABSOLUTE, fromX1, Animation.ABSOLUTE, x1, Animation.ABSOLUTE, fromY1, Animation.ABSOLUTE, y1);
                animation2.setDuration(duration);
                animation2.setAnimationListener(this);
                fromX1 = x1;
                fromX1 = x1;
                fromY1 = y1;
                buttonA2.setAnimation(animation2);
                buttonA2.setClickable(true);
            }
        }

        public void onAnimationRepeat(Animation animation) {
            //To change body of implemented methods use File | Settings | File Templates.
        }

    }
}