为了让用户更舒适的在某些情况下,利用动画是那么非常有必要的。Android在3.0一旦支持两种动画Tween动漫Frame动画。Tween动画支持简单的平移,缩放,旋转,渐变。Frame动画就像Gif图通过一系列图片来模拟动画效果,而在Android 3.0以后引入了新的动画就是属性动画(property animation)。 Android 分享一个简单有趣的动画效果就是利用了属性动画。


今天我们主要来学习Tween动画也就是View动画。

View 动画仅仅能应用于View对象,并且仅仅支持一部分属性。并且对于View 动画,它仅仅是改变了View对象绘制的位置。而没有改变View对象本身,比方当前有一个button的坐标是(200,200)通过平移动画移动到(200,500),可是你点击移动后的button是没有不论什么效果,例如以下图:


知道了这个大前提我们就開始了解View动画的基本使用方法吧。动画能够用java代码写也能够用xml写

1,平移动画

<?xml version="1.0" encoding="utf-8"?><translate xmlns:android="http://schemas.android.com/apk/res/android"    android:duration="3000"    android:fillAfter="true"    android:fromXDelta="0%"    android:fromYDelta="0%"    android:repeatCount="10"    android:repeatMode="restart"    android:toXDelta="0%"    android:toYDelta="70%" ></translate>
duration动画时间。fillAfter保持动画结束后状态,fromXDelta起始X位置,fromYDelta起始Y位置,repeatCount反复次数,repeatMode反复模式 restart为正序reverse为倒序,在java代码中用

Animation animation =AnimationUtils.loadAnimation(this, R.anim.tran_btn);

view.startAnimation(animation);


<span style="font-size:18px;">Animation animation2 = new TranslateAnimation(0, 20, 0, 0);animation2.setDuration(2000);animation2.setRepeatCount(10);animation2.setRepeatMode(Animation.RESTART);button.startAnimation(animation2);</span>
new TranslateAnimation(fromXDelta, toXDelta, fromYDelta, toYDelta)
看參数相信大家也都知道意思了


2。旋转动画

<?

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


大致同上。pivot旋转的中心点.

Animation animation2 = new RotateAnimation(0, 360, 0, 0);animation2.setDuration(2000);button.startAnimation(animation2);



3,渐变动画

<span style="font-size:18px;"><?

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

事实上看通一个剩下的都是触类旁通
<span style="font-size:18px;">Animation animation = new AlphaAnimation(1, 0);animation.setDuration(3000);button.startAnimation(animation);</span>


4。缩放动画

<?xml version="1.0" encoding="utf-8"?><scale xmlns:android="http://schemas.android.com/apk/res/android"    android:duration="2000"    android:fromXScale="0.0"    android:fromYScale="1.0"    android:pivotX="50%"    android:pivotY="50%"    android:toXScale="1.0"    android:toYScale="1.0" ></scale>



ok,这下四种基本动画都简单的结束了一下。可是我们有时可能会有一些特殊的需求。比方让播放一组动画,这时我们能够使用set

<?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:pivotX="50%"        android:pivotY="50%"        android:toDegrees="360" />    <alpha        android:duration="2000"        android:fromAlpha="1.0"        android:toAlpha="0.0" /></set>

这是旋转于渐变同一时候播放,假设依次播放的话仅仅需加上startOffset

<?

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:pivotX="50%" android:pivotY="50%" android:toDegrees="360" /> <alpha android:duration="2000" android:fromAlpha="1.0" android:startOffset="2000" android:toAlpha="0.0" /></set>

这样就能够依次播放。但有时我们想要一下特殊的效果比方说动画的加速度,这时候我们能够用上interpolator,animation.setInterpolator(new AccelerateInterpolator());

AccelerateDecelerateInterpolator 在动画開始与结束的地方速率改变比較慢。在中间的时候加速

AccelerateInterpolator 在动画開始的地方速率改变比較慢。然后開始加速

AnticipateInterpolator 開始的时候向后然后向前甩

AnticipateOvershootInterpolator 開始的时候向后然后向前甩一定值后返回最后的值

BounceInterpolator 动画结束的时候弹起

CycleInterpolator 动画循环播放特定的次数,速率改变沿着正弦曲线

DecelerateInterpolator 在动画開始的地方快然后慢

LinearInterpolator 以常量速率改变

OvershootInterpolator 向前甩一定值后再回到原来位置


谢谢耐心的看完。不积跬步无以至千里,有什么疑问的话也能够留言。。







更多相关文章

  1. Android(安卓)播放gif
  2. Android(安卓)的变形矩阵 -Matrix
  3. Android(安卓)UI开发第十二篇——动画效果Animation
  4. Android属性动画--基础使用
  5. 安卓xml文件中设置动画匀速旋转无效?
  6. Android(安卓)Matrix详解
  7. Android学习整理 - 系列
  8. Activity切换动画无效(android:windowIsTranslucent)影响(androi
  9. 安卓xml文件中设置动画匀速旋转无效?

随机推荐

  1. android facebook authorize 时禁止调用f
  2. android 模拟器访问PC
  3. android 系统(8)---Android(安卓)学习网站
  4. Android(安卓)Firebase Dynamic Links 动
  5. Android(安卓)版本号对应的SDK版本
  6. 安卓模拟器去掉头部标题
  7. android 根据string 获取资源文件的id
  8. 善用Android预定义样式
  9. 自定义单选按钮(RadioButton)的样式
  10. RelativeLayout常用属性介绍