Android常用动画总结

来源:互联网 发布:钢材进销存软件免费版 编辑:程序博客网 时间:2024/06/05 02:38

Android中常用的动画:
(1)帧动画:
例如加载的dialog,音量大小的效果等。就像放电影一样,一帧一帧的,通过不停的切换图片达到动画的效果,很简单,看代码:

<?xml version="1.0" encoding="utf-8"?><animation-list xmlns:android="http://schemas.android.com/apk/res/android">    <item        android:drawable="@drawable/chatfrom_voice_playing_f1"        android:duration="200" />    <item        android:drawable="@drawable/chatfrom_voice_playing_f2"        android:duration="200" />    <item        android:drawable="@drawable/chatfrom_voice_playing_f3"        android:duration="60" /></animation-list>

在res\anim目录下建立动画文件voice.xml。
使用:
在布局中如下:

 <ImageButton    android:id="@+id/animation1"    android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:background="@anim/voice" />

开启动画:

ImageButton btn = (ImageButton) findViewById(R.id.animation1);AnimationDrawable animationDrawable = (AnimationDrawable) btn.getBackground();animationDrawable.start();

(2)补件动画:
针对View的旋转,平移,缩放,透明度。记录如下:
res\anim\animation.xml

<?xml version="1.0" encoding="utf-8"?><set xmlns:android="http://schemas.android.com/apk/res/android"    android:fillAfter="true">    <scale        android:duration="@android:integer/config_shortAnimTime"        android:fromXScale="1.0"        android:fromYScale="1.0"        android:interpolator="@android:interpolator/anticipate_overshoot"        android:pivotX="50%"        android:pivotY="50%"        android:toXScale="1.5"        android:toYScale="1.5" />    <scale        android:duration="@android:integer/config_shortAnimTime"        android:fromXScale="1"        android:fromYScale="1"        android:pivotX="50%"        android:pivotY="50%"        android:startOffset="@android:integer/config_shortAnimTime"        android:toXScale="0.6"        android:toYScale="0.6" />    <alpha        android:duration="@android:integer/config_shortAnimTime"        android:fromAlpha="0"        android:interpolator="@android:interpolator/anticipate_overshoot"        android:toAlpha="1" />    <translate        android:duration="@android:integer/config_shortAnimTime"        android:fromXDelta="0"        android:fromYDelta="0"        android:interpolator="@android:interpolator/anticipate_overshoot"        android:toXDelta="80%p"        android:toYDelta="-100%" /></set>

set:集合的意思,容纳多个动画,是他们同时执行。
duration:动画时长,fromXXX,toXXX就是某个属性的起始值和要达到的值。
比如translate中的android:fromXDelta=”0”,android:toXDelta=”80%p”就是水平方向移动,fromXDelta=”0”:起始值为0,就是自身所在的位置,toXDelta=”80%p”,该View所在父view的宽的80%,p为parent,如果没有p就是相对与自身。View将水平方向移动父View宽度的80%。
对scale,rotate注意需要制定变换的中心点。否则默认(0,0)点,即View的左上角。
android:pivotX=”50%”
android:pivotY=”50%”
意思是中心点的位置,50%就是View的中心。
interpolator:差值器。就是一个函数,不如sin,cos。等等。
android:startOffset:动画开始的延时。
android:fillAfter=”true”:动画结束后,view停留在动画的最后一帧。默认是停留在第一帧。
再来看一个:

<?xml version="1.0" encoding="utf-8"?><rotate xmlns:android="http://schemas.android.com/apk/res/android"    android:duration="600"    android:interpolator="@android:anim/linear_interpolator"    android:fromDegrees="0.0"    android:toDegrees="359"    android:pivotX="50%"    android:pivotY="50%"    android:repeatCount="infinite"    />

看使用:

ImageView img2 = (ImageView) findViewById(R.id.animation2);Animation aniamtion = AnimationUtils.loadAnimation(this, R.anim.test);img2.startAnimation(aniamtion);

ok,补件动画结束。
(3)属性动画
在Android3.0后,推出了属性动画,取代补件动画。重点.
ValueAniamtion,ObjectAniamtion。
郭神有经典的博客。
一个简单的使用。

    private void startAniamtion(View view, int distance) {        AnimatorSet sets = new AnimatorSet().setDuration(1000);        ObjectAnimator yAnimator = new ObjectAnimator();        yAnimator.setProperty(View.TRANSLATION_Y);        yAnimator.setInterpolator(new OvershootInterpolator());        yAnimator.setFloatValues(0, distance);        yAnimator.setTarget(view);        addLayerListener(yAnimator, view);        ObjectAnimator alphaAniamtor = new ObjectAnimator();        alphaAniamtor.setProperty(View.ALPHA);        alphaAniamtor.setFloatValues(0, 1);        alphaAniamtor.setTarget(view);        sets.play(yAnimator).with(alphaAniamtor);        sets.start();    }

至此。
Android中的动画主要就这些,对于View的一些变换还有矩阵,Matrix。找时间总结一下。

0 0
原创粉丝点击