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
package com.move;

import android.app.Activity;
import android.content.DialogInterface;
import android.graphics.Rect;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.MotionEvent;
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 {

    int fromX = 0;
    int fromY = 0;
    int fromX1 = 0;
    int fromY1 = 0;
    int maxX, maxY;
    Button button1, button2;
    TranslateAnimation animation1, animation2;
    int duration = 1500;
    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);
        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);
        button1.setAnimation(animation1);
        button2.setAnimation(animation2);
        fromX = x;
        fromX1 = x1;
        fromY = y;
        fromY1 = y1;
        animation1.setAnimationListener(this);
        animation2.setAnimationListener(this);




    }

    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;
            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) {
        //To change body of implemented methods use File | Settings | File Templates.
    }

    class CoolAsync extends AsyncTask<Button, Void, Void>{

        @Override
        protected Void doInBackground(Button... buttons) {
            Button button = buttons[0];
            button.setOnClickListener(new View.OnClickListener() {
                public void onClick(View view) {
                    Toast toast = Toast.makeText(MyActivity.this, button1.getText(), Toast.LENGTH_SHORT);
                        toast.show();
                }
            });
           return null;
        }
    }
}