动画的种类
透明动画alphaAnimation
在代码中配置动画:
findViewById(R.id.btnAnimMe).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { AlphaAnimation alphaAnimation = new AlphaAnimation(0, 1); alphaAnimation.setDuration(1000); view.startAnimation(alphaAnimation); } });
在xml中配置动画:
<?xml version="1.0" encoding="utf-8"?> <alpha xmlns:android="http://schemas.android.com/apk/res/android" android:fromAlpha="0" android:toAlpha="1" android:duration="1000"> </alpha>
findViewById(R.id.btnAnimMe).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { view.startAnimation(AnimationUtils.loadAnimation(getApplicationContext(), R.anim.aa)); } });
旋转动画RoateAnimation
在代码中配置动画:
findViewById(R.id.btnRotateMe).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { RotateAnimation rotateAnimation = new RotateAnimation(0, 360);
//相对于自身旋转
//RotateAnimation rotateAnimation = new RotateAnimation(0,360, Animation.RELATIVE_TO_SELF, 0.5f,
Animation.RELATIVE_TO_SELF, 0.5f);
rotateAnimation.setDuration(1000); view.startAnimation(rotateAnimation); } });
在xml中配置动画:
<?xml version="1.0" encoding="utf-8"?> <rotate android:fromDegrees="0" android:toDegrees="1" android:duration="1000" android:pivotX="50%" android:pivotY="50%" xmlns:android="http://schemas.android.com/apk/res/android"> </rotate>
view.startAnimation(AnimationUtils.loadAnimation(getApplicationContext(), R.anim.ra));
移动动画translateAnimation
在代码中配置动画:
findViewById(R.id.btnTranslateMe).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { //refer to self position TranslateAnimation ta = new TranslateAnimation(0, 200, 0, 200); ta.setDuration(1000); view.startAnimation(ta); } });
在xml中配置动画:
<?xml version="1.0" encoding="utf-8"?> <translate android:fromXDelta="0" android:toXDelta="200" android:fromYDelta="0" android:toYDelta="200" android:duration="1000" xmlns:android="http://schemas.android.com/apk/res/android"> </translate>
view.startAnimation(AnimationUtils.loadAnimation(getApplicationContext(), R.anim.ta));
缩放动画
在代码中配置动画:
findViewById(R.id.btnScaleMe).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { // ScaleAnimation sa = new ScaleAnimation(0, 1, 0, 1); ScaleAnimation sa = new ScaleAnimation(0, 1, 0, 1, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f); sa.setDuration(1000); view.startAnimation(sa); } });
在xml中配置动画:
<scale android:fromXScale="0" android:toXScale="1" android:fromYScale="0" android:toYScale="1" android:duration="1000" android:pivotX="50%" android:pivotY="50%" xmlns:android="http://schemas.android.com/apk/res/android"> </scale>
view.startAnimation(AnimationUtils.loadAnimation(getApplicationContext(), R.anim.sa));
动画混合
在代码中配置动画:
findViewById(R.id.btnAnimMe).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { AnimationSet as = new AnimationSet(true); as.setDuration(1000); AlphaAnimation aa = new AlphaAnimation(0, 1); aa.setDuration(1000); as.addAnimation(aa); TranslateAnimation ta = new TranslateAnimation(200, 0 , 200, 0); ta.setDuration(1000); as.addAnimation(ta); view.startAnimation(as); } });
在xml中配置动画:
<set xmlns:android="http://schemas.android.com/apk/res/android" android:shareInterpolator="true" android:duration="1000"> <alpha android:fromAlpha="0" android:toAlpha="1"/> <translate android:fromYDelta="200" android:toYDelta="0" android:fromXDelta="200" android:toXDelta="0"/> </set>
动画效果侦听
Animation a = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.sa); a.setAnimationListener(new Animation.AnimationListener() { @Override public void onAnimationStart(Animation animation) { Toast.makeText(MainActivity.this, "Animation Start", Toast.LENGTH_SHORT).show(); } @Override public void onAnimationEnd(Animation animation) { Toast.makeText(MainActivity.this, "Animation End", Toast.LENGTH_SHORT).show(); } @Override public void onAnimationRepeat(Animation animation) { } }); view.startAnimation(a);
自定义动画效果
自定义动画需要自定义一个类继承自Animation, 并重写applyTransformation. 在applyTransformation中,第一个参数interpolatedTime是一个0到1的变化范围。
- 如果调用Transform.setAlpha(interpolatedTime)就是一个透明的AlphaAnimation效果。
- 如果要设置移位的动画,可以通过getmetrix:
t.getMatrix().setTranslate(200*interpolatedTime, 200);
每一个动画执行前都会执行initalize。