您的位置:首页 > 移动开发 > Android开发

android的动画之Tween动画

2014-11-09 15:48 218 查看
Tween动画 又叫"补间动画" 或者"中间动画".

Tween动画分为4种 即 AlphaAnimation(透明度动画)、TranslateAnimation(平移动画)、ScaleAnimation(缩放动画)、RotateAnimation(旋转动画)

从字面看就可以大概了解动画形式了、他们都继承自android.view.Animation类、实现方法有 Javacode(java源代码)、XML(xml配置文件)。

现在 、 我们一个个来解析吧。

AlphaAnimation(透明度动画):就是通过改变组件的透明度来实现动画效果的。

XML属性:

android:fromAlpha="0.1f " ------------fromAlpha为起始透明度 为Float型

android:toAlpha=“1.0f " -------------toAlpha为结束透明度 也为Float型

android:duration = ”4000“ ********属性为动画持续时间 4000毫秒

Java源代码:

AlphaAnimation有两个构造函数

1. AlphaAnimation(Context contex、AttributeSet attrs)第一参数为上下文环境、第二个参数是个属性集、用来自定义控件使用AttributeSet来完成控
件类 的构造函数,并在构造函数中将自定义控件类中变量与attrs.xml中的属性连接起来.

2.AlphaAnimation( float fromAlpha , flaot toAlpha) 这两个参数都为浮点型

TranteAnimation(平移动画)

有三个构造函数

1:TranteAnimation (Context context、AttributeSet attrs) 同上 略过

2:TrantAnimation (float fromXDelta, float toXDelta, float fromYDelta, float toYDelta)

fromXDelta:表示动画开始的点 与当前ViewX轴坐标的差值

toXDelta:表示动画结束的点 与当前ViewX轴坐标的差值

fromYDelta:表示动画开始的点
与当前ViewY轴坐标的差值

float toYDelta:表示动画结束的点 与当前ViewY轴坐标的差值

3:ScaleAnimation(缩放动画)

有四个构造函数

ScaleAnimation(Context context 、AttributeSet attrs) 同上 略过

ScaleAnimation(float fromX,float toX,float fromY,float toY)第一二个参数为X轴的起始 、结束坐标 第三四个参数为Y轴的起始、结束坐标

ScaleAnimation(float fromX, float toX, float fromY, float toY, float pivotX, float pivotY):前面四个不讲 后面两个

pivot 意思是枢轴 、支点 float pivotX, float pivotY 为动画相对于组件的X、Y坐标的开始位置、从0%~100%取值

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

4:RotateAnimation: 有四个构造函数

1:RotateAnimation(Context
Context 、 AttributeSet attrs) 同上 略过

2:RotateAnimation(float
fromdDegress 、 float toDegress) 参数为开始的角度 和结束的角度

例:RotateAnimation(10.0f,90.0f) 就是从10度旋转到90度

3:RotateAnimation(float
fromDegrees, float toDegrees, float pivotX, float pivotY) 参照上面讲解

RotateAnimation(float fromDegrees, float toDegrees, int pivotXType,
float pivotXValue, int pivotYType, float pivotYValue) 参照上面 不废话了

package com.android.my5;

import android.app.Activity;

import android.os.Bundle;

import android.view.Menu;

import android.view.MenuItem;

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 MainActivity extends Activity {

private Button start; //定义开始按钮

private Animation animation; //动画效果

private ImageView image; //实行动画的图片

private Spinner spinner; //定义动画类型下拉选项

String[] string = {"平移动画","透明度动画","旋转动画","缩放动画"}; //定义一个String数组组成下拉列表的选项

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

//分别通过Id获取组件

spinner = (Spinner)findViewById(R.id.spinner);

image = (ImageView)findViewById(R.id.image);

start = (Button)findViewById(R.id.button);

//定义一个适配器

ArrayAdapter adapter = new ArrayAdapter(MainActivity.this,android.R.layout.simple_spinner_item,string);

spinner.setAdapter(adapter); //spinner 设置适配器

//为start绑定监听器

start.setOnClickListener(new OnClickListener() {

public void onClick(View v) {

InitialAnimation();

image.startAnimation(animation);

}

});

}

//定义动画方法

public void InitialAnimation(){

switch(spinner.getSelectedItemPosition()){

case 0:

animation = new TranslateAnimation(0,0,120,300); //实例化动画为平移动画 第一个和第二个参数对映着X轴的开始 结束坐标 第三个和第四个对映着Y轴的

break;

case 1:

animation = new AlphaAnimation(0.1f,1.0f); //透明度动画 参数为FLoat型 开始透明度和最后透明度

break;

case 2:

//动画效果为旋转动画 参数为Float型 第一个参数为开始角度 第二个为最后角度

animation = new RotateAnimation(0, 10,150,150);

break;

case 3:

//动画效果为缩放动画 前两个参数为X轴的起始、结束坐标 后两个为Y轴的起始和结束坐标

animation = new ScaleAnimation(0.0f, 2.0f, 0.0f, 2.0f);

break;

}

animation.setDuration(4000); //设置动画完成需要的时间

}

}

xml文件

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

xmlns:tools="http://schemas.android.com/tools"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical"

tools:context="${packageName}.${activityClass}" >

<Spinner

android:id="@+id/spinner"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

/>

<Button

android:id="@+id/button"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:background="@drawable/button"

android:textColor="#f00"

android:shadowColor="#0f0"

android:text="开始播放"/>

<ImageView

android:id="@+id/image"

android:layout_width="150dp"

android:layout_height="150dp"

android:layout_gravity="center_horizontal"

android:src="@drawable/bar"

/>

</LinearLayout>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: