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

android Animations 动画效果(一)

2014-03-10 14:51 387 查看
Animations提供了一系列的动画效果,这些效果可以应用在绝大多数的控件上

Animations可以分为两大类:

第一类:Tweened Animations

该类Animations提供了旋转,移动,伸展和淡出效果

第二类:Frame-by-Frame Animations

这个类Animations可以创建一个Drawable序列,这些Drawable可以按照指定的时间间歇一个一个的显示

Tweened Animations的分类

1.Alpha:淡入淡出效果

2.Scale:缩放效果

3.Rotate:旋转效果

4.Translate:移动效果

使用Tweened Animations的步骤

1.创建一个AnimationSet对象

2.根据需要创建相应的Animation对象

3.根据软件动画的需要,为Animation对象设置相应的数据

4.将Animation对象添加到AnimationSet对象中

5.使用控件对象开始执行AnimationSet

Tween Animations的通用属性

//设置动画执行的时间为1s

animationSet.setDuration(1000);

//如果为true,执行完动画后,停留到执行结束的时候

animationSet.setFillAfter(true);

//如果为true,执行完动画后,停留到执行开始的时候

animationSet.setFillBefore(false);

//执行前停留的时间(毫秒)

animationSet.setStartOffset(1000);

//执行次数

animationSet.setRepeatCount(2);

下面是例子程序

activity_main.xml

<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"

android:paddingLeft="@dimen/activity_horizontal_margin"

android:paddingRight="@dimen/activity_horizontal_margin"

android:paddingTop="@dimen/activity_vertical_margin"

tools:context=".MainActivity" >

<Button

android:id="@+id/scaleButton"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:layout_alignParentBottom="true"

android:text="测试scale动画效果"

/>

<Button

android:id="@+id/rotateButton"

android:layout_above="@id/scaleButton"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:text="测试rotate动画效果"

/>

<Button

android:id="@+id/alphaButton"

android:layout_above="@id/rotateButton"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:text="测试Alpha动画效果"

/>

<Button

android:id="@+id/translateButton"

android:layout_above="@id/alphaButton"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:text="测试Translate动画效果"

/>

<LinearLayout

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:layout_alignLeft="@+id/scaleButton"

android:layout_alignParentTop="true"

android:orientation="vertical" >

<ImageView

android:id="@+id/imageViewId"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_margin="50px"

android:src="@drawable/ic_launcher" />

</LinearLayout>

</RelativeLayout>

MainActivity.java

package com.yx.animations01;

import android.os.Bundle;

import android.app.Activity;

import android.view.Menu;

import android.view.View;

import android.view.View.OnClickListener;

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.Button;

import android.widget.ImageView;

public class MainActivity extends Activity {

private Button scaleButton=null;

private Button rotateButton=null;

private Button alphaButton=null;

private Button translateButton=null;

private ImageView imageView = null;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);



scaleButton = (Button) findViewById(R.id.scaleButton);

scaleButton.setOnClickListener(new scaleButtonListener());



rotateButton = (Button) findViewById(R.id.rotateButton);

rotateButton.setOnClickListener(new rotateButtonListener());



alphaButton = (Button) findViewById(R.id.alphaButton);

alphaButton.setOnClickListener(new alphaButtonListener());



translateButton = (Button) findViewById(R.id.translateButton);

translateButton.setOnClickListener(new translateButtonListener());



imageView = (ImageView) findViewById(R.id.imageViewId);

}

class scaleButtonListener implements OnClickListener{

@Override

public void onClick(View v) {

// 创建一个AnimationSet对象

AnimationSet animationSet = new AnimationSet(true);

/*Parameters

fromX Horizontal scaling factor to apply at the start of the animation

toX Horizontal scaling factor to apply at the end of the animation

fromY Vertical scaling factor to apply at the start of the animation

toY Vertical scaling factor to apply at the end of the animation

pivotXType Specifies how pivotXValue should be interpreted. One of Animation.ABSOLUTE, Animation.RELATIVE_TO_SELF, or Animation.RELATIVE_TO_PARENT.

pivotXValue The X coordinate of the point about which the object is being scaled, specified as an absolute number where 0 is the left edge. (This point remains fixed while the object changes size.) This value can either be an absolute number if pivotXType
is ABSOLUTE, or a percentage (where 1.0 is 100%) otherwise.

pivotYType Specifies how pivotYValue should be interpreted. One of Animation.ABSOLUTE, Animation.RELATIVE_TO_SELF, or Animation.RELATIVE_TO_PARENT.

pivotYValue The Y coordinate of the point about which the object is being scaled, specified as an absolute number where 0 is the top edge. (This point remains fixed while the object changes size.) This value can either be an absolute number if pivotYType is
ABSOLUTE, or a percentage (where 1.0 is 100%) otherwise.

*/

ScaleAnimation scaleAnimation = new ScaleAnimation(1,0.1f,1,0.1f,

Animation.RELATIVE_TO_SELF,0.5f,

Animation.RELATIVE_TO_SELF,0.5f

);





//设置动画执行的时间为1s

scaleAnimation.setDuration(1000);

//将alphaAnimation添加到animationSet中

animationSet.addAnimation(scaleAnimation);

//imageView的setAnimation(animationSet)执行动画

imageView.setAnimation(animationSet);



}

}



class rotateButtonListener implements OnClickListener{

@Override

public void onClick(View v) {

// 创建一个AnimationSet对象

AnimationSet animationSet = new AnimationSet(true);

/**

* Parameters

fromDegrees Rotation offset to apply at the start of the animation.

toDegrees Rotation offset to apply at the end of the animation.

pivotXType Specifies how pivotXValue should be interpreted. One of Animation.ABSOLUTE, Animation.RELATIVE_TO_SELF, or Animation.RELATIVE_TO_PARENT.

pivotXValue The X coordinate of the point about which the object is being rotated, specified as an absolute number where 0 is the left edge. This value can either be an absolute number if pivotXType is ABSOLUTE, or a percentage (where 1.0 is 100%) otherwise.

pivotYType Specifies how pivotYValue should be interpreted. One of Animation.ABSOLUTE, Animation.RELATIVE_TO_SELF, or Animation.RELATIVE_TO_PARENT.

pivotYValue The Y coordinate of the point about which the object is being rotated, specified as an absolute number where 0 is the top edge. This value can either be an absolute number if pivotYType is ABSOLUTE, or a percentage (where 1.0 is 100%) otherwise.


*/

//1.从哪个角度2.到那个角度3.x轴坐标的类型可取值Animation.ABSOLUTE, Animation.RELATIVE_TO_SELF, or Animation.RELATIVE_TO_PARENT

//4.其中1f表示一个父控件的横坐标5-6同3-4

RotateAnimation rotateAnimation = new RotateAnimation(0,360,

Animation.RELATIVE_TO_PARENT,1f,

Animation.RELATIVE_TO_PARENT,0f

);

rotateAnimation.setDuration(1000);

animationSet.addAnimation(rotateAnimation);

//imageView的setAnimation(animationSet)执行动画

imageView.setAnimation(animationSet);



}

}



//淡入淡出

class alphaButtonListener implements OnClickListener{

@Override

public void onClick(View v) {

// 创建一个AnimationSet对象

AnimationSet animationSet = new AnimationSet(true);

//创建一个AlphaAnimation对象括号为从什么透明度到什么样的透明度1为不透明,0为透明

AlphaAnimation alphaAnimation = new AlphaAnimation(1,0);

//设置动画执行的时间为1s

alphaAnimation.setDuration(1000);

//将alphaAnimation添加到animationSet中

animationSet.addAnimation(alphaAnimation);

//imageView的setAnimation(animationSet)执行动画

imageView.setAnimation(animationSet);

}

}



class translateButtonListener implements OnClickListener{

@Override

public void onClick(View v) {

// 创建一个AnimationSet对象

AnimationSet animationSet = new AnimationSet(true);

/*

* Parameters

fromXType Specifies how fromXValue should be interpreted. One of Animation.ABSOLUTE, Animation.RELATIVE_TO_SELF, or Animation.RELATIVE_TO_PARENT.

fromXValue Change in X coordinate to apply at the start of the animation. This value can either be an absolute number if fromXType is ABSOLUTE, or a percentage (where 1.0 is 100%) otherwise.

toXType Specifies how toXValue should be interpreted. One of Animation.ABSOLUTE, Animation.RELATIVE_TO_SELF, or Animation.RELATIVE_TO_PARENT.

toXValue Change in X coordinate to apply at the end of the animation. This value can either be an absolute number if toXType is ABSOLUTE, or a percentage (where 1.0 is 100%) otherwise.

fromYType Specifies how fromYValue should be interpreted. One of Animation.ABSOLUTE, Animation.RELATIVE_TO_SELF, or Animation.RELATIVE_TO_PARENT.

fromYValue Change in Y coordinate to apply at the start of the animation. This value can either be an absolute number if fromYType is ABSOLUTE, or a percentage (where 1.0 is 100%) otherwise.

toYType Specifies how toYValue should be interpreted. One of Animation.ABSOLUTE, Animation.RELATIVE_TO_SELF, or Animation.RELATIVE_TO_PARENT.

toYValue Change in Y coordinate to apply at the end of the animation. This value can either be an absolute number if toYType is ABSOLUTE, or a percentage (where 1.0 is 100%) otherwise.

*/

TranslateAnimation translateAnimation = new TranslateAnimation(

Animation.RELATIVE_TO_SELF,0f,

Animation.RELATIVE_TO_SELF,0.5f,

Animation.RELATIVE_TO_SELF,0f,

Animation.RELATIVE_TO_SELF,1.0f

);

//设置动画执行的时间为1s

animationSet.setDuration(1000);

animationSet.setFillAfter(true);

animationSet.setFillBefore(false);

animationSet.setStartOffset(1000);

animationSet.setRepeatCount(2);



//将alphaAnimation添加到animationSet中

animationSet.addAnimation(translateAnimation);

//imageView的setAnimation(animationSet)执行动画

imageView.setAnimation(animationSet);



}

}



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