工作内容:
Android帧动画(在drawable中创建Animation resource file)
Android动画(在res中创建Android resource directory选择类型anim— 在其中创建文件
1.alpha 透明度动画
2.Scale 缩放(1.0表示1倍(原大小))
3.Translate 位移(X,Y轴都必须使用,20%p—指父布局的20%位置)
4.Rotate旋转(+360表示顺时针旋转,- 360表示逆时针旋转)
5.实现页面切换动画(右滑出,右进入)
1.帧动画:
实例:drawable下创建Drawable resource file,命名为:frameanimation
<animation-list>
<Item android:duration=”1000” android:drawable = “@drawalbe/imag1”/>
<Item android:duration=”1000” android:drawable = “@drawalbe/imag2”/>
<Item android:duration=”1000” android:drawable = “@drawalbe/imag3”/>
</animation-list>
布局文件中ImageView属性:background:"@drawable/frameanimation"
Java文件:
AnimationDrawable drawable = (AnimationDrawable)imageView.getDrawable();//某些情况下可以使用getBackground获得
drawable .start();
其余动画调用
Animation animation = AnimationUtils.loadAnimation(getContext(), R.anim.version_update_img_rotate);
vUpdatImg.startAnimation(animation);
实例:
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/accelerate_interpolator"
android:repeatCount="-1">
<rotate//旋转动画
android:pivotX="50%"
android:pivotY="50%"
android:fromDegrees="0"
android:toDegrees="+360"
android:duration ="2000"
android:repeatCount="-1"
android:repeatMode="restart"//设置循环方式
/>
<translate//位移动画
android:fromYDelta="0%p"
android:toYDelta="100%p"
android:duration="5000"
android:repeatCount="-1"//设置循环次数 -1为无限次
>
</translate>
</set>Animation animation2 = AnimationUtils.loadAnimation(this,R.anim.rotate);
imageView_addLife.startAnimation(animation2);//为imageView添加动画
<set xmlns:android="http://schemas.android.com/apk/res/android">
<scale//缩放
android:fromYScale="1.0"
android:fromXScale="1.0"
android:toYScale="0"//倍数是0
android:toXScale="1.0"
android:pivotX="50%p"
android:pivotY="100%p"
android:repeatCount="-1"
android:duration="2000"
/>
</set>
页面切换动画
在finish()后执行:overridePendingTransition(R.anim.in, R.anim.out);
在startActivity()后执行:overridePendingTransition(R.anim.leftin, R.anim.leftout);
xml代码(anim动画实现)——左侧退出
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/decelerate_interpolator" >
<translate
android:fromXDelta="0"
android:toXDelta="-100%"
android:duration="350" />
</set>
xml代码(anim动画实现)——右侧进入
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/decelerate_interpolator"
android:zAdjustment="top" >
<translate
android:duration="350"
android:fromXDelta="100%p"
android:toXDelta="0"
/>
</set>
在主题风格中添加
<pre name="code" class="html"><resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
<item name="android:windowIsTranslucent">true</item> <!--窗口半透明,false则会导致切换时闪一下黑色的背景-->
</style>
</resources>
注意:
1.如果下一个页面视频播放,需要做处理,因为窗口半透明导致。播放界面可以看到上一个界面,很丑,需要做处理
2.如果只在进入下一个页面时添加overridePendingTransition(R.anim.leftin, R.anim.leftout),也会有退出动画——变淡放大[应该是窗口半透明的退出效果]
下面是动画执行样式
android:interpolator="@android:anim/accelerate_interpolator" 设置动画为加速动画(动画播放中越来越快)
android:interpolator="@android:anim/decelerate_interpolator" 设置动画为减速动画(动画播放中越来越慢)
android:interpolator="@android:anim/accelerate_decelerate_interpolator" 设置动画为先加速在减速(开始速度最快 逐渐减慢)
android:interpolator="@android:anim/anticipate_interpolator" 先反向执行一段,然后再加速反向回来(相当于我们弹簧,先反向压缩一小段,然后在加速弹出)
android:interpolator="@android:anim/anticipate_overshoot_interpolator" 同上先反向一段,然后加速反向回来,执行完毕自带回弹效果(更形象的弹簧效果)
android:interpolator="@android:anim/bounce_interpolator" 执行完毕之后会回弹跳跃几段(相当于我们高空掉下一颗皮球,到地面是会跳动几下)
android:interpolator="@android:anim/cycle_interpolator" 循环,动画循环一定次数,值的改变为一正弦函数:Math.sin(2* mCycles* Math.PI* input)
android:interpolator="@android:anim/linear_interpolator" 线性均匀改变
android:interpolator="@android:anim/overshoot_interpolator" 加速执行,结束之后回弹
然后我们在代码中也可以设置,顺序效果同上
animation.setInterpolator(new AccelerateInterpolator());
animation.setInterpolator(new DecelerateInterpolator());
animation.setInterpolator(new AccelerateDecelerateInterpolator());
animation.setInterpolator(new AnticipateInterpolator());
animation.setInterpolator(new AnticipateOvershootInterpolator());
animation.setInterpolator(new BounceInterpolator());
animation.setInterpolator(new CycleInterpolator(2));
animation.setInterpolator(new LinearInterpolator());
animation.setInterpolator(new OvershootInterpolator());
动画不设置默认为匀速