这里通过三个小李子来讲述一下,Android中通过xml获取三种动画的方法。
属性动画:从api 11引入
<!--animator/alpha_animator.xml-->
<objectAnimator xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="5000"
android:propertyName="alpha"
android:valueFrom="0.0f"
android:valueTo="1.0f"></objectAnimator>
ImageView imageView = new ImageView(this);
setContentView(imageView);
imageView.setImageResource(R.drawable.icon_0);
ObjectAnimator objectAnimator = (ObjectAnimator) AnimatorInflater.loadAnimator(this, R.animator.alpha_animator);
objectAnimator.setTarget(imageView);
objectAnimator.start();
视图动画:常用alpha/scale/translate/rotate
<!--anim/test.xml-->
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/accelerate_interpolator">
<rotate
android:duration="1000"
android:fromDegrees="0"
android:pivotX="50%"
android:pivotY="50%"
android:startOffset="500"
android:toDegrees="360" />
<alpha
android:duration="500"
android:fromAlpha="1.0"
android:startOffset="500"
android:toAlpha="0.0"></alpha>
<scale
android:duration="500"
android:fromXScale="1.0"
android:fromYScale="1.0"
android:pivotX="50%"
android:pivotY="50%"
android:startOffset="500"
android:toXScale="0"
android:toYScale="0"></scale>
</set>
ImageView imageView = new ImageView(this);
setContentView(imageView);
imageView.setImageResource(R.drawable.icon_0);
Animation animation = AnimationUtils.loadAnimation(this, R.anim.test);
imageView.setAnimation(animation);
animation.start();
帧动画:几张图片的有规律出现
<?xml version="1.0" encoding="utf-8"?>
<!--drawable/test.xml-->
<animation-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:drawable="@drawable/icon_0"
android:duration="500"></item>
<item
android:drawable="@drawable/icon_bg_release_topic_a"
android:duration="500"></item>
<item
android:drawable="@drawable/icon_1"
android:duration="1000"></item>
</animation-list>
ImageView imageView = new ImageView(this);
setContentView(imageView);
imageView.setBackgroundResource(R.drawable.test);
AnimationDrawable animationDrawable = (AnimationDrawable) imageView.getBackground();
animationDrawable.start();