Android(java)学习笔记142:Android中补间动画(Tween Animation)

本文主要简单介绍补间动画使用代码实现,

关于使用xml实现补间动画,

可以参看:自定义控件三部曲之动画篇(一)——alpha、scale、translate、rotate、set的xml属性及用法

1. 由于这个View动画的逻辑很简单,我这里就直接先附上代码:

activity_main.xml:

 1 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 2     xmlns:tools="http://schemas.android.com/tools"
 3     android:layout_width="match_parent"
 4     android:layout_height="match_parent"
 5     tools:context=".MainActivity" >
 6 
 7     <LinearLayout
 8         android:layout_width="match_parent"
 9         android:layout_height="wrap_content"
10         android:orientation="horizontal" >
11 
12         <Button
13             android:layout_width="0dip"
14             android:layout_height="wrap_content"
15             android:layout_weight="1"
16             android:onClick="alpha"
17             android:text="alpha" />
18 
19         <Button
20             android:layout_width="0dip"
21             android:layout_height="wrap_content"
22             android:layout_weight="1"
23             android:onClick="scale"
24             android:text="scale" />
25 
26         <Button
27             android:layout_width="0dip"
28             android:layout_height="wrap_content"
29             android:layout_weight="1"
30             android:onClick="trans"
31             android:text="trans" />
32 
33         <Button
34             android:layout_width="0dip"
35             android:layout_height="wrap_content"
36             android:layout_weight="1"
37             android:onClick="rotate"
38             android:text="rotate" />
39         
40         <Button
41             android:layout_width="0dip"
42             android:layout_height="wrap_content"
43             android:layout_weight="1"
44             android:onClick="set"
45             android:text="set" />
46     </LinearLayout>
47 
48     <ImageView
49         android:id="@+id/iv"
50         android:layout_width="wrap_content"
51         android:layout_height="wrap_content"
52         android:layout_centerHorizontal="true"
53         android:layout_centerVertical="true"
54         android:src="@drawable/ic_launcher" />
55 
56 </RelativeLayout>

接下来MainActivity.java:

package com.itheima.tweenanim;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.RotateAnimation;
import android.view.animation.ScaleAnimation;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;

public class MainActivity extends Activity {
    private ImageView iv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        iv = (ImageView) findViewById(R.id.iv);
    }

    /**
     * 透明度变化的动画
     * 
     * @param view
     */
    public void alpha(View view) {
        // 声明动画 完全透明--》完全不透明
        AlphaAnimation aa = new AlphaAnimation(0.0f, 1.0f);
        // 设置动画播放的时间
        aa.setDuration(2000);
        // 重复播放的次数
        aa.setRepeatCount(2);
        // 倒序播放
        aa.setRepeatMode(Animation.REVERSE);
        iv.startAnimation(aa);
    }

    /**
     * 缩放动画
     * 
     * @param view
     */
    public void scale(View view) {
        ScaleAnimation sa = new ScaleAnimation(0.1f, 2.0f, 0.1f, 2.0f,
                Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,
                0.5f);
        // 设置动画播放的时间
        sa.setDuration(2000);
        // 重复播放的次数
        sa.setRepeatCount(2);
        // 倒序播放
        sa.setRepeatMode(Animation.REVERSE);
        iv.startAnimation(sa);
    }

    /**
     * 位移动画
     * 
     * @param view
     */
    public void trans(View view) {
        TranslateAnimation ta = new TranslateAnimation(
                Animation.RELATIVE_TO_PARENT, -0.5f,
                Animation.RELATIVE_TO_PARENT, 0.5f,
                Animation.RELATIVE_TO_PARENT, -0.5f,
                Animation.RELATIVE_TO_PARENT, 0.5f);
        // 设置动画播放的时间
        ta.setDuration(2000);
        // 重复播放的次数
        ta.setRepeatCount(2);
        // 倒序播放
        ta.setRepeatMode(Animation.REVERSE);
        iv.startAnimation(ta);
    }

    public void rotate(View view) {
        RotateAnimation ra = new RotateAnimation(0, 360,
                Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF,
                0.0f);
        // 设置动画播放的时间
        ra.setDuration(2000);
        // 重复播放的次数
        ra.setRepeatCount(2);
        // 倒序播放
        ra.setRepeatMode(Animation.REVERSE);
        iv.startAnimation(ra);
    }
    /**
     * 动画集合
     * @param view
     */
    public void set(View view){
        AnimationSet set = new AnimationSet(false);//每个动画时间变化的情况都是独立的
        ScaleAnimation sa = new ScaleAnimation(0.1f, 2.0f, 0.1f, 2.0f,
                Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,
                0.5f);
        // 设置动画播放的时间
        sa.setDuration(2000);
        // 重复播放的次数
        sa.setRepeatCount(2);
        // 倒序播放
        sa.setRepeatMode(Animation.REVERSE);
        TranslateAnimation ta = new TranslateAnimation(
                Animation.RELATIVE_TO_PARENT, -0.2f,
                Animation.RELATIVE_TO_PARENT, 0.2f,
                Animation.RELATIVE_TO_PARENT, -0.2f,
                Animation.RELATIVE_TO_PARENT, 0.2f);
        // 设置动画播放的时间
        ta.setDuration(2000);
        // 重复播放的次数
        ta.setRepeatCount(2);
        // 倒序播放
        ta.setRepeatMode(Animation.REVERSE);
        RotateAnimation ra = new RotateAnimation(0, 360,
                Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF,
                0.0f);
        // 设置动画播放的时间
        ra.setDuration(2000);
        // 重复播放的次数
        ra.setRepeatCount(2);
        // 倒序播放
        ra.setRepeatMode(Animation.REVERSE);
        set.addAnimation(ra);
        set.addAnimation(ta);
        set.addAnimation(sa);
        iv.startAnimation(set);
    }
}

 2. 构造方法解析

(1)AlphaAnimation 

AlphaAnimation 动画,窗口的动画效果,渐变透明度动画效果 ,有些游戏的欢迎动画,logo的淡入淡出效果就使用AlphaAnimation。

AlphaAnimation(float fromAlpha, float toAlpha);

浮点型值:

             fromAlpha 属性为动画起始时透明度

                 toAlpha  属性为动画结束时透明度

说明:

            0.0表示完全透明

           1.0表示完全不透明

以上值取0.0-1.0之间的float数据类型的数字

(2)ScaleAnimation

ScaleAnimation类是Android系统中的尺寸变化动画类,用于控制View对象的尺寸变化,该类继承于Animation类。ScaleAnimation类中的很多方法都与Animation类一致,该类中最常用的方法便是ScaleAnimation构造方法。

public ScaleAnimation (float fromX, float toX, float fromY, float toY, int pivotXType, float pivotXValue, int pivotYType, float pivotYValue);

 

参数说明

fromX:动画起始时,X坐标上的伸缩尺寸。              0.0表示收缩到没有

toX:动画结束时,X坐标上的伸缩尺寸 。                  1.0表示正常无伸缩

fromY:动画起始时,Y坐标上的伸缩尺寸 。            值小于1.0表示收缩

toY:动画结束时,Y坐标上的伸缩尺寸。                  值大于1.0表示放大

这4个参数确定从什么大小缩放到什么大小

pivotXType:动画在X轴相对于物件位置类型 (X轴的伸缩模式),可以取值为ABSOLUTE、RELATIVE_TO_SELF、RELATIVE_TO_PARENT。

pivotXValue:动画相对于物件的X坐标的开始位置。

pivotYType:动画在Y轴相对于物件位置类型 (Y轴的伸缩模式),可以取值为ABSOLUTE、RELATIVE_TO_SELF、RELATIVE_TO_PARENT。

pivotYValue:动画相对于物件的Y坐标的开始位置 。

这4个参数确定开始缩放的坐标,最后坐标是原来的坐标

有三种默认值:

RELATIVE_TO_PARENT 相对于父控件

RELATIVE_TO_SELF 相对于符自己

RELATIVE_TO_ABSOLUTE 绝对坐标

 

(3)TranslateAnimation 

在android动画中,最常用的一个莫不是TranslateAnimation了,这个类主要负责实现控件的动态位移,经常被用做指示器的移动动画。比如qq安卓客户端的指示器,如下图。

 

 

(4)RotateAnimation 

public RotateAnimation (float fromDegrees, float toDegrees, int pivotXType, float pivotXValue, int pivotYType, float pivotYValue);

参数说明

fromDegrees:旋转的开始角度。

toDegrees:旋转的结束角度。

pivotXType:X轴的伸缩模式,可以取值为ABSOLUTE、RELATIVE_TO_SELF、RELATIVE_TO_PARENT。

pivotXValue:X坐标的伸缩值。

pivotYType:Y轴的伸缩模式,可以取值为ABSOLUTE、RELATIVE_TO_SELF、RELATIVE_TO_PARENT。

pivotYValue:Y坐标的伸缩值。

 

posted on 2015-08-28 12:40  鸿钧老祖  阅读(300)  评论(0)    收藏  举报

导航