您的位置:首页 > 其它

补间动画TweenAnimation-简单使用

2018-03-31 00:42 423 查看

1.补间动画有4种基本类型

透明度变化alpha , 大小变化scale , 位移变化translate , 旋转变化rotate
每种类型都有两种实现方式,xml资源方式 和 代码方式.

2.资源方式所有资源xml代码

在res/anim 中创建 补间动画文件
alphaanimation.xml<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/accelerate_decelerate_interpolator"
> <!--用了一个默认的插值器,可以性控制动画变化速度-->

<alpha
android:duration="2000"
android:fromAlpha="0.0"
android:toAlpha="1.0"
/> <!-- 0表示完全透明 -->

</set>scaleanimation.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:duration="2000"
android:fromXScale="0.2"
android:fromYScale="0.2"
android:toXScale="1.5"
android:toYScale="1.5"
android:pivotX="50%"
android:pivotY="50%"
/>
<!--
android:fromXScale 缩放开始尺寸
android:toXScale="1.5" 缩放结束尺寸
android:pivotX 缩放中心位置
-->
</set>translateanimation.xml<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"> <!--可以设置插值器-->

<translate
android:duration="2000"
android:fromXDelta="0"
android:fromYDelta="0"
android:toXDelta="0"
android:toYDelta="500"
/>
<!--
android:fromXDelta 开始位置
android:toXDelta 结束位置
-->

</set>rotateanimation.xml<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"><!--可以设置一个插值器-->

<rotate
android:duration="2000"
android:fromDegrees="0"
android:toDegrees="360"
android:pivotX="50%"
android:pivotY="50%"
android:repeatMode="restart"
android:repeatCount="-1"
android:startOffset="0"
/>
<!--
android:fromDegrees 开始时角度
android:toDegrees 结束时角度

通用属性:
android:repeatMode reverse表示倒序回放,restart表示从头播放
android:startOffset 等待开始运行的时间,单位为毫秒
android:repeatCount 重复次数 -1表示一直重复
-->

</set>

3.调用xml资源显示动画方式,及代码显示动画方式

//AlphaAnimation-资源方式
//imag1.setBackgroundResource(R.anim.alphaanimation); //该方法不行
Animation alphaanimation1 = AnimationUtils.loadAnimation(this, R.anim.alphaanimation); //必须用该方法
imag1.setAnimation(alphaanimation1);
//AlphaAnimation-代码方式
AlphaAnimation alphaAnimation2 = new AlphaAnimation(0 , 1); //透明度从0到1
alphaAnimation2.setDuration(5000);
alphaAnimation2.setFillAfter(true); //动画结束后保留结束时状态
alphaAnimation2.setInterpolator(new AccelerateDecelerateInterpolator());//用了一个插值器,控制变化速度
imag2.setAnimation(alphaAnimation2);
//ScaleAnimation-资源方式
Animation scaleanimation1 = AnimationUtils.loadAnimation(this , R.anim.scaleanimation);
imag1.setAnimation(scaleanimation1);
//ScaleAnimation-代码方式
ScaleAnimation scaleAnimation2 = new ScaleAnimation(1.0f , 4.0f , 1.0f , 4.0f , 0.0f , 0.0f);
//参数分别为:fromx .tox , fromy , toy , pivox , pivoy
scaleAnimation2.setDuration(5000);
scaleAnimation2.setFillAfter(true);
imag2.setAnimation(scaleAnimation2);
//TranslateAnimation-资源方式
Animation translateanimation1 = AnimationUtils.loadAnimation(this , R.anim.translateanimation);
imag1.setAnimation(translateanimation1);
//TranslateAnimation-代码方式
TranslateAnimation translateAnimation2 = new TranslateAnimation(0f , 500f , 0f , 0f);
//参数分别为:fromx , tox , fromy , toy
translateAnimation2.setDuration(5000);
translateAnimation2.setFillAfter(true);
imag2.setAnimation(translateAnimation2);
//RotateAnimation-资源方式
Animation rotateanimation1 = AnimationUtils.loadAnimation(this , R.anim.rotateanimation);
imag1.setAnimation(rotateanimation1);
//RotateAnimation-代码方式
RotateAnimation rotateAnimation2 = new RotateAnimation(90f , 0f , 0.5f , 0.5f);
//参数分别为:fromDegres , toDegress , pivox , pivoy
//如果开始的角度小于结束的角度,则顺时针转,否则逆时针转
rotateAnimation2.setDuration(5000);
rotateAnimation2.setFillAfter(true);
imag2.setAnimation(rotateAnimation2);

4.ok,简单使用到此为止

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: