public class Animations_Activity extends Activity {
private Button button1;
private Button button2;
private Button button3;
private Button button4;
private Button button5;
private ImageView imageView;
@Override
protected void onCreate(Bundle savedInstanceState) {
(savedInstanceState);
setContentView(.activity_animations_);
button1=(Button)findViewById(.button_alpha);
button2=(Button)findViewById(.button_rotate);
button3=(Button)findViewById(.button_scale);
button4=(Button)findViewById(.button_translate);
button5=(Button)findViewById(.button_alpha_translate);
imageView=(ImageView)findViewById();
(new MyButton());
(new MyButton());
(new MyButton());
(new MyButton());
(new MyButton());
}
class MyButton implements OnClickListener{
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
switch (()) {
case .button_alpha:
Alpha();
break;
case .button_rotate:
Rotata();
break;
case .button_scale:
Scale();
break;
case .button_translate:
Translate();
break;
case .button_alpha_translate:
Alpha_Translate();
break;
default:
break;
}
}
}
/*
* 1. Create an AnimationSet object, which stores a collection of animations
* 2. Create corresponding Animation objects as needed
* 3. Set corresponding data for Animation objects according to the needs of the animation (i.e., execution effect)
* 4. Add animation object to the AnimationSet object
* 5. Use the control object to start executing AnimationSet
*/
public void Alpha() {
AnimationSet animationSet=new AnimationSet(true);
AlphaAnimation alphaAnimation=new AlphaAnimation(1, 0);
(2000);
(alphaAnimation);
(animationSet);
}
public void Rotata(){
AnimationSet animationSet=new AnimationSet(true);
//The following four parameters define the rotation center position
RotateAnimation rotateAnimation=new RotateAnimation(
0, 360,
Animation.RELATIVE_TO_PARENT, 1f,
Animation.RELATIVE_TO_PARENT, 0f);
(2000);
(rotateAnimation);
(animationSet);
}
public void Scale() {
AnimationSet animationSet=new AnimationSet(true);
ScaleAnimation scaleAnimation=new ScaleAnimation(
1, 0.1f, 1, 0.1f,
Animation.RELATIVE_TO_SELF, 0.5f,
Animation.RELATIVE_TO_SELF, 0.5f);
(2000);
(scaleAnimation);
(scaleAnimation);
}
public void Translate() {
AnimationSet animationSet=new AnimationSet(true);
TranslateAnimation translateAnimation=new TranslateAnimation(
Animation.RELATIVE_TO_SELF, 0f, //The start position of the X-axis
Animation.RELATIVE_TO_SELF, 0.5f, //The end position of X-axis
Animation.RELATIVE_TO_SELF, 0f, //The start position of the Y axis
Animation.RELATIVE_TO_SELF, 1.0f); //The end position of the Y axis
(2000);
(translateAnimation);
/*
* If the setting of the first line is true, the effect is frozen after the animation is executed.
* If the setting of the second line is false, the effect will be frozen after the animation is executed.
* The third line sets a long value, which refers to how many milliseconds the animation is delayed.
* The fourth line defines how many times the animation is executed
*/
(true);
(false);
(2000);
(3);
(animationSet);
}
public void Alpha_Translate() {
AnimationSet animationSet=new AnimationSet(true);
AlphaAnimation alphaAnimation=new AlphaAnimation(1, 0);
(2000);
(alphaAnimation);
TranslateAnimation translateAnimation=new TranslateAnimation(
Animation.RELATIVE_TO_SELF, 0f, //The start position of the X-axis
Animation.RELATIVE_TO_SELF, 0.5f, //The end position of X-axis
Animation.RELATIVE_TO_SELF, 0f, //The start position of the Y axis
Animation.RELATIVE_TO_SELF, 1.0f); //The end position of the Y axis
(2000);
(translateAnimation);
(animationSet);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(.activity_animations_, menu);
return true;
}
}