package
com.tianjf;
import
android.app.Activity;
import
android.os.Bundle;
import
android.view.View;
import
android.view.View.OnClickListener;
import
android.view.animation.AlphaAnimation;
import
android.view.animation.Animation;
import
android.view.animation.AnimationSet;
import
android.view.animation.RotateAnimation;
import
android.view.animation.ScaleAnimation;
import
android.view.animation.TranslateAnimation;
import
android.widget.Button;
import
android.widget.ImageView;
public
class
AnimationDemoActivity
extends
Activity
implements
OnClickListener {
private
ImageView mImageView;
private
Button mAlphaButton;
private
Button mScaleButton;
private
Button mRotateButton;
private
Button mTranslateButton;
@Override
public
void
onCreate(Bundle savedInstanceState) {
super
.onCreate(savedInstanceState);
setContentView(R.layout.main);
mImageView = (ImageView) findViewById(R.id.imageView);
mAlphaButton = (Button) findViewById(R.id.alphaButton);
mScaleButton = (Button) findViewById(R.id.scaleButton);
mRotateButton = (Button) findViewById(R.id.rotateButton);
mTranslateButton = (Button) findViewById(R.id.translateButton);
mAlphaButton.setOnClickListener(
this
);
mScaleButton.setOnClickListener(
this
);
mRotateButton.setOnClickListener(
this
);
mTranslateButton.setOnClickListener(
this
);
}
@Override
public
void
onClick(View v) {
switch
(v.getId()) {
case
R.id.alphaButton:
AnimationSet animationSet =
new
AnimationSet(
true
);
AlphaAnimation alphaAnimation =
new
AlphaAnimation(
1
,
0
);
alphaAnimation.setDuration(
1000
);
animationSet.addAnimation(alphaAnimation);
mImageView.startAnimation(animationSet);
break
;
case
R.id.scaleButton:
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);
scaleAnimation.setStartOffset(
1000
);
scaleAnimation.setDuration(
2000
);
animationSet.setFillAfter(
true
);
animationSet.addAnimation(scaleAnimation);
mImageView.startAnimation(animationSet);
break
;
case
R.id.rotateButton:
animationSet =
new
AnimationSet(
true
);
RotateAnimation rotateAnimation =
new
RotateAnimation(
0
,
360
,
Animation.RELATIVE_TO_PARENT,
0
.5f, Animation.RELATIVE_TO_PARENT,
0
.25f);
rotateAnimation.setDuration(
5000
);
animationSet.addAnimation(rotateAnimation);
mImageView.startAnimation(animationSet);
break
;
case
R.id.translateButton:
animationSet =
new
AnimationSet(
true
);
TranslateAnimation translateAnimation =
new
TranslateAnimation(
Animation.RELATIVE_TO_SELF, 0f, Animation.RELATIVE_TO_SELF,
0
.5f,
Animation.RELATIVE_TO_SELF, 0f, Animation.RELATIVE_TO_SELF,
1
.0f);
translateAnimation.setDuration(
1000
);
animationSet.addAnimation(translateAnimation);
mImageView.startAnimation(animationSet);
break
;
default
:
break
;
}
}
}