package com.alanz.path;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.RotateAnimation;
import android.view.animation.TranslateAnimation;
import android.view.animation.Animation.AnimationListener;
import android.widget.Button;
public class Main extends Activity implements OnClickListener{
private final static int RADIUS = 200;
private final static long ANIMATION_TIME = 300;
private final static long TIME_INTERVAL = 30;
private Button mMain;
private Button mB1;
private Button mB2;
private Button mB3;
private Button mB4;
private List<AnimationSet> mOutAnimatinSets = new ArrayList<AnimationSet>();
private List<AnimationSet> mInAnimatinSets = new ArrayList<AnimationSet>();
private List<Button> mList = new ArrayList<Button> ();
private boolean isIn = true;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
initViews();
initAnimation();
}
private void initViews() {
mMain = (Button) findViewById(R.id.main);
mB1 = (Button) findViewById(R.id.b1);
mB2 = (Button) findViewById(R.id.b2);
mB3 = (Button) findViewById(R.id.b3);
mB4 = (Button) findViewById(R.id.b4);
mList.add(mB1);
mList.add(mB2);
mList.add(mB3);
mList.add(mB4);
mMain.setOnClickListener(this);
}
private void initAnimation() {
RotateAnimation outRotaAni = new RotateAnimation(0, 720, 0, 0);
outRotaAni.setDuration(ANIMATION_TIME);
RotateAnimation inRotaAni = new RotateAnimation(720, 0, 0, 0);
inRotaAni.setDuration(ANIMATION_TIME);
int size = mList.size();
for (int i = 0; i < size; i ++) {
final Button button = mList.get(i);
int x;
int y;
double angle;
if (i == 0) {
x = 0;
y = RADIUS;
angle = 0;
} else if (i == mList.size() - 1) {
x = RADIUS;
y = 0;
angle = 90;
} else {
angle = (90 / (size - 1)) * i;
x = (int) (RADIUS * Math.sin(Math.toRadians(angle)));
y = (int) (RADIUS * Math.cos(Math.toRadians(angle)));
}
long time = ANIMATION_TIME - i * TIME_INTERVAL;
// System.out.println("===============================");
// System.out.println("====>" + x);
// System.out.println("====>" + y);
// System.out.println("====>" + angle);
//
TranslateAnimation outTranAni = new TranslateAnimation(0, x ,0 , -y);
outTranAni.setDuration(time);
AnimationSet outSet = new AnimationSet(true);
outSet.addAnimation(outTranAni);
// outSet.addAnimation(outRotaAni);
outSet.setFillAfter(true);
mOutAnimatinSets.add(outSet);
//
final AnimationSet outAfterSet = new AnimationSet(true);
TranslateAnimation outAfterTranAni = new TranslateAnimation(x, x * 8 / 9 ,-y , -y * 8 / 9);
outAfterTranAni.setDuration(time);
outAfterSet.addAnimation(outAfterTranAni);
outAfterSet.setFillAfter(true);
outSet.setAnimationListener(new AnimationListener() {
@Override
public void onAnimationEnd(Animation animation) {
button.startAnimation(outAfterSet);
}
@Override
public void onAnimationRepeat(Animation animation) {
}
@Override
public void onAnimationStart(Animation animation) {
}
});
//
TranslateAnimation inTranAni = new TranslateAnimation(x * 8 / 9, 0 , -y * 8 / 9 , 0);
inTranAni.setDuration(time);
AnimationSet inSet = new AnimationSet(true);
inSet.addAnimation(inTranAni);
// inSet.addAnimation(inRotaAni);
inSet.setFillAfter(true);
mInAnimatinSets.add(inSet);
}
}
@Override
public void onClick(View v) {
if (isIn) {
startOutAnimation();
isIn = false;
} else {
startInAnimation();
isIn = true;
}
}
private void startInAnimation() {
for (int i = 0; i < mList.size(); i ++) {
Button button = mList.get(i);
button.startAnimation(mInAnimatinSets.get(i));
}
}
private void startOutAnimation() {
for (int i = 0; i < mList.size(); i ++) {
Button button = mList.get(i);
button.startAnimation(mOutAnimatinSets.get(i));
}
}
}
- 1
- 2
前往页