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

Android Animations

2015-09-11 16:36 387 查看
<span style="font-family: Arial, Helvetica, sans-serif;">	</span><span style="font-family: Arial, Helvetica, sans-serif; color: rgb(102, 102, 102);">星期五下午不太适合上班,完全没有工作动力,加上这环境,太困了,谢谢blog提提神。前几天项目要求弄动画,这儿就总结一下吧</span>


Android动画目前分为三类:

Tweened(补件动画)

效果:对View进行移动、缩放、旋转及透明度。

Frame(帧动画)

效果:在不同时间切换不同图片。

Property(属性动画)

效果:可以实现自由操作。

一、Tweened(补件动画)

创建动画,可以直接用java写,也可以在anim/**.xml中配置好,然后在用java加载xml创建。

Animation animation = AnimationUtils.loadAnimation(this,
R.anim.tweened_alpha);
<span style="white-space:pre">	</span>view.startAnimation(animation);


下面是Tweened相关动画说明

1、Translate:移动相关属性及说明

<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="3000"
android:fromXDelta="0%p"
android:fromYDelta="0%"
android:toXDelta="90%p"
android:toYDelta="0%"
android:interpolator="@android:anim/decelerate_interpolator"
android:repeatCount="1"
android:repeatMode="reverse"
android:startOffset="200" >

</translate>
<!--
translate-位移动画
duration-动画运行时间(毫秒ms)
fromXDelta-动画X开始位置(百分比、具体值)注: android:fromXDelta="80%p"父容器的80%(p=parent)
toXDelta-动画X结束位置(百分比、具体值)
fromYDelta-动画Y开始位置(百分比、具体值)
toYDelta-动画Y结束位置(百分比、具体值)
shareInterpolator-是否恭喜插入器
interpolator-加速器(插补器)
@android:anim/accelerate_interpolator: 越来越快
@android:anim/decelerate_interpolator:越来越慢
@android:anim/accelerate_decelerate_interpolator:先快后慢
@android:anim/anticipate_interpolator: 先后退一小步然后向前加速
@android:anim/overshoot_interpolator:快速到达终点超出一小步然后回到终点
@android:anim/anticipate_overshoot_interpolator:到达终点超出一小步然后回到终点
@android:anim/bounce_interpolator:到达终点产生弹球效果,弹几下回到终点
@android:anim/linear_interpolator:均匀速度。
fillEnabled-设置fillAfter&fillBefore的值,一旦设置fillAfter&fillBefore将无效
fillBefore-该动画转化是否在动画开始前被应用 ,
fillAfter-该动画转化是否在动画开始前被应用, 当设置为true ,该动画转化在动画结束后被应用
repeatCount-设置重复次数
repeatMode-设置重复模式(reverse:反方向,restart:正方向)0,不循环,1一次,-1一直循环
startOffset-延迟*(毫秒ms)执行动画
zAdjustment-定义动画的Z Order的改变
normal:保持Z Order不变
top:保持在最上层
bottom:保持在最下层
detachWallpaper-表示是否在壁纸上运行
-->


2、scale:放大缩小

<?xml version="1.0" encoding="utf-8"?>
<scale xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="3000"
android:fromXScale="0%"
android:fromYScale="0%"
android:pivotX="0%"
android:pivotY="0%"
android:toXScale="200%"
android:toYScale="200%" >
</scale>
<!--
scale-缩放动画
duration-动画运行时间
fromXScale-动画X开始放大比例
toXScale-动画X结束放大比例
fromYScale-动画Y开始放大比例
toYScale-动画Y开结束大比例
pivotX-缩放动画X中心点
pivotY-缩放动画Y中心点
interpolator-加速器(插补器)具体详见tweened_translate.xml
其他属性-详见见tweened_translate.xml
-->


3、rotate:旋转

<?xml version="1.0" encoding="utf-8"?>
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="3000"
android:fromDegrees="0"
android:pivotX="100%"
android:pivotY="100%"
android:toDegrees="-360" >

</rotate><!--
rotate-旋转动画
fromDegrees-开始旋转度数(0-360)
toDegrees-+顺时针,-逆时针,值可以大于360
其他属性详见tweened_translate.xml

-->


4、alpha:透明度

<?xml version="1.0" encoding="utf-8"?>
<alpha xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="3000"
android:fromAlpha="1"
android:toAlpha="0" >

</alpha>
<!--
alpha-渐变
fromAlpha-1。0完全不透明
toAlpha-0.0完全透明
-->


5、set:动画集

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:fillAfter="true"
android:shareInterpolator="false" >

<translate
android:duration="3000"
android:fillEnabled="true"
android:fromXDelta="0%p"
android:interpolator="@android:anim/accelerate_decelerate_interpolator"
android:toXDelta="30%p" />

<scale
android:duration="3000"
android:fillEnabled="true"
android:fromXScale="100%"
android:fromYScale="100%"
android:toXScale="50%"
android:toYScale="50%" />

<rotate
android:duration="3000"
android:fillEnabled="true"
android:fromDegrees="0"
android:pivotX="0%p"
android:pivotY="50%p"
android:toDegrees="30" />

</set> <!-- set-动画集 -->


二、Frame(帧动画)

帧动画相对比较简单,先找好需要的图片,然后在xml中配置,或者知己在java中添加即可。

Frame动画java代码

<span style="white-space:pre">	</span>View anima=findViewbyId(R.id.animation);
<span style="white-space:pre">	</span>anima.setBackgroundResource(R.anim.frame_anim);
AnimationDrawable frameAnimationDrawable = (AnimationDrawable) anima.getBackground();
frameAnimationDrawable.setOneShot(false);// true:不循环,false:一直循环
frameAnimationDrawable.start();


1、anim/**.xml中配置信息

<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
android:oneshot="false" >
<item android:drawable="@drawable/k_01"  android:duration="100"/>
<item android:drawable="@drawable/k_02"  android:duration="100"/>
<item android:drawable="@drawable/k_03"  android:duration="100"/>
<item android:drawable="@drawable/k_04"  android:duration="100"/>
<item android:drawable="@drawable/k_05"  android:duration="100"/>
<item android:drawable="@drawable/k_06"  android:duration="100"/>
<item android:drawable="@drawable/k_07"  android:duration="100"/>
<item android:drawable="@drawable/k_08"  android:duration="100"/>
<item android:drawable="@drawable/k_09"  android:duration="100"/>
<item android:drawable="@drawable/k_10"  android:duration="100"/>
<item android:drawable="@drawable/k_11"  android:duration="100"/>
<item android:drawable="@drawable/k_12"  android:duration="100"/>
<item android:drawable="@drawable/k_13"  android:duration="100"/>
<item android:drawable="@drawable/k_14"  android:duration="100"/>
<item android:drawable="@drawable/k_15"  android:duration="100"/>
<item android:drawable="@drawable/k_16"  android:duration="100"/>
<item android:drawable="@drawable/k_17"  android:duration="100"/>
<item android:drawable="@drawable/k_18"  android:duration="100"/>
<item android:drawable="@drawable/k_19"  android:duration="100"/>
<item android:drawable="@drawable/k_20"  android:duration="100"/>
<item android:drawable="@drawable/k_21"  android:duration="100"/>
<item android:drawable="@drawable/k_22"  android:duration="100"/>
<item android:drawable="@drawable/k_23"  android:duration="100"/>
<item android:drawable="@drawable/k_24"  android:duration="100"/>
afca
</animation-list>


2、java代码实现

<span style="white-space:pre"></span><span style="white-space:pre">		</span>AnimationDrawable frameAnimationDrawable = new AnimationDrawable();
<span style="white-space:pre">		</span>for (int j = 0; j < 24; j++) {
<span style="white-space:pre">			</span>int resourcesId = getResources().getIdentifier(
<span style="white-space:pre">					</span>j < 9 ? "k_0" + (j + 1) : "k_" + (j + 1),
<span style="white-space:pre">					</span>"drawable", this.getPackageName());//获取资源ID
<span style="white-space:pre">			</span>if (resourcesId != 0) {
<span style="white-space:pre">				</span>Drawable drableTemp = getResources().getDrawable(resourcesId);
<span style="white-space:pre">				</span>frameAnimationDrawable.addFrame(drableTemp, 100);//100该张图片显示时间
//<span style="white-space:pre">				</span>Log.i("Evil.Thief", "find resourcesId "+j);
<span style="white-space:pre">			</span>}else{
<span style="white-space:pre">				</span>Log.i("Evil.Thief", "not find resourcesId "+j);
<span style="white-space:pre">			</span>}
<span style="white-space:pre">		</span>}
<span style="white-space:pre">		</span>frameAnimationDrawable.setOneShot(true);//是否只执行一次,true只执行一次,false一直重复执行
<span style="white-space:pre">		</span>view.setBackgroundDrawable(frameAnimationDrawable);
<span style="white-space:pre">		</span>frameAnimationDrawable.start();


三、Property(属性动画)

属性动画,ObjectAnimator和ValueAnimator

1、ObjectAnimator相对来说比较简单,(但有的复杂动画不好实现)

<span style="white-space:pre">		</span>view.setPivotX(view.getWidth());//view是动画效果,该处设置的是view最右边为X的旋转中心。Y轴同理
/**单个动画实现*/
ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(view,
"rotationY", 0.0F, 360.0F); // view围绕Y轴旋转360度,旋转中心由view.setPrivotX决定,默认为view中心点
/**自定义属性修改*/
objectAnimator.addUpdateListener(new AnimatorUpdateListener() {

@Override
public void onAnimationUpdate(ValueAnimator animation) {
// TODO 自定义修改view属性
float value = (Float) animation.getAnimatedValue();
//做你想做的事
}
});
objectAnimator.setDuration(5000).start();

/**多个动画(动画集)实现*/
PropertyValuesHolder pvhX = PropertyValuesHolder.ofFloat("alpha", 1f,
0f);// 不透明到透明,"alpha" 是通过反射机制实现
PropertyValuesHolder pvhY = PropertyValuesHolder.ofFloat("scaleX", 1f,
0, 1f);// X缩小后再放在
PropertyValuesHolder pvhZ = PropertyValuesHolder.ofFloat("scaleY", 1f,
0, 1f);// Y缩小后再放大
ObjectAnimator.ofPropertyValuesHolder(view, pvhX, pvhY, pvhZ)
.setDuration(5000).start();


2、ValueAnimator动画其实个人认为最好用,如果你对动画不太熟悉可以能也是最难用的,它其实也不是动画,它只提供动画相关变化值,动画还需要你自己写。

ValueAnimator valueAnimator = ValueAnimator.ofFloat(0, 1);
valueAnimator.addUpdateListener(new AnimatorUpdateListener() {

@Override
public void onAnimationUpdate(ValueAnimator animation) {
// TODO Auto-generated method stub
int translationWidth = -view.getWidth()
+ ((View) view.getParent()).getWidth();
Float baseValue = (Float) animation.getAnimatedValue();
float translationX = translationWidth * baseValue;

if (translationX >= translationWidth * 0.7) {
float k = 0.7f;
float r = (baseValue - k);// 0-0.3
// 0-1
float m = 1 - (r * 1.5f);

view.setScaleX(m);
view.setScaleY(m);
view.setAlpha(m);
}
view.setTranslationX(translationX);
}
});
valueAnimator.setDuration(5000);
valueAnimator.start();
多个属性操作

<span style="white-space:pre">		</span>/**多个动画(动画集)实现*/
PropertyValuesHolder pvhX = PropertyValuesHolder.ofFloat("alpha", 1f,
0f);// 不透明到透明,"alpha" 是通过反射机制实现
PropertyValuesHolder pvhY = PropertyValuesHolder.ofFloat("scaleX", 1f,
0, 1f);// X缩小后再放在
PropertyValuesHolder pvhZ = PropertyValuesHolder.ofFloat("scaleY", 1f,
0, 1f);// Y缩小后再放大
ObjectAnimator.ofPropertyValuesHolder(view, pvhX, pvhY, pvhZ)
.setDuration(5000).start();


3、AnimatorSet:动画集

// 右一动
ObjectAnimator animTranslate = ObjectAnimator
.ofFloat(view, "x", 0, 600);
animTranslate.setDuration(5000);
// 缩小
ObjectAnimator animScaleX = ObjectAnimator.ofFloat(view, "scaleX", 1f,
0.5f);
animScaleX.setDuration(2000);
ObjectAnimator animScaleY = ObjectAnimator.ofFloat(view, "scaleY", 1f,
0.5f);
animScaleY.setDuration(2000);
// 透明度
ObjectAnimator animApha = ObjectAnimator.ofFloat(view, "alpha", 1f,
0.5f);
animApha.setDuration(2000);
// Set
AnimatorSet animSet = new AnimatorSet();
animSet.play(animTranslate);
animSet.play(animScaleX).after(3000);
animSet.play(animScaleY).after(3000);
animSet.play(animApha).after(3000);
// animSet.play(animTranslate2).after(animTranslate);
animSet.setDuration(000);
animSet.start();
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息