补间动画、属性动画、帧动画、矢量动画

一、补间动画

补间动画有两种实现方式,代码与xml。
补间动画共用方法:
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 Animator.AnimatorListener objects that are currently listening for events on this Animator object.

abstract long getStartDelay()

The amount of time, in milliseconds, to delay processing the animation after start() is called.

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 listeners and pauseListeners from this object.

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 start() is called.

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.


1.AlphaAnimation 透明度动画可以实现渐变效果。
代码:
   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:
<?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>



3.TranslateAnimation 平移动画
代码:
 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:
<?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:
<?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>

二、属性动画

1.关于属性动画,最上层的类ValueAnimator,若直接使用这个比较难,但是实现的效果会更加随意。
2.本文是直接讲他的子类ObjectAnimator使用。
3.属性动画与补间动画一个最大区别是:比如一个Button,从最左平移到最右,但是此时Button的点击事件在最左边,若点击右边是不管用的,而属性动画则是Button平移过去,点击事件也跟着平移过去。

ObjectAnimator.ofFloat(); 参数二:propertyName值如下:
  • translationXtranslationY:这些属性控制在何处查看位于作为从中由其布局容器设置其左边和顶部坐标的增量。
  • rotationrotationXrotationY:这些属性控制在2D(旋转rotation绕轴心点属性)和3D。
  • scaleXscaleY:这些属性控制绕其轴心点视图的2D缩放。
  • pivotXpivotY:这些属性控制枢轴点的位置,在其周围旋转和缩放变换发生。默认情况下,枢转点位于所述物体的中心。
  • xy:这些都是简单实用的属性来描述其容器查看的最后位置,左侧和顶部的价值观和translationX和translationY值的总和。
  • alpha:表示在视图的Alpha透明度。此值是1(不透明)默认情况下,以0代表完全透明值(不可见)。
例子(效果是往右平移一段距离):
 ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(iv_, "translationX", 100f, 300);
        objectAnimator.setDuration(3000);
        objectAnimator.start();

补间动画有AnimatorSet可以让动画先执行再执行什么,而属性动画拥有ofPropertyValuesHolder()方法,可以让动画同一时间执行多种动画,比如边平移边放大。
 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
 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%时的位置


表3. 内插器(setInterpolator()用来在运动过程中做一些运行设置,补间动画与属性动画都进行设置)
类/接口 描述
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) {
                //动画重复
            }
        });

三、帧动画

1.帧动画就是一系列图片快速切换,大概就这意思,可以用来做等待的对话框

①在Drawable文件中新建一个动画列表:
<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();

四 、矢量动画

贴一个大神的帖子吧,感觉写的非常详细,所以提供大家去看:http://blog.csdn.net/u013478336/article/details/52277875,在此就不再多做写了,里面写的非常详细

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值