public class MainActivity extends Activity {
private ImageView imageView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageView = (ImageView) findViewById(R.id.image);
}
/**
* 透明度
*
* @param view
*/
public void click1(View view) {
// 两个参数分别代表动画图片开始的透明度和结束的透明度
AlphaAnimation animation = new AlphaAnimation(0.0f, 1.0f);
// 设置动画的时间长度
animation.setDuration(2000);
// 设置动画播放的重复次数,设置为2代表一共播放3次
animation.setRepeatCount(2);
// 设置重复的模式,Animation.REVERSE代表往返重复
animation.setRepeatMode(Animation.REVERSE);
imageView.startAnimation(animation);
}
/**
* 缩放
*
* @param view
*/
public void click2(View view) {
// 0.2f(X或Y方向的开始缩放大小为图片的20%) 1.0f(X或Y方向的结束缩放大小为图片的100%)
// Animation.RELATIVE_TO_SELF(以自身相关来改变) 0.5f(X和Y以自身的中心圆点来缩放)
ScaleAnimation animation = new ScaleAnimation(0.2f, 1.0f, 0.2f, 1.0f,
Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,0.5f);
animation.setDuration(2000);
animation.setRepeatCount(2);
animation.setRepeatMode(Animation.REVERSE);
imageView.startAnimation(animation);
}
/**
* 旋转
*
* @param view
*/
public void click3(View view) {
RotateAnimation animation = new RotateAnimation(0, 360,
Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,0.5f);
animation.setDuration(2000);
animation.setRepeatCount(2);
animation.setRepeatMode(Animation.REVERSE);
imageView.startAnimation(animation);
}
/**
* 平移
*
* @param view
*/
public void click4(View view) {
// 水平移动
TranslateAnimation animation = new TranslateAnimation(
Animation.RELATIVE_TO_PARENT, 0.0f,
Animation.RELATIVE_TO_PARENT, 1.0f,
Animation.RELATIVE_TO_PARENT, 0.0f,
Animation.RELATIVE_TO_PARENT, 0.0f);
animation.setDuration(2000);
animation.setRepeatCount(2);
animation.setRepeatMode(Animation.REVERSE);
imageView.startAnimation(animation);
}
/**
* 组合
*
* @param view
*/
public void click5(View view) {
AnimationSet animationSet = new AnimationSet(false);
ScaleAnimation scaleAnimation = new ScaleAnimation(0.2f, 1.0f, 0.2f,
1.0f, Animation.RELATIVE_TO_SELF, 0.5f,
Animation.RELATIVE_TO_SELF, 0.5f);
scaleAnimation.setDuration(2000);
scaleAnimation.setRepeatCount(2);
scaleAnimation.setRepeatMode(Animation.REVERSE);
RotateAnimation rotateAnimation = new RotateAnimation(0, 360,
Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,
0.5f);
rotateAnimation.setDuration(2000);
rotateAnimation.setRepeatCount(2);
rotateAnimation.setRepeatMode(Animation.REVERSE);
TranslateAnimation translateAnimation = new TranslateAnimation(
Animation.RELATIVE_TO_PARENT, 0.0f,
Animation.RELATIVE_TO_PARENT, 1.0f,
Animation.RELATIVE_TO_PARENT, 0.0f,
Animation.RELATIVE_TO_PARENT, 0.0f);
translateAnimation.setDuration(2000);
translateAnimation.setRepeatCount(2);
translateAnimation.setRepeatMode(Animation.REVERSE);
animationSet.addAnimation(translateAnimation);
animationSet.addAnimation(rotateAnimation);
animationSet.addAnimation(scaleAnimation);
// 把平移、旋转、缩放结合在一起的动画
imageView.startAnimation(animationSet);
}
}
android tween animation 补间动画
最新推荐文章于 2024-04-20 12:53:29 发布