android 动画介绍

来源:互联网 发布:php互助金融 编辑:程序博客网 时间:2024/06/05 21:49

Tween动画:

Animation中文翻译就是动画的意思,android提供的该动画抽象类可以在View ,Surface或者Object中应用,来实现简单的一些动画效果。

它的直接子类中,除了AnimationSet,其他的四个子类就是可以实现的动画效果了,渐变,缩放,移动和旋转。


setDuration():动画运行时间内,以毫秒为单位

setFillAfter():设置为true,动画转换在动画结束的时候应用

setFillBefore():设置为true,动画转化在动画开始前应用

setInterpolator():指定一个动画插入器

setRepeatCount():动画重复次数

setRepeatMode():动画重复行为,1:重新开始 2:回退

setStartOffset():动画之间的时间间隔,从上次动画停多少时间开始执行下个动画

setZAdjustment():允许在动画播放期间,调整播放内容在Z轴方向的顺序,0:保持不变。 1:top,动画播放期间,强制把当前内容放到其他内容上面。 -1:bottom,在动画播放期间,强制把当前内容不放到其他内容下面。


AnimationSet:

Animation用来实现Tween动画,而动画的使用放松也分为两种,一种是在Java代码中,使用AnimationSet动画集合类来控制多个动画的组合。另外一种就是在res/anim中,建立set文件,在xml里面添加动画效果。在代码中动态的添加具有更高的灵活性,而xml设置具有更好的复用性,不用每次使用同样的效果都得重新写一堆相同的代码。


Alpha透明度效果:

控制透明度等级,可以在在渐进渐出效果上使用。

 AlphaAnimation(float fromAlpha, float toAlpha)Constructor to use when building an AlphaAnimation from code
其他的设置是继承自父类Animation的,它本身需要设置的就是起始的透明度了。

xml set,这里除了起始的参数,其他比如持续时间 加速器等都没有设置,看去来几乎及没有动画效果,参考Animation的属性,添加更多的效果试试:

<set xmlns:android="http://schemas.android.com/apk/res/android">    <alpha        android:fromAlpha="1"        android:toAlpha="0" /></set>
给图片添加动画效果:

 Animation anim = AnimationUtils.loadAnimation(mContext, R.anim.anim_test);        ImageView img = (ImageView) findViewById(R.id.img_test);        img.startAnimation(anim);
java中动态设置:

  //构造方法 设置起始的透明度        Animation anim = new AlphaAnimation(1, 0);//        anim.setDuration(5000);        ImageView img = (ImageView) findViewById(R.id.img_test);        img.startAnimation(anim);

rotate旋转效果:
控制旋转,这种旋转发生在x-y平面上,你可以指定旋转的中心点,(0,0)点在左上角,它也是默认的旋转点。

 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 codeRotateAnimation(float fromDegrees, float toDegrees, int pivotXType, float pivotXValue, int pivotYType, float pivotYValue)Constructor to use when building a RotateAnimation from code

fromDegress:动画开始时物体角度。

toDegress:动画结束时物体角度。

说明:

角度为负,表示逆时针旋转。角度为正数,表示顺时针旋转。


pivotXType和pivotYType这两个参数,他们是动画相对于物体位置类型,与pivotXValue和pivotYValue结合使用,以确定x ,y轴上选择中心。

pivotXType=Animation.ABSOLUTE,则此参数为旋转中心在屏幕上X轴的值;pivotXType=Animation.RELATIVE_TO_PARENT,则参数pivotXValue为旋转中心在父控件水平位置百分比,如0.5表示在父控件水平方向中间位置pivotXType=Animation.RELATIVE_TO_SELF,则参数pivotXValue为旋转中心在控件自身水平位置百分比,如果X和Y的Value都设置为0.5,则该控件以自身中心旋转。

Y轴上同理。

点位置也可以使用百分比数值:
从0%-100%中取值,50%为物件的X或Y方向坐标上的中点位置。


scale缩放效果:

控制缩放,可以指定缩放的中心点。

 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 codeScaleAnimation(float fromX, float toX, float fromY, float toY, float pivotX, float pivotY)Constructor to use when building a ScaleAnimation from codeScaleAnimation(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
参数:

fromX Horizontal scaling factor to apply at the start of the animationtoX Horizontal scaling factor to apply at the end of the animationfromY Vertical scaling factor to apply at the start of the animationtoY Vertical scaling factor to apply at the end of the animationpivotXType 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. 
fromX:开始动画时 水平X轴缩放尺寸

fromY:开始动画时 垂直Y轴缩放尺寸

toX:结束动画时 水平X轴缩放尺寸

toY:结束动画时 垂直Y轴缩放尺寸

说明:

0.0 :表示缩放到没有。1.0:表示正常无伸缩。

数值小于1.0 表示收缩,数值大于1.0表示放大。

设置缩放中心点位置,可以参考上面的旋转中心位置,这里就不再重复了。


translate移动效果:

控制移动位置效果。

 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 codeTranslateAnimation(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
参数:

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. 

AnimationUtils,为动画定义共同的效果。

static long currentAnimationTimeMillis()Returns the current animation time in milliseconds.static Animation loadAnimation(Context context, int id)Loads an Animation object from a resourcestatic Interpolator loadInterpolator(Context context, int id)Loads an Interpolator object from a resourcestatic LayoutAnimationController loadLayoutAnimation(Context context, int id)static Animation makeInAnimation(Context c, boolean fromLeft)Make an animation for objects becoming visible.static Animation makeInChildBottomAnimation(Context c)Make an animation for objects becoming visible.static Animation makeOutAnimation(Context c, boolean toRight)Make an animation for objects becoming invisible.

startOffset,控制动画的执行顺序,如果不设置就默认为0。

<?xml version="1.0" encoding="utf-8"?><set xmlns:android="http://schemas.android.com/apk/res/android">    <scale        android:fromXScale="0.5"        android:fromYScale="0.5"        android:toXScale="1.5"        android:toYScale="1.5"        android:pivotX="50%"        android:pivotY="50%"        android:duration="5000"        android:startOffset="0" />    <rotate        android:fromDegrees="0"        android:toDegrees="180"        android:pivotX="50%"        android:pivotY="50%"        android:duration="5000"        android:startOffset="5000" />    <translate        android:fromXDelta="0"        android:fromYDelta="0"        android:toXDelta="200"        android:toYDelta="200"        android:duration="5000"        android:startOffset="10000" />    <alpha        android:fromAlpha="1"        android:toAlpha="0"        android:duration="5000"        android:startOffset="15000" /></set>
注意:

<set xmlns:android="http://schemas.android.com/apk/res/android">    <scale        android:fromXScale="0.5"        android:fromYScale="0.5"        android:toXScale="1.5"        android:toYScale="1.5"        android:pivotX="50%"        android:pivotY="50%"        android:duration="5000" />    <translate        android:fromXDelta="0"        android:fromYDelta="0"        android:toXDelta="200"        android:toYDelta="200"        android:duration="5000" />    <rotate        android:fromDegrees="0"        android:toDegrees="180"        android:pivotX="50%"        android:pivotY="50%"        android:duration="5000" />    <alpha        android:fromAlpha="1"        android:toAlpha="0"        android:duration="5000" /></set>
rotate和translate的顺序更改了,动画效果也会变化。如果rotate放在translate的后面,rotate的旋转中心点不会随着动画而不断改变,就是初始的位置。

Interpolator内插器:
给动画定义改变的频率,运行基本的动画效果被加速,减速重复等。

AccelerateDecelerateInterpolator     在动画开始与介绍的地方速率改变比较慢,在中间的时候加速
AccelerateInterpolator     在动画开始的地方速率改变比较慢,然后开始加速
CycleInterpolator     动画循环播放特定的次数,速率改变沿着正弦曲线
DecelerateInterpolator     在动画开始的地方速率改变比较慢,然后开始减速
LinearInterpolator     在动画的以均匀的速率改变


Frame动画:

跟补间动画不同的是,帧动画跟放电影类似,是顺序播放播放事先准备好的图像,通常都是多张图片,资源放在res/drawable下面。android sdk提供AnimationDrawable来定义,也可以xml或者编码实现。

xml:

<?xml version="1.0" encoding="utf-8"?><animation-list xmlns:android="http://schemas.android.com/apk/res/android"    android:oneshot="true">    <item        android:drawable="@drawable/keyboard_zero"        android:duration="1000" />    <item        android:drawable="@drawable/keyboard_one"        android:duration="1000" />    <item        android:drawable="@drawable/keyboard_two"        android:duration="1000" />    <item        android:drawable="@drawable/keyboard_three"        android:duration="1000" />    <item        android:drawable="@drawable/keyboard_four"        android:duration="1000" /></animation-list>
在java中设置:

  private void init() {        mContext = this;        img = (ImageView) findViewById(R.id.img_test);        img.setBackgroundResource(R.drawable.anim_frame_test);    }    /**     * 可以交互的时候 开始动画     * @param hasFocus     */    @Override    public void onWindowFocusChanged(boolean hasFocus) {        super.onWindowFocusChanged(hasFocus);        if (hasFocus) {            drawable = (AnimationDrawable) img.getBackground();            drawable.start();        }    }
编码:

 private void init() {        mContext = this;        img = (ImageView) findViewById(R.id.img_test);        addDrawable();        img.setBackgroundDrawable(drawable);    }    private void addDrawable() {        drawable = new AnimationDrawable();        drawable.setOneShot(true);        drawable.addFrame(getResources().getDrawable(R.drawable.keyboard_zero), 1000);        drawable.addFrame(getResources().getDrawable(R.drawable.keyboard_one), 1000);        drawable.addFrame(getResources().getDrawable(R.drawable.keyboard_two), 1000);        drawable.addFrame(getResources().getDrawable(R.drawable.keyboard_three), 1000);        drawable.addFrame(getResources().getDrawable(R.drawable.keyboard_four), 1000);    }    /**     * 可以交互的时候 开始动画     *     * @param hasFocus     */    @Override    public void onWindowFocusChanged(boolean hasFocus) {        super.onWindowFocusChanged(hasFocus);        if (hasFocus) {            drawable = (AnimationDrawable) img.getBackground();            drawable.start();        }    }

0 0
原创粉丝点击