一、补间动画
void | addListener(Animator.AnimatorListener listener) Adds a listener to the set of listeners that are sent events through the life of an animation, such as start, repeat, and end. |
void | addPauseListener(Animator.AnimatorPauseListener listener) Adds a pause listener to this animator. |
void | cancel() Cancels the animation. |
Animator | clone() Creates and returns a copy of this object. |
void | end() Ends the animation. |
abstract long | getDuration() Gets the duration of the animation. |
TimeInterpolator | getInterpolator() Returns the timing interpolator that this animation uses. |
ArrayList<Animator.AnimatorListener> | getListeners() Gets the set of |
abstract long | getStartDelay() The amount of time, in milliseconds, to delay processing the animation after |
long | getTotalDuration() Gets the total duration of the animation, accounting for animation sequences, start delay, and repeating. |
boolean | isPaused() Returns whether this animator is currently in a paused state. |
abstract boolean | isRunning() Returns whether this Animator is currently running (having been started and gone past any initial startDelay period and not yet ended). |
boolean | isStarted() Returns whether this Animator has been started and not yet ended. |
void | pause() Pauses a running animation. |
void | removeAllListeners() Removes all |
void | removeListener(Animator.AnimatorListener listener) Removes a listener from the set listening to this animation. |
void | removePauseListener(Animator.AnimatorPauseListener listener) Removes a pause listener from the set listening to this animation. |
void | resume() Resumes a paused animation, causing the animator to pick up where it left off when it was paused. |
abstract Animator | setDuration(long duration) Sets the duration of the animation. |
abstract void | setInterpolator(TimeInterpolator value) The time interpolator used in calculating the elapsed fraction of the animation. |
abstract void | setStartDelay(long startDelay) The amount of time, in milliseconds, to delay processing the animation after |
void | setTarget(Object target) Sets the target object whose property will be animated by this animation. |
void | setupEndValues() This method tells the object to use appropriate information to extract ending values for the animation. |
void | setupStartValues() This method tells the object to use appropriate information to extract starting values for the animation. |
void | start() Starts this animation. |
ImageView iv = (ImageView) findViewById(R.id.iv);
//透明度
AlphaAnimation alphaAnimation = new AlphaAnimation(0, 1.0f);
alphaAnimation.setDuration(3000);
iv.setAnimation(alphaAnimation);
XML:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<alpha
android:duration="3000"
android:fromAlpha="0"
android:toAlpha="1"></alpha>
</set>
2.RotateAnimation 旋转动画
RotateAnimation rotateAnimation = new RotateAnimation(0, 360, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
rotateAnimation.setDuration(3000);
iv.setAnimation(rotateAnimation);
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<rotate
android:duration="3000"
android:fromDegrees="0"
android:pivotX="0.5"
android:pivotY="0.5"
android:toDegrees="360"></rotate>
</set>
ImageView iv = (ImageView) findViewById(R.id.iv);
//旋转
TranslateAnimation translateAnimation = new TranslateAnimation(0, 0, 0, 500);
translateAnimation.setDuration(3000);
translateAnimation.setFillAfter(true);
iv.setAnimation(translateAnimation);
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:fromYDelta="0"
android:toYDelta="500"
android:duration="3000"></translate>
</set>
4.ScaleAnimation 拉伸动画
ImageView iv = (ImageView) findViewById(R.id.iv);
//旋转
ScaleAnimation scaleAnimation = new ScaleAnimation(0, 1.2f, 0, 1.2f);
scaleAnimation.setDuration(3000);
scaleAnimation.setFillAfter(true);
iv.setAnimation(scaleAnimation);
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<scale
android:duration="2000"
android:fillAfter="true"
android:fromXScale="0"
android:fromYScale="1"
android:toXScale="1"
android:toYScale="1"></scale>
</set>
二、属性动画
translationX
和translationY
:这些属性控制在何处查看位于作为从中由其布局容器设置其左边和顶部坐标的增量。rotation
,rotationX
和rotationY
:这些属性控制在2D(旋转rotation
绕轴心点属性)和3D。scaleX
和scaleY
:这些属性控制绕其轴心点视图的2D缩放。pivotX
和pivotY
:这些属性控制枢轴点的位置,在其周围旋转和缩放变换发生。默认情况下,枢转点位于所述物体的中心。x
和y
:这些都是简单实用的属性来描述其容器查看的最后位置,左侧和顶部的价值观和translationX和translationY值的总和。alpha
:表示在视图的Alpha透明度。此值是1(不透明)默认情况下,以0代表完全透明值(不可见)。
ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(iv_, "translationX", 100f, 300);
objectAnimator.setDuration(3000);
objectAnimator.start();
PropertyValuesHolder pvhX = PropertyValuesHolder.ofFloat("translationX", 170f, 170f);
PropertyValuesHolder pvhY = PropertyValuesHolder.ofFloat("translationY", 0f, 400f);
PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1, 1.5f);
PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1, 1.5f);
ObjectAnimator objectAnimator = ObjectAnimator.ofPropertyValuesHolder(iv_, pvhX, pvhY, scaleX, scaleY);
objectAnimator.setDuration(3000);
objectAnimator.start();
Keyframe keyframe0 = Keyframe.ofFloat(0f, 100);
Keyframe keyframe1 = Keyframe.ofFloat(.01f, 500);
Keyframe keyframe2 = Keyframe.ofFloat(0f, 400);
Keyframe keyframe3 = Keyframe.ofFloat(1f, 1000);
PropertyValuesHolder pvhM1 = PropertyValuesHolder.ofKeyframe("translationX", keyframe0, keyframe1);
PropertyValuesHolder pvhM2 = PropertyValuesHolder.ofKeyframe("translationY", keyframe2, keyframe3);
ObjectAnimator trans = ObjectAnimator.ofPropertyValuesHolder(iv_, pvhM1, pvhM2);
trans.setDuration(3000);
trans.start();
参数意思:
每个keyframe变量第一个参数执行总时间0%~100%,0表示动画未执行,1代码动画执行完毕
keyframe0代表动画在0%时的位置
keyframe1代表动画在1%时所处的位置
keyframe2代表动画在0%时的位置
keyframe3代表动画在100%时的位置
类/接口 | 描述 |
---|---|
AccelerateDecelerateInterpolator | 内插器变化率的启动和结束的慢,但通过中间加速。 |
AccelerateInterpolator | 内插的变化,其速度开始慢慢出来,然后加速。 |
AnticipateInterpolator | 内插器且其变动开始向后甩,然后向前。 |
AnticipateOvershootInterpolator | 内插器,其变化落后开始,甩向前和过冲的目标值,然后最终返回到最终值。 |
BounceInterpolator | 内插器,其改变跳动在末端。 |
CycleInterpolator | 内插器的动画将重复周期的指定数目。 |
DecelerateInterpolator | 内插的变化,其速度快开始了和再减速。 |
LinearInterpolator | 内插的变化,其速度是恒定的。 |
OvershootInterpolator | 内插器且其变动甩向前冲过最后一个值,然后回来。 |
TimeInterpolator | 一个接口,可以让你实现自己的插补器。 |
trans.addListener(new Animator.AnimatorListener() {
@Override
public void onAnimationStart(Animator animator) {
//动画开始
}
@Override
public void onAnimationEnd(Animator animator) {
//动画结束
}
@Override
public void onAnimationCancel(Animator animator) {
//动画取消
}
@Override
public void onAnimationRepeat(Animator animator) {
//动画重复
}
});
三、帧动画
<animation-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/icon1" android:duration="150"></item>
<item android:drawable="@drawable/icon2" android:duration="150"></item>
<item android:drawable="@drawable/icon3" android:duration="150"></item>
<item android:drawable="@drawable/icon4" android:duration="150"></item>
<item android:drawable="@drawable/icon5" android:duration="150"></item>
<item android:drawable="@drawable/icon6" android:duration="150"></item>
</animation-list>
②在图片控件上src路径设置成刚才动画列表的文件
<ImageView
android:id="@+id/iv_"
android:layout_width="match_parent"
android:layout_height="300dp"
android:scaleType="fitXY"
android:src="@drawable/ss" />
iv_ = (ImageView) findViewById(R.id.iv_);
animationDrawable = (AnimationDrawable) iv_.getDrawable();
animationDrawable.start();