Android动画的分类
1.补间动画(TweenAnimation)
2.帧动画(FrameAnimation)
3.属性动画(PropertyAnimation)
今天我们来介绍下补间动画TweenAnimation的使用。
Tween动画是操作某个控件让其展现出旋转、渐变、移动、缩放的这么一种转换过程。我们可以以XML形式定义动画。也可以编码实现。如果以XML形式定义一个动画,我们按照动画的定义语法完成XML,并放置于/res/anim目录下。文件名可以作为资源ID被引用;如果由编码实现。我们需要使用到Animation对象。建议使用XML文件定义,因为它更具可读性、可重用性。
其中包括alpha(透明度)、scale(缩放)、translate(移动)、rotate(翻转) 对应java code:
alpha->AlphaAnimation scale->ScaleAnimation translate->TranslateAnimation rotate->RotateAnimation
Animation基本属性
1,alpha动画
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<alpha
android:fromAlpha="1.0"
android:toAlpha="0.1"
android:duration="5000"
/>
</set>
2.scale动画
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<scale
android:duration="700"
android:fillAfter="false"
android:fromXScale="0.0"
android:fromYScale="0.0"
android:interpolator="@android:anim/accelerate_decelerate_interpolator"
android:pivotX="50%"
android:pivotY="100%"
android:toXScale="1.4"
android:toYScale="1.4" />
</set>
3.translate动画
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:duration="2000"
android:fromXDelta="30"
android:fromYDelta="30"
android:toXDelta="-80"
android:toYDelta="300" />
</set>
4.rotate动画
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<rotate
android:duration="3000"
android:fromDegrees="0"
android:interpolator="@android:anim/accelerate_decelerate_interpolator"
android:pivotX="50%"
android:pivotY="50%"
android:toDegrees="+350" />
</set>
在代码中通过AnimationUtils调用。
Animation animation = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.alpha);show.startAnimation(animation);
几种类型动画组合使用
Animation animation1 =
AnimationUtils.loadAnimation(getApplicationContext(),
R.anim.rotate);
Animation animation2 =
AnimationUtils.loadAnimation(getApplicationContext(),
R.anim.alpha);
Animation animation3 =
AnimationUtils.loadAnimation(getApplicationContext(),
R.anim.translate);
Animation animation4 =
AnimationUtils.loadAnimation(getApplicationContext(),
R.anim.scale);
AnimationSet animationSet =
new AnimationSet(
true);
animationSet.addAnimation(animation1);
animationSet.addAnimation(animation2);
animationSet.addAnimation(animation3);
animationSet.addAnimation(animation4);
show.startAnimation(animationSet);
当然你也可以用组合的方式在
xml
中将4个动画写在同一个set标签下面。像这样:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<scale
/>
<alpha
/>
<rotate
/>
<translate
/>
</set>
AnimationSet
继承自
Animation
是上面四种的组合容器管理类,没有自己特有的属性。他的属性继承自
Animation
所以特别注意。当我们对
set
标签使用
Animation
的属性时会对该标签下的所有子控件都产生影响。
转载请注明原文地址: https://ju.6miu.com/read-1301777.html