Android 动画片之Tween动画详细讲解及java源码实现

Android 动画之Tween动画详细讲解及java源码实现Tween动画又称“补间动画”、“中间动画”,这并不重要,就好像很

Android 动画之Tween动画详细讲解及java源码实现

Tween动画又称“补间动画”、“中间动画”,这并不重要,就好像很多人都知道鲁迅,却不知道他叫:周树人。

?

? ?Tween动画在Android中分为4类,它们分别是:AlphaAnimation(透明度动画)、TranslateAnimation(平移动画)、ScaleAnimation(缩放动画)、RotateAnimation(旋转动画)。都继承自android.view.Animation类,它们都是表示从一个状态A向状态B变化的一个过程,所以英文名字叫Tween动画、中文名叫:“补间动画”、“中间动画”。它们总的说来有两种实现方式:java code(java源代码)、xml(xml配置文件),这里先从java code开始

?

? ?以前就是因为每中Tween动画都有很多构造函数不清楚,现在仔细看了下,记录下来方便以后查看

?

? ? AlphaAnimation(透明度动画)

?

? ? AlphaAnimation有两个构造函数,分别是:

?

? ? ? ? ? ? ? ? ? ? ? ?—— AlphaAnimation(Context context, AttributeSet attrs):第二个参数是个属性集,之后会详细对AttributeSet 讲解

?

? ? ? ? ? ? ? ? ? ? ? ?——AlphaAnimation(float fromAlpha, float toAlpha):第一个参数是初始透明度,第二个参数是终止透明度

?

? ? TranslateAnimation(平移动画)

?

? ? TranslateAnimation有三个构造函数,分别是:

?

? ? ? ? ? ? ? ? ? ? ? ?——TranslateAnimation(Context context, AttributeSet attrs):略过

?

? ? ? ? ? ? ? ? ? ? ? ?——TranslateAnimation(float fromXDelta, float toXDelta, float fromYDelta, float toYDelta):分别对应x轴的起始、终点 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?坐标,与y轴的起始、终点坐标

?

? ? ? ? ? ? ? ? ? ? ? ——TranslateAnimation(int fromXType, float fromXValue, int toXType, float toXValue, int fromYType, float fromYValue, int ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? toYType, float toYValue):第一个参数是x轴方向的值的参照(Animation.ABSOLUTE, Animation.RELATIVE_TO_SELF, ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? or Animation.RELATIVE_TO_PARENT);第二个参数是第一个参数类型的起始值;第三个参数与第四个参数是x轴方向的

?

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 终点参照与对应值;后面四个参数就不用解释了。如果全部选择Animation.ABSOLUTE,其实就是第二个构造函数。

?

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 以x轴为例介绍参照与对应值的关系

?

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 如果选择参照为Animation.ABSOLUTE,那么对应的值应该是具体的坐标值,比如100到300,指绝对的屏幕像素单位

?

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 如果选择参照为Animation.RELATIVE_TO_SELF或者 Animation.RELATIVE_TO_PARENT指的是相对于自身或父控件,

?

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 对应值应该理解为相对于自身或者父控件的几倍或百分之多少。一定要多试试这几个参数类型!

?

? ? ? ?ScaleAnimation(缩放动画)

?

? ? ? ?ScaleAnimation(缩放动画)有四个构造函数,分别是:

?

? ? ? ? ? ? ? ? ? ? ? ?——ScaleAnimation(Context context, AttributeSet attrs):略过

?

? ? ? ? ? ? ? ? ? ? ? ?——ScaleAnimation(float fromX, float toX, float fromY, float toY):同TranslateAnimation(float fromXDelta, float toXDelta, ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?float fromYDelta, float toYDelta)

?

? ? ? ? ? ? ? ? ? ? ? ?——ScaleAnimation(float fromX, float toX, float fromY, float toY, float pivotX, float pivotY):这里解释后面两个参数,pivot

?

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?英文意思为“枢轴”,也就是支点。通过这两个参数可以控制缩放动画的放大方向,这个点不会随对象大小变化而变化

?

? ? ? ? ? ? ? ? ? ? ? ——ScaleAnimation(float fromX, float toX, float fromY, float toY, int pivotXType, float pivotXValue, int pivotYType, float ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? pivotYValue):如果理解了前面所讲的,这个就不做多的说明,如果不清楚,请回头多用代码试试。

?

? ? ? ? RotateAnimation(旋转动画)

?

? ? ? ? ?RotateAnimation(旋转动画)同样有四个构造函数,分别是:

?

? ? ? ? ? ? ? ? ? ? ? ——RotateAnimation(Context context, AttributeSet attrs)

?

? ? ? ? ? ? ? ? ? ? ?——RotateAnimation(float fromDegrees, float toDegrees)

?

? ? ? ? ? ? ? ? ? ? ?——RotateAnimation(float fromDegrees, float toDegrees, float pivotX, float pivotY)

?

? ? ? ? ? ? ? ? ? ? ?——RotateAnimation(float fromDegrees, float toDegrees, int pivotXType, float pivotXValue, int pivotYType, float ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?pivotYValue)

?

? ? ? ? ? ? ? ? ? ? 这里不废话了!

?

说了这么多,直接上代码。

?

这里是Java源代码

?

?

package com.tfsp.training.testtweenanimation; import android.app.Activity;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.view.animation.AlphaAnimation;import android.view.animation.Animation;import android.view.animation.RotateAnimation;import android.view.animation.ScaleAnimation;import android.view.animation.TranslateAnimation;import android.widget.ArrayAdapter;import android.widget.Button;import android.widget.ImageView;import android.widget.Spinner; public class TestTweenAnimation extends Activity {//定义开始按钮private Button start = null;//定义动画类型下拉列表private Spinner select = null;//这张图片是动画执行者private ImageView img = null;//定义动画private Animation tAnimation = null;//定义一个String数组用于构造下拉列表的适配器private String str[] = {"平移动画", "透明度动画", "旋转动画", "缩放动画"};    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.main);        //分别从xml文件中得到每个控件        start = (Button) findViewById(R.id.startButton);        select = (Spinner) findViewById(R.id.select);        img = (ImageView) findViewById(R.id.img);        //实例化适配器        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, str);        select.setAdapter(adapter);        //为开始按钮设置监听        start.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {InitialAnimation();img.startAnimation(tAnimation);}});    }        //初始化动画    public void InitialAnimation(){     switch(select.getSelectedItemPosition()){    case 0:    tAnimation = new TranslateAnimation(0, 300, 50, 50);//    tAnimation = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, 1.0f, Animation.RELATIVE_TO_PARENT, -0.5f, Animation.RELATIVE_TO_PARENT, -0.5f);    break;    case 1:tAnimation = new AlphaAnimation(0.1f, 1.0f);    break;    case 2:tAnimation = new RotateAnimation(0.0f, +360.0f);    break;    case 3://    tAnimation = new ScaleAnimation(0.0f, 1.0f, 0.0f, 1.0f);    tAnimation = new ScaleAnimation(0.0f, 1.0f, 0.0f, 1.0f, 200.0f, 0.0f);    break;    }    //为动画设置完成所需时间    tAnimation.setDuration(2000);    }}
?

?

?

这里是main.xml

?

<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    >    <Spinner    android:id="@+id/select"    android:layout_width="fill_parent"    android:layout_height="wrap_content"    />    <Button    android:id="@+id/startButton"    android:layout_width="fill_parent"    android:layout_height="wrap_content"    android:layout_below="@id/select"    android:text="开始播放"    />    <ImageView    android:id="@+id/img"    android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:layout_alignParentBottom="true"    android:src="@drawable/sun"    /></RelativeLayout>
?

?

?

?

?

?