成佩涛编程之路——Android控件动画效果(一)

Animation的4个基本动画效果

What is Animation?

public abstract class
Animation
extends Object
implements Cloneable

Abstraction for an Animation that can be applied to Views, Surfaces, or other objects.

1、AlphaAnimation:淡入淡出效果
public class
AlphaAnimation
extends Animation

An animation that controls the alpha level of an object. Useful for fading things in and out. This animation ends up changing the alpha property of aTransformation

Public Constructors
AlphaAnimation(Context context, AttributeSet attrs)
Constructor used when an AlphaAnimation is loaded from a resource.
AlphaAnimation(float fromAlpha, float toAlpha)
Constructor to use when building an AlphaAnimation from code
public class
AnimationSet
extends Animation

Represents a group of Animations that should be played together. The transformation of each individual animation are composed together into a single transform. If AnimationSet sets any properties that its children also set (for example, duration or fillBefore), the values of AnimationSet override the child values.


在代码中实现动画效果的方法:

ImageView imageView = (ImageView) findViewById(R.id.imageView1); AnimationSet animationSet = new AnimationSet(true); AlphaAnimation alphaAnimation = new AlphaAnimation(0, 1); alphaAnimation.setDuration(1000); alphaAnimation.setStartOffset(10000); animationSet.addAnimation(alphaAnimation); //animationSet.setStartOffset(10000); animationSet.setFillBefore(false); animationSet.setFillAfter(true); imageView.startAnimation(animationSet);


在XML文件中实现动画效果的方法:

① 在res目录下创建一个anim文件夹,在里面添加一个alpha.xml文件:

<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" android:interpolator="@android:anim/accelerate_interpolator" android:fillAfter="true" android:fillBefore="false"> <alpha android:fromAlpha="1.0" android:toAlpha="0.0" android:startOffset="1000" android:duration="1000" /> </set>

② 在Activity中使用AnimationUtils获取Animation并进行设置:

Animation animation = AnimationUtils.loadAnimation(MainActivity.this, R.anim.alpha); imageView.startAnimation(animation);
2、ScaleAnimation:缩放效果
public class
ScaleAnimation
extends Animation

An animation that controls the scale of an object. You can specify the point to use for the center of scaling.

Public Constructors
ScaleAnimation(Context context, AttributeSet attrs)
Constructor used when a ScaleAnimation is loaded from a resource.
ScaleAnimation(float fromX, float toX, float fromY, float toY)
Constructor to use when building a ScaleAnimation from code
ScaleAnimation(float fromX, float toX, float fromY, float toY, float pivotX, float pivotY)
Constructor to use when building a ScaleAnimation from code
ScaleAnimation(float fromX, float toX, float fromY, float toY, int pivotXType, float pivotXValue, int pivotYType, float pivotYValue)
Constructor to use when building a ScaleAnimation from code


在代码中实现动画效果:

ImageView imageView = (ImageView) findViewById(R.id.imageView1); AnimationSet animationSet = new AnimationSet(true); ScaleAnimation scaleAnimation = new ScaleAnimation(1, 0.5f, 1, 0.5f, Animation.RELATIVE_TO_SELF, 1f, Animation.RELATIVE_TO_SELF, 1f); animationSet.addAnimation(scaleAnimation); animationSet.setDuration(1000); imageView.startAnimation(animationSet);


在XML文件中实现动画效果的方法:

① 在res的anim文件夹下,创建一个scale.xml文件:

<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" android:interpolator="@android:anim/accelerate_interpolator"> <scale android:fromXScale="1.0" android:toXScale="0.0" android:fromYScale="1.0" android:toYScale="0.0" android:pivotX="50%" android:pivotY="50%" android:duration="2000" /> </set>

② 在Activity中使用AnimationUtils获取Animation并进行设置:

Animation animation = AnimationUtils.loadAnimation(MainActivity.this, R.anim.scale); imageView.startAnimation(animation);
3、Rotate:旋转效果
public class
RotateAnimation
extends Animation

An animation that controls the rotation of an object. This rotation takes place int the X-Y plane. You can specify the point to use for the center of the rotation, where (0,0) is the top left point. If not specified, (0,0) is the default rotation point.

Public Constructors
RotateAnimation(Context context, AttributeSet attrs)
Constructor used when a RotateAnimation is loaded from a resource.
RotateAnimation(float fromDegrees, float toDegrees)
Constructor to use when building a RotateAnimation from code.
RotateAnimation(float fromDegrees, float toDegrees, float pivotX, float pivotY)
Constructor to use when building a RotateAnimation from code
RotateAnimation(float fromDegrees, float toDegrees, int pivotXType, float pivotXValue, int pivotYType, float pivotYValue)
Constructor to use when building a RotateAnimation from code


在代码中实现动画效果:

ImageView imageView = (ImageView) findViewById(R.id.imageView1); AnimationSet animationSet = new AnimationSet(true); RotateAnimation rotateAnimation = new RotateAnimation(0, 360, Animation.RELATIVE_TO_PARENT, 0.5f, Animation.RELATIVE_TO_PARENT, 0.5f); rotateAnimation.setDuration(1000); animationSet.addAnimation(rotateAnimation); imageView.startAnimation(animationSet);


在XML文件中实现动画效果的方法:

① 在res的anim文件夹下,创建一个rotate.xml文件:

<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" android:interpolator="@android:anim/accelerate_interpolator"> <rotate android:fromDegrees="0" android:toDegrees="+360" android:pivotX="50%" android:pivotY="50%" android:duration="1000" /> </set>

② 在Activity中使用AnimationUtils获取Animation并进行设置:

Animation animation = AnimationUtils.loadAnimation(MainActivity.this, R.anim.rotate); imageView.startAnimation(animation);
4、 Translate:移动效果
public class
TranslateAnimation
extends Animation

An animation that controls the position of an object.

Public Constructors
TranslateAnimation(Context context, AttributeSet attrs)
Constructor used when a TranslateAnimation is loaded from a resource.
TranslateAnimation(float fromXDelta, float toXDelta, float fromYDelta, float toYDelta)
Constructor to use when building a TranslateAnimation from code
TranslateAnimation(int fromXType, float fromXValue, int toXType, float toXValue, int fromYType, float fromYValue, int toYType, float toYValue)
Constructor to use when building a TranslateAnimation from code


在代码中实现动画效果:

ImageView imageView = (ImageView) findViewById(R.id.imageView1); AnimationSet animationSet = new AnimationSet(true); TranslateAnimation translateAnimation = new TranslateAnimation( Animation.RELATIVE_TO_SELF, 0f, Animation.RELATIVE_TO_SELF, 1.0f, Animation.RELATIVE_TO_SELF, 0f, Animation.RELATIVE_TO_SELF, 1.0f); translateAnimation.setDuration(1000); animationSet.addAnimation(translateAnimation); imageView.startAnimation(animationSet);


在XML文件中实现动画效果的方法:

① 在res的anim文件夹下,创建一个translate.xml文件:

<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" android:interpolator="@android:anim/accelerate_interpolator"> <translate android:fromXDelta="0%p" android:toXDelta="100%p" android:fromYDelta="0%p" android:toYDelta="100%p" android:duration="1000" /> </set>

其中100%p表示相对于父空间的位置

② 在Activity中使用AnimationUtils获取Animation并进行设置:

Animation animation = (Animation) AnimationUtils.loadAnimation(MainActivity.this, R.anim.translate); imageView.startAnimation(animation);
也可以使用AnimationSet为一个控件添加多个动画,或者在xml文件中添加多个动画标签,以下分别使用代码和XML文件实现相同的效果:
代码中实现:
AnimationSet animationSet = new AnimationSet(false); AlphaAnimation alpha = new AlphaAnimation(1.0f, 0.0f); ScaleAnimation scale = new ScaleAnimation(1, 0.5f, 1, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f); animationSet.addAnimation(alpha); animationSet.addAnimation(scale); animationSet.setDuration(2000); animationSet.setStartOffset(1000); animationSet.setFillAfter(true); imageView.startAnimation(animationSet);
XML实现:

alpha.xml

<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" android:interpolator="@android:anim/accelerate_interpolator" android:shareInterpolator="true" android:fillAfter="true"> <alpha android:fromAlpha="1.0" android:toAlpha="0.0" android:startOffset="1000" android:fillAfter="true" android:duration="2000" /> <scale android:fromXScale="1.0" android:toXScale="0.5" android:fromYScale="1.0" android:toYScale="0.5" android:pivotX="50%" android:pivotY="50%" android:startOffset="1000" android:duration="2000" /> </set>

Activity中的代码:

Animation animation = AnimationUtils.loadAnimation(MainActivity.this, R.anim.alpha); imageView.startAnimation(animation);
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值