开始学动画之补间动画

    之前学了也有将近一年的android吧,都是自学的,每次要什么都是直接百度,感觉自己没有了网络完全不会写代码了,而现在自己已经大三下学期了,实习的话离学校太远,学校还有课程,所以,这半年我打算好好把知识总结一下,于是我也开始写博客了。我博客起始位置就是动画,之前一直没有碰动画,现在打算好好学学,并记录下来,下次就不用到处google或百度了。如有错误恳请大家指正。

动画一般分为两种,View Animation 和Propety Animation,前者只适用与View,效果比较简单,但是使用非常方便,后者效果很多,单使用比较复杂。本篇主要介绍View Animation,也就是tweenAnimation,共有四中效果,位移,缩放,旋转,渐变,同时能够多个组合起来使用,使用方式大同小异,所以这里只以TranslateAnimation为例,但会提供所有动画的简单实现方式源代码下载:https://github.com/FamliarMan/TweenAnimationStudy

注意,本文介绍的只是最基本的使用方式,就是简单的让view动起来。

一、基于xml的TranslateAnimatioin

      1、在xml中新建动画。 在res文件夹下新建一个目录并命名为anim,然后在anima中新建一个animation资源文件,如下
</pre><pre name="code" class="html"><?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">

    <!--本xml文件中只定义位移动画-->
    <translate
        android:duration="3000"
        android:toXDelta="80"
        android:toYDelta="80" />
</set>
其中translate标签中定义的就是位移动画的各个属性,为了弄清各个属性的作用,强烈推荐看官方文档,这样避免每个都区百度从而浪费大量时间。在此简单说一下各个属android:fromXDelta
  此处是为了设置动画起始位置的水平地址,注意该x值是相对于某个要被设置动画的当前位置而言的偏离值。有三种表示方式,一种float,直接表示偏移量,第二种是相对于该对象的宽度的width,例如 10%,如何对象宽50px,那就相当于android:fromXDelta="5",第三种是相对于父容器的宽度,例如 10%p。
android:toXDelta
动画结束时的水平地址,使用方式如上。
android:fromYDelta
动画开始时的竖直方向偏移值,原理和使用方式同上。
android:toYDelta
动画结束 时的竖直方向偏移值,原理和使用方式同上。
  2、加载并使用动画。动画xml文件建好了,我们就要把他用到某个view上去。方式如下:
 protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_move_animation);
        View v = findViewById(R.id.view_translate);
        Animation translateAnimation = AnimationUtils.loadAnimation(this,R.anim.translate);
        v.startAnimation(translateAnimation);
    }
   很简单,就是先实例化我们定义的动画,然后调用view的startAnimation方法即可
坑:android动画默认结束后会回到原来的位置,如果想让它停留在最后位置就要设置fillBefore,或fillAfter。fillBefore是指动画结束时画面停留在第一帧,fillAfter是指动画结束是画面停留在最后一帧。最关键的问题是,这2个参数不能在</alpha>,</scale>,</translate>,</rotate>中设置,这是没有用的,必须在动画xml文件的</set>节点中设置,或是用java代码设置。

二、利用纯代码实现动画

public class TranslateCodeActivity extends Activity {

    private TranslateAnimation animation;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_translate_code);
        animation = new TranslateAnimation(0,50,0,200);
        animation.setDuration(3000);
        View view = findViewById(R.id.view_translate_code);
        view.startAnimation(animation);

    }
}
这个也很简单,就是将xml中的各个属性用代码设置下。

三、使用interpolators

所有的补间动画都是确定开始点和结束点,剩下的都是算出来的,那么,这些从开始到结束的变化过程必然涉及到了一个变化速率问题。这里使用的就是interpolators。官方说明如下:
An interpolator is an animation modifier defined in XML that affects the rate of change in an animation. This allows your existing animation effects to be accelerated, decelerated, repeated, bounced, etc.
其实就是一个预先在xml中定义好的能够控制动画的加速、减速、重复、弹跳等效果的东西。Android提供了有好几个预先定义好的供我们使用,具体可以查看官方文档。这里简单介绍下使用,直接在定义动画的xml中
增加(注意,在set节点下添加)
   android:interpolator="@android:anim/bounce_interpolator"
  如果觉得android官方提供的不够满意可以自己定义interplators,此处不做介绍。


四、AnimationSet

      这四种动画除了可以单独使用外还可以组合使用,这就用到了AnimationSet,也是两种方式:
利用xml文件直接定义
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:fillAfter="true"
    >

    <alpha
        android:fromAlpha="0.2"
        android:toAlpha="0.8"
        android:duration="3000"
        />
    <translate
        android:fromXDelta="0"
        android:fromYDelta="0"
        android:toYDelta="60"
        android:toXDelta="60"
        android:duration="3000"
        android:interpolator="@android:anim/accelerate_interpolator"/>
    <scale
        android:fromXScale="0.5"
        android:toXScale="1.0"
        android:fromYScale="0.2"
        android:duration="3000"
        android:toYScale="1.0"/>

</set>
使用方式一样
 Animation animation = AnimationUtils.loadAnimation(this,R.anim.animationset);
        view.startAnimation(animation);
利用代码实现:
 ScaleAnimation scaleAnimation = new ScaleAnimation(0.2f,1.0f,0.3f,1.5f);
      TranslateAnimation translateAnimation = new TranslateAnimation(0,0,30,50);
        AnimationSet animationSet = new AnimationSet(false);
        animationSet.addAnimation(scaleAnimation);
        animationSet.addAnimation(translateAnimation);
        animationSet.setDuration(3000);
        animationSet.setFillAfter(true);
        view.startAnimation(animationSet);


这里贴一下官方各种动画的xml语法,便于参考。

    <set>
        A container that holds other animation elements (<alpha>, <scale>, <translate>, <rotate>) or other <set> elements. Represents an AnimationSet.

        attributes:
        android:interpolator
            Interpolator resource. An Interpolator to apply on the animation. The value must be a reference to a resource that specifies an interpolator (not an interpolator class name). There are default interpolator resources available from the platform or you can create your own interpolator resource. See the discussion below for more about Interpolators.
        android:shareInterpolator
            Boolean. "true" if you want to share the same interpolator among all child elements.


    <alpha>
        A fade-in or fade-out animation. Represents an AlphaAnimation.


        attributes:

       android:fromAlpha
            Float. Starting opacity offset, where 0.0 is transparent and 1.0 is opaque.
        android:toAlpha
            Float. Ending opacity offset, where 0.0 is transparent and 1.0 is opaque.
 
   <scale>
        A resizing animation. You can specify the center point of the image from which it grows outward (or inward) by specifying pivotX and pivotY. For example, if these values are 0, 0 (top-left corner), all growth will be down and to the right. Represents a ScaleAnimation.


        attributes:

        android:fromXScale
            Float. Starting X size offset, where 1.0 is no change.
        android:toXScale
            Float. Ending X size offset, where 1.0 is no change.
        android:fromYScale
            Float. Starting Y size offset, where 1.0 is no change.
        android:toYScale
            Float. Ending Y size offset, where 1.0 is no change.
        android:pivotX
            Float. The X coordinate to remain fixed when the object is scaled.
        android:pivotY
            Float. The Y coordinate to remain fixed when the object is scaled.


        For more attributes supported by <scale>, see the Animation class reference (of which, all XML attributes are inherrited by this element).
   
 <translate>
        A vertical and/or horizontal motion. Supports the following attributes in any of the following three formats: values from -100 to 100 ending with "%", indicating a percentage relative to itself; values from -100 to 100 ending in "%p", indicating a percentage relative to its parent; a float value with no suffix, indicating an absolute value. Represents a TranslateAnimation.

        attributes:

        android:fromXDelta
            Float or percentage. Starting X offset. Expressed either: in pixels relative to the normal position (such as "5"), in percentage relative to the element width (such as "5%"), or in percentage relative to the parent width (such as "5%p").
        android:toXDelta
            Float or percentage. Ending X offset. Expressed either: in pixels relative to the normal position (such as "5"), in percentage relative to the element width (such as "5%"), or in percentage relative to the parent width (such as "5%p").
        android:fromYDelta
            Float or percentage. Starting Y offset. Expressed either: in pixels relative to the normal position (such as "5"), in percentage relative to the element height (such as "5%"), or in percentage relative to the parent height (such as "5%p").
        android:toYDelta
            Float or percentage. Ending Y offset. Expressed either: in pixels relative to the normal position (such as "5"), in percentage relative to the element height (such as "5%"), or in percentage relative to the parent height (such as "5%p").


    <rotate>
        A rotation animation. Represents a RotateAnimation.
        attributes:

        android:fromDegrees
            Float. Starting angular position, in degrees.
        android:toDegrees
            Float. Ending angular position, in degrees.
        android:pivotX
            Float or percentage. The X coordinate of the center of rotation. Expressed either: in pixels relative to the object's left edge (such as "5"), in percentage relative to the object's left edge (such as "5%"), or in percentage relative to the parent container's left edge (such as "5%p").
        android:pivotY
            Float or percentage. The Y coordinate of the center of rotation. Expressed either: in pixels relative to the object's top edge (such as "5"), in percentage relative to the object's top edge (such as "5%"), or in percentage relative to the parent container's top edge (such as "5%p").

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值